Strange behaviour of edittext when receiving focus - android

I have 2 layouts with EditText widget in them. In the first one EditText automatically receives focus on activity's start but shows no software keyboard (this is the desired behaviour). In the second layout, however, EditText is focused and shows soft-keyboard. I examined both layouts and can't find a reason for this behavour, because both widgets have identical properties. Soft-keyboard should only be displayed when I touch the input field. Am I missing something?
<EditText
android:id="#+id/scanLine"
android:layout_width="253.0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="19.0dp"
android:imeOptions="actionDone"
android:singleLine="true"
android:inputType="text"
android:maxLines="1"
android:focusableInTouchMode="true" />

Add the following line to the onCreate() method of your activity:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Hope it helps!

Related

Two fragments stacked ontop of each other using transactional ADD - how to prevent edit text gaining focus on bottom fragment?

Try stacking two fragments with editTexts on top of each other using an Add Transaction. after that when you press the keyboard imeOption key next button the bottom fragment's edit text can gain focus. this is a security concern. user can type things into the bottom fragments edit text (blindly). I tried the following code:
android:filterTouchesWhenObscured="true"
but it has not helped at least on api 27.
my edit text itself looks like this, nothing special:
<EditText
android:id="#+id/et"
android:layout_width="195dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:imeOptions="actionNone"
android:layout_marginBottom="10dp"
android:hint="#string/enter_name"
android:filterTouchesWhenObscured="true"
android:inputType="textNoSuggestions"
android:textColorHint="#959595"
android:textSize="11sp" />
the issue is very similar to android tap jacking
i tried even doing this:
android:nextFocusDown="#+id/et_two" thinking it would bypass and go directly to the edittext i want. but instead the bottom edit text still gains focus.
the issue solution was surprising. recyclerview was stealing focus. see this SO incident:
adding android:descendantFocusability="blocksDescendants" to the recyclerview stopped the issue.

How do I show the Phone Input softkeyboard when my activity starts?

Between the Activity tags in my AndroidManifest.xml I have :
android:windowSoftInputMode = "stateVisible"
So when my activity starts the soft keyboard comes up:
Is there not something I can add to make the phone input keyboard come up instead? Or how would I do it? Thanks.
EDIT
For the sake of clarity to this question I'm adding this 'edit'.
First of all, thanks for all the answers below.
However, my objective is to have no Edittext Visible on the screen, at first. The Phone Input soft keyboard comes up as soon as the activity starts and when the user starts
to type then the Edittext will become visible, with the text being typed.
I tried making the Edittext invisible, and then make it visible when user starts typing, but the problem is an invisible edittext can't have the focus,
So, android:inputType="number" was having no effect. And neither was: edittext.requestFocus(); Because the edittext was invisible.
What I ended up doing eventually was something very simple, setting width and height to 0dp in my xml like :
android:id="#+id/etPhoneNumber"
android:layout_width="0dp"
android:layout_height="0dp"
android:inputType="number" />
That way, the edittext can get focus with
edittext.requestFocus();
as soon as the activity is created, and inputType = "number" is recognised.
The next thing I'll do is increase the size of the edittext as soon as a key is hit on the soft keyboard, so users can see it.
I'll put all this in as an answer so it might be helpful to other users. +1 for the help!
Assuming that you have an EditText in the Activity which you show once your app is started, you can add below XML Attribute to your EditText.
android:inputType="phone"
Check developer docs for other input types in case if you need.
You can add
android:inputType="phone"
or
android:inputType="number"
to your View in your XML layout.
You should have some View in your code where you can give input like EditText or AutoCompleteTextView for changing the type of the keypad.
For example -
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="phone"
/>
In manifest file set the property in activity,
android:windowSoftInputMode="stateAlwaysVisible"
and then in your xml where your EditText set the property,
android:inputType="number"
first of all take an EditText in your xml
<EditText
android:id="#+id/etPhoneNumber"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number" />
use this piece of code in the onCreate method of your activity !
EditText etPhoneNumber = (EditText) findViewById(R.id.etPhoneNumber);
etPhoneNumber.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
This works for me very well ! Hope it works for you too !
Let me know if this works ! :)
The objective is to have no Edittext Visible on the screen, at first.
The Phone Input soft keyboard comes up as soon as the activity starts and when the user starts
to type then the Edittext will become visible, with the text being typed.
I tried making the Edittext invisible, and then make it visible when user starts typing, but the problem is an invisible edittext can't have the focus,
So, android:inputType="number" was having no effect. And neither was: edittext.requestFocus(); Because the edittext was invisible.
What I ended up doing eventually was something very simple, setting width and height to 0dp in my xml like :
android:id="#+id/etPhoneNumber"
android:layout_width="0dp"
android:layout_height="0dp"
android:inputType="number" />
That way, the edittext can get focus with
edittext.requestFocus();
as soon as the activity is created, and inputType = "number" is recognised.
The next thing to do now is increase the size of the edittext as soon as a key is hit on the soft keyboard, so users can see it.

How to make Android EditText expand vertically when full

I have this EditText
<EditText
android:layout_width="match_parent"
android:layout_height="72dp"
android:hint="#string/write_message"
android:textColorHint="#color/primary_color"
android:ems="10"
android:imeOptions="actionDone"
android:inputType="textImeMultiLine"
android:id="#+id/message_input"
android:layout_gravity="center_horizontal"
android:backgroundTint="#color/primary_color"/>
When the box is filled up with user inputted text, it scrolls to the right to make room for more text, I do not like this behavior, and would prefer it if the EditText box expanded upwards when it needs more room? Is there a way to do this? Thanks.
Yes, this actually involves two things:
Making the EditText accept multi-line input.
Having its height grow as more text lines are added.
Therefore, to achieve this, you need to set up:
android:inputType="textMultiLine"
android:layout_height="wrap_content"
(Be mindful of the difference between textMultiLine and textImeMultiLine).
The full XML snippet would be:
<EditText
android:layout_width="match_parent"
android:inputType="textMultiLine"
android:layout_height="wrap_content"
android:hint="#string/write_message"
android:textColorHint="#color/primary_color"
android:ems="10"
android:imeOptions="actionDone"
android:id="#+id/message_input"
android:layout_gravity="center_horizontal"
android:backgroundTint="#color/primary_color"/>
Use both flags: textMultiLine will wrap your input, and textImeMultiLine will provide a break-line key in your keyboard.
<EditText
...
android:layout_height="wrap_content"
android:inputType="textImeMultiLine|textMultiLine"
... />
In my case I have multiline text, but it showed one line and a keyboard:
Though I have already set android:inputType="textCapSentences|textAutoCorrect|textMultiLine", it didn't help. Then I understood that when the keyboard appears in DialogFragment, it collapses the EditText. See DialogFragment and force to show keyboard to show keyboard when DialogFragment shows.
Then I added a short delay (100-300 ms) before showing the keyboard. Now I have:
In AndroidManifest I set android:windowSoftInputMode="adjustResize" for current activity.

Click on EditText won't show the keyboard?

I have a table whose tapping on a cell changes its content and displays some edit texts. The first of these edit texts requests the focus, but oddly when I tap on it, the keyboard doesn't show. It is when I tap the others edit texts though. What should I do? Here is how I implemented my first edit text.
<EditText
android:id="#+id/editContent"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="top"
android:inputType="textMultiLine" >
</requestFocus>
</EditText>
When I don't request the focus, there is also a strange behaviour: it opens the keyboard when I tap it but my edit text loses the focus and I've to tap it again to write in it.
I found the problem, it was because of this line in the Activity block of the Manifest :
android:windowSoftInputMode="stateHidden"
I changed it by :
android:windowSoftInputMode="adjustPan"
Works well now.

How to keep cursor from going to editText field on activity start android

I am having a problem with text edits. I suppressed the keyboard from poping up on activity start, but how do I keep the cursor from being on the first edit text? I want the cursor to appear only when the editText field is clicked and when the activity starts.
<EditText
android:id="#+id/stuff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/stufflabel"
android:layout_toRightOf="#+id/stuffpic"
android:hint="#string/name_str"
android:maxLength="20"
/>
Kindly search the forum for similar posts.
one post i found is -
Android: Force EditText to remove focus?
I hope you are looking for something similar !
enjoy :)
You can add a dummy EditText on inside layout of the XML file, so that the focus will get on that EditText and not on other EditText.
<EditText
android:layout_width="0dp"
android:layout_height="0dp" />

Categories

Resources