Disable auto focus on previous EditText after current EditText is hidden - android

I have two EditTexts. EditText1 and EditText2
I also have a feature that hides EditText2 depending on which option you choose.
The problem is that if you are focusing on EditText2 and run setVisibility(View.GONE) on it then it automatically reverts focus back to EditText1 which does other stuff.
How can I prevent any focus events and just keep everything the same?
I've tried
EditText2.setFocusableInTouchMode(true);
EditText2.setFocusable(true);
EditText2.setFocusableInTouchMode(false);
EditText2.setFocusable(false);
While they disable focus on EditText2 As soon as visibility is set to gone, EditText1 receives focus

This solved my problem. Stop EditText from gaining focus at Activity startup
Putting this right before my EditText
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>

Related

android - edittext keyboard not showing

I am creating an edittext programmatically. But I was not able to type anything into the edittext. I have covered all the related answers on this site but none of them worked for me. Maybe I am missing some minor details. It would be helpful if I can get some help.
Code to create edittext dynamically:
EditText editText = new EditText(dt.c);
editText.setBackground(context.getResources().getDrawable(R.drawable.rectangle_answer));
editText.setTextColor(ContextCompat.getColor(dt.c, R.color.md_black_1000));
editText.setTextSize(16);
editText.setTag(questionDetails[0].getOptionId());
editText.setFocusable(true);
editText.setHint(dt.gStr(R.string.enter_your_answer));
editText.requestFocus();
container.Add(editText);
Some of the answers that I covered:
How can I set the focus (and display the keyboard) on my EditText programmatically
Android - EditText made programmatically not showing keyboard
Android: How to open keyboard for editing EditText when click button?
Disable keyboard on EditText
How to disable the keyboard when I click on EditText?
I was having the same problem and nothing worked until I found an answer way down in one of the posts from years ago that worked. I'll repost the weird hack for anyone who might have this problem in the future.
In your xml file, add a dummy edittext to your topmost layout layer that looks like this:
<EditText
android:id="#+id/editTextFix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAutofill="no"
tools:targetApi="o"
android:inputType="text"
android:visibility="gone"/>
the dummy edittext will open the keyboard, but your programatically created edittext will still have focus.

How to make cursor visible with inputype set to null?

I am creating a simple calculator app which has a Edittext and some buttons to take input from user.
<EditText
android:id="#+id/editText"
style="#style/editTextStyle"
android:text="0"
android:inputType="none"
android:textIsSelectable="true" />
Following is the java code.
editText = (EditText) findViewById(R.id.editText);
editText.setInputType(InputType.TYPE_NULL);
I set the input type to null so that when the user taps on the edittext, the default system keyboard stays hidden. But this also makes the cursor invisible. When I insert some text into the Edittext and taps on it, the cursor position changes but the cursor is not visible.
Is there any way to make the cursor visible with input type set to null??
Update:
I tried using the following code.
editText = (EditText) findViewById(R.id.editText);
editText.setInputType(InputType.TYPE_NULL);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
This solution works for android versions till Nougat. When I tap on Edittext, the system keyboard stays hidden and cursor is visible. But in Oreo, this solution does not work. When I tap on Editext, the system keyboard pops up.
is there any way to keep system keyboard hidden while cursor is visible??
You should use:
editText.setTextIsSelectable(true);
Use this in your Activity:
editText.setFocusable(true);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT‌​_INPUT_STATE_ALWAYS_HIDDEN);
you don't need to InputType.TYPE_NULL
In onCreate() of the activity where you have the edittext box:
protected void onCreate(Bundle savedInstanceState) {
mAmountText = (EditText) findViewById(R.id.amount);
// This will disable the Soft Keyboard from appearing by default
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// Provide focus to the Amount box (since it is the top edittext box inside RelativeLayout)
mAmountText.requestFocus();
}
In your layout file (call it note_edit.xml):
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout
android:id=”#+id/main_layout”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#ffffffff”
android:descendantFocusability=”beforeDescendants”
android:focusableInTouchMode=”true”
>
<EditText
android:id=”#+id/amount”
android:textColor=”#ff0066ff”
android:numeric=”decimal”
android:gravity=”left”
android:hint=”$0.00″
android:textSize=”48sp”
android:layout_height=”72dp”
android:layout_width=”fill_parent”
android:windowSoftInputMode=”stateHidden”
>
</EditText>
</RelativeLayout>

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.

"Next" key on soft keyboard does not display

Well this is making me a bit crazy.
I've got 2 Edit Text fields and a button. I want the soft keyboard to display when the 1st Edit Text gets focus. I want the soft keyboard to show the "Next" key so when its pressed the cursor jumps to the second Edit Text field.
When the second Edit Text field gets the cursor, I still want the soft keyboard to show "Next". Pressing "Next" now will dismiss the soft keyboard and make focus go to the button.
Here's my code:
EditText editText1 = (EditText) findViewById(R.id.some_text_field);
EditText editText2 = (EditText) findViewById(R.id.some_other_text_field);
editText1.setInputType(EditorInfo.some constant);
editText1.setFocusable(true);
editText1.setFocusableInTouchMode(true);
if (editText1.requestFocus()){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
}
editText1.setImeOptions(EditorInfo.IME_ACTION_NEXT);
editText1.setNextFocusDownId(R.id.some_other_text_field);
editText2.setInputType(EditorInfo.some other constant);
editText2.setFocusable(true);
editText2.setFocusableInTouchMode(true);
editText2.setImeOptions(EditorInfo.IME_ACTION_DONE);
Seems pretty straightforward.
Thing is, the first time this runs (after app install) editText1 gets focus and the soft keyboard displays BUT there is no "Next" key - instead it displays "Done". Pressing "Done" moves you to editText2 (like you want) and changes "Done" to "Next. Hitting "Next" now leaves focus on editText2 and dismisses the soft keyboard.
IF you touch editText1 now, the soft keyboard displays and the "Next" key appears like its supposed to.
What can I do to get the proper behavior (editText1 gets focus, "Next" displays) to happen on the first run of this Activity?
I thought editText1.setImeOptions(EditorInfo.IME_ACTION_NEXT); was supposed to force the "Next" key to display. What am i missing?
Have you tried doing the same from layout file. I did it in layout file and is working. Following is my working code for login edittexts
<EditText
android:id="#+id/et_sign_in_with_email_email"
style="#style/SignInEditText"
android:layout_below="#id/rl_titlebar_signin"
android:layout_marginTop="16dp"
android:hint="#string/hint_email"
android:imeOptions="actionNext"
android:inputType="textEmailAddress" >
</EditText>
<EditText
android:id="#+id/et_sign_in_with_email_pwd"
style="#style/SignInEditText"
android:layout_below="#+id/et_sign_in_with_email_email"
android:layout_marginTop="20dp"
android:hint="#string/hint_pwd"
android:imeOptions="actionDone"
android:inputType="textPassword" />
to navigate the focus to the next edit field add
android:imeOptions="flagNavigateNext"
and for dismissing the softkey with done click add
android:imeOptions="actionDone"

how to keep softkeyboard even after I clicked send button

I use EditText to do input actions, and set android:imeOptions="actionSend" to the widget, so that the soft keyboard can have a send button, but when I click send, the soft keyboard will disappeared, however, it's not friendly to users since user must click EditText again to use the soft keyboard, all I want is to keep the soft keyboard, does any one know how to achieve it?
Here is a piece of my layout code:
<EditText
android:id="#+id/input_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/emo_btn"
android:inputType="textAutoCorrect"
android:singleLine="true"
android:imeOptions="actionSend"/>
you are using setOnEditorActionListener or setOnKeyListener ? on both case try your return statement with true

Categories

Resources