How to make EditText not focused when creating Activity - java

I've read the other questions discussing this, and all of them work for my layouts, except for the very first one created.
At the moment, this is at the top of my onCreate method:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
^ That makes it so at least the keyboard doesn't pop up on startup, but the EditText is still focused on.
This is the XML for my EditText:
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
This is what it looks like when I bring up my activity:
The problem is that with some phones, when an EditText is focused like this, they can't write in it. I want it to not focus.
What I've done works for the next layouts brought up in that it the EditTexts are not focused on and would look more like this:
Notice that it's the same layout, though. This is the screen after the user has been brought back to this screen, which would indicate nothing wrong with the XML because this is the same XML, but the problem is that the EditText is only focused on when the activity is created.
I've done research and all of the other questions don't help me with this (they did however help the keyboard not show up, thankfully). How can I make it so the EditText on startup will look like the second screenshot, rather than the first?

You can set property of Layout like android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="true"
Example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
</RelativeLayout>
May this one helpful ;)

XML code:
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:focusable="false"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
Java code:
EditText edPwd = (EditText)findViewById(R.id.password);
edtPwd.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
set focusable false in xml and set it true via the code

In your main_layout
add this 2 lines:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"> *YOUR LAYOUT CODE* </RelativeLayout>

the best solution is here:
https://stackoverflow.com/a/45139132/3172843
the correct and simple solution is to setFocusable false and setFocusableInTouchMode true . so the EditText gain focus only when user touch that EditText
android:focusable="false"
android:focusableInTouchMode="true"

Add this in onCreate()
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
or in onCreateView()
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

XML Code
<EditText
android:imeOptions="flagNoExtractUi"
android:focusable="false"
/>
Java Code in onClickListerner
mEdtEnterPhoneNumber.setFocusable(true);
mEdtEnterPhoneNumber.setFocusableInTouchMode(true);

Whenever I'm trying to open Editext after selection item of Spinner, so on that time not able to open keyboard & write down the values in Edit text, but I resolved my issue with this code.
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/titleName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:hint="#string/hint_your_full_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Thanks!

You can do this programmatically. use editText.setEnabled(false) in your onStart() method of your activity (not in onCreate() - because this is some method for initializing GUI components)

It is possible to use android:focusable="false" to disable it, but if that does not work for some reason, then you can simply put a LinearLayout and it will take the focus without disrupting your layout.
NOTE: Eclipse will give you an error saying that your LinearLayout is useless because it has no contents. You should be able to disregard it with no problems.
For example:
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="false"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
</LinearLayout>

The easiest way is to add
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
in the activity tag of the Manifest.xml file

1) Its the simplest,open the manifest and put the following code between the activity tag:
android:windowSoftInputMode="stateHidden"
2) Put this attributes in the parent layout
android:focusable="true"
android:focusableInTouchMode="true"

In Manifest , copy and paste the beneath code.
<activity
android:name=".LoginActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>

<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden" />
android:windowSoftInputMode="stateAlwaysHidden"
add this line in the activity tag of the Manifest.xml file.when you click on the EditText the keyboard gets into focus.

use android:focusable="false" for focusing disable
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30"
android:focusable="false" >
</EditText>

Related

Can't fully hide android material card background

Image how it looks
Can't fully hide android material card background. I have searched and watched all videos available but nothing has worked so far. I know I could just put something to fill the card but I want to know just in case.
The code:
<com.google.android.material.card.MaterialCardView
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#40000000"
android:clickable="true"
app:cardCornerRadius="30dp"
android:layout_margin="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Planner"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="20sp"
android:fontFamily="#font/nova_bold"/>
<ImageView
android:layout_width="150dp"
android:layout_height="130dp"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:padding="1dp"
app:srcCompat="#drawable/ic_plan_vec" />
</com.google.android.material.card.MaterialCardView>
please use in your XML app:cardBackgroundColor="#color/yourColor"
or in your java code card.setCardBackgroundColor(YourColor); instead of the default setBackgroundColor()
UPDATE
for the opacity change android:backgroundTint="#40000000" by android:alpha="0.4"

How do make layout in android studio?

I am trying to make an activity which should look like the following:
here
The problem is that it does not get the same when I start the app. The buttons are cut or the text is cut. And if I change the position of one button, everything gets in the upper-left-corner.
My question is: What do I have to do that everything gets the position I want?
I used a Relative Layout. Maybe this is the problem?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button3"
android:layout_width="245dp"
android:layout_height="68dp"
android:layout_below="#+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="83dp"
android:layout_marginTop="26dp"
android:layout_marginEnd="83dp"
android:layout_marginBottom="238dp"
android:text="Info"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="normal" />
<Button
android:id="#+id/button"
android:layout_width="245dp"
android:layout_height="68dp"
android:layout_centerInParent="true"
android:text="Kartenansicht"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="normal" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="115dp"
android:layout_above="#+id/button2"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="71dp"
android:layout_marginTop="112dp"
android:layout_marginEnd="73dp"
android:layout_marginBottom="18dp"
android:text="Um zu überprüfen, ob Sicherheitskameras in der Nähe sind, bitte hier drücken:"
android:textSize="20sp" />
<Button
android:id="#+id/button2"
android:layout_width="244dp"
android:layout_height="62dp"
android:layout_above="#+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="83dp"
android:layout_marginEnd="84dp"
android:layout_marginBottom="24dp"
android:text="Start"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
</RelativeLayout>
You have a big mess in your layout. For example:
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
These 2 parameters can't co-exist.
For your desired layout you should use LinearLayout with Orientation set to vertical. That way, all you'll need to do is write your UI objects 1 below the other and they will be placed 1 above the other. To center them, you can then use gravity and layout_gravity
android:layout_gravity="center"
android:gravity="center"
And that should be all.
Edit:
Just for you to know, Relative layout basically places any given object at position 0,0 of the layout. If you create few objects, they will be placed one on top of the other.
use the constraintlayout and for example you give each view a constraint of start and end after you can set the width to 0dp
if you would like to create the design by dragging and dropping then it would be better to use constraint layout instead of relative layout, but if you like to use relative layout anyway, you should use "centerInParent = "true" "in the middle button and set "layout_above = "#+id/your_centered_view_id" for the above button and "layout_below = "#+id/your_centered_view_id" ".

AndroidStudio EditText can't be edited (cursor disappear)

I'm having a weird problem i've never encountered.
I'm building a layout for an activity. My only issue is, i can't edit the edittexts in any way. I've tried both with the emulator and the real device. To be more specific, when i click on the edittext, the cursor appears for a very short moment, then disappears. The keyboard shows up but get stuck and doesn't work. In the java code only enable / disable the the editext :
#Override
public void onClick(View v) {
if(bottlename.isEnabled()){
//bottlenumber.setEnabled(false);
ArrayList<Bottle> bottle = BottleView.getBottlelist();
bottle.set(Integer.parseInt(bottlenumber.getText().toString())-1, new Bottle(bottlename.getText().toString(),Integer.parseInt(bottlenumber.getText().toString()), R.drawable.vodka));
bottlename.setEnabled(false);
}else{
bottlename.setEnabled(true);
//bottlenumber.setEnabled(true);
}
}
});
Any idea what could be the issue?
I've already tried many solutions from other posts, with no luck.
Attaching the xml code here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/bottleimage"
android:layout_width="150dp"
android:layout_height="160dp" />
<LinearLayout
android:layout_width="350dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/bottlename"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#android:color/transparent"
android:enabled="false"
android:focusable="true"
android:gravity="fill_vertical"
android:text="Bottle"
android:textAlignment="center"
android:inputType="text"
android:textSize="36sp"
android:textStyle="bold" />
<EditText
android:id="#+id/bottlenumber"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#android:color/transparent"
android:enabled="false"
android:focusable="true"
android:gravity="fill_vertical"
android:inputType="number"
android:text="X"
android:textAlignment="center"
android:textSize="24sp" />
</LinearLayout>
<ImageButton
android:id="#+id/editbottle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:scaleType="center"
android:src="#android:drawable/ic_menu_edit" />
</LinearLayout>
</RelativeLayout>
Is all your EditText fields disabled (android:enabled="false") in your layout intentionally?
the behavior it's a bit weird, I suggest to move the action of reading the value of the editText to a button, so you will have to change the text of the button like this :
When EditTexts enabled ==> "Send"
When EditTexts disabled ==> "Enable"

Android: Placing imageButton over a Button

I would like to place 2 small icons (with actions) over a big Button that take the 1/3 of the screen. The 2 icons and the button have to support actions when we click on them. I'm pretty sure I have to use imageButton for the icons. However I can't find any way to keep the icons (imageButton) on top of the button.
Here is my code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<ImageButton
android:id="#+id/icon1"
android:src="#drawable/ic1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/ic1"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action1"/>
<ImageButton
android:id="#+id/icon2"
android:src="#drawable/ic2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:contentDescription="#string/ic2"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action2"/>
<com.myapp.CustomViews.myButton
android:id="#+id/big_button1"
android:text="#string/text_big_button1"
android:background="#drawable/changing_button1"
android:textColor="#color/white"
android:layout_height="match_parent"
android:textSize="#dimen/font_size_buttons"
android:layout_width="match_parent"
android:gravity="center"
android:textAllCaps="false"
android:capitalize="none"
android:onClick="bigAction1"/>
</RelativeLayout>
The "#drawable/changing_button1" on the big Button allows me to put one color for the button and a different one when focused or pressed.
I tried so many different things and I couldn't find a way while I'm sure there is a simple solution. The icons remain hidden behind the big Button.
Thank you for your help!
You could use FrameLayout to overlay views. For example:
<FrameLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<com.myapp.CustomViews.myButton
android:layout_gravitiy="center"
.../>
<ImageButton
android:layout_gravitiy="left|center_vertical"
.../>
<ImageButton
android:layout_gravitiy="right|center_vertical"
.../>
</FrameLayout>
Here is a slightly modified version of your layout, which works fine.
I replaced all your custom stuff with plain android and standard buttons, and moved the big button up in the layout, because if it is at the bottom, it has a higher z-index and overlays the two small buttons.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/big_button1"
android:text="BIG BUTTON"
android:background="#android:color/holo_blue_dark"
android:textColor="#color/white"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:textAllCaps="false"
android:capitalize="none"
android:onClick="bigAction1"/>
<Button
android:id="#+id/icon1"
android:text="BT1"
android:background="#android:color/holo_red_dark"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action1"/>
<Button
android:id="#+id/icon2"
android:text="BT2"
android:background="#android:color/holo_green_dark"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action2"/>
</RelativeLayout>
How about changing order in the layout ? Place your Button widget on top, then declare 2 icons after that ?
You should add android:translationZ="5dip" for the 2 imageButton. It works (you should run and see, it didn't appear so on the IDE)
I finally found a way to do it.
The solution is to use the RelativeLayout as a button and replace the Button by a simple TextView:
<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:background="#drawable/custom_button_changing_color"
android:clickable="true"
android:focusable="true"
android:id="#+id/button_layout"
android:onClick="bigAction1">
<TextView
android:id="#+id/text_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text"
android:gravity="center"
android:layout_centerVertical="true"
android:textColor="#color/white"
android:textSize="#dimen/font_size_buttons"
android:textAllCaps="false"
android:background="#color/transparent"/>
<ImageButton
android:id="#+id/icon1"
android:src="#drawable/ic1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/ic1"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action2"/>
<ImageButton
android:id="#+id/icon2"
android:src="#drawable/ic2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:contentDescription="#string/ic2"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action3"/>
</RelativeLayout>
I hope this helps! :)

align text center with android

I know it sounds easy. I need to put a text in center, but when the text is too long it needs to go below, but still align in the center of my xml.
Here's my code :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_centerHorizontal="true"
>
<TextView
android:id="#+id/showdescriptiontitle"
android:text="Title"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
I put paddingTop and Bottom because I need some space.
PS: My code is bigger; it's in a RelativeLayout.
Set also android:gravity parameter in TextView to center.
For testing the effects of different layout parameters I recommend to use different background color for every element, so you can see how your layout changes with parameters like gravity, layout_gravity or others.
use this way
txt.setGravity(Gravity.CENTER);
use this way in xml
<TextView
android:id="#+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time is precious, so love now."
android:gravity="center"
android:textSize="30dp"
android:textColor="#fff"
/>
You can use the following:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Your text"
android:typeface="serif" />
</LinearLayout>
The layout needs to be relative for the "center" property to be used.
Adding android:gravity="center" in your TextView will do the trick (be the parent layout is Relative/Linear)!
Also, you should avoid using dp for font size. Use sp instead.
Just add these 2 lines;
android:gravity="center_horizontal"
android:textAlignment="center"
add layout_gravity and gravity with center value on TextView
<TextView
android:text="welcome text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
/>
android:layout_gravity="center"
or
android:gravity="center"
both works for me.
Or check this out this will help align all the elements at once.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
>
<TextView
android:id="#+id/showdescriptiontitle"
android:text="Title"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
If you want to change the alignment from the mainActivity.java file, then use the gravity. For example:
chatMsg.setGravity(Gravity.RIGHT);
chatMsg is my textView. I wanted the text to be right aligned and this code worked well.
you can easily done this by adding these lines
android:layout_width="match_parent"
android:gravity="center"
android:textAlignment="center"

Categories