imeOptions "actionNext" programmatically - how to jump to next field? - android

In layout XML it is possible to specify android:imeOptions="actionNext" which adds Next button in virtual keyboard and by clicking on it - focus jumps to the next field.
How to do this programmatically - e.g. based on some event trigger focus to go to the next field?

You can use the constants from EditorInfo class for the IME options.
like,
editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);

Search for the next focusable field and than invoke requestFocus().
TextView nextField = (TextView)currentField.focusSearch(View.FOCUS_RIGHT);
nextField.requestFocus();

Just suggestion, if you are using
EditTextSample.setImeOptions(EditorInfo.IME_ACTION_DONE);
it doesn't work, make sure that your EditText is using a single line.
Eg:
editTextSample.setSingleLine();

There is always necessity to add extra keys apart from default keys available in virtual QWERTY keyboard.
Using XML
<EditText android:text="#+id/EditText01"
android:id="#+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:imeOptions="actionDone"/>
By Programmatic Way
An EditorInfo is most useful class when you have to deal with any type of user input in your Android application.
IME_ACTION_DONE: This action performs a “done” operation for nothing to input and the IME will be closed.
EditTextSample.setImeOptions(EditorInfo.IME_ACTION_DONE);
For more information you may visit http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html

The kotlin pendant
editText.imeOptions = EditorInfo.IME_ACTION_DONE

editText.setLines(1);
editText.setSingleLine(true);
editText.setImeOptions(EditorInfo.IME_ACTION_GO);
I solve the problem make sure with single line and go next editText when click enter

In my case, set an imeOptions fix the problem.
edtAnswer.maxLines = 1
edtAnswer.inputType = InputType.TYPE_CLASS_TEXT
edtAnswer.imeOptions = EditorInfo.IME_ACTION_NEXT

I tried all of the answers but EditorAction worked for me!
EditText.onEditorAction(EditorInfo.IME_ACTION_NEXT)
My XML layout:
<EditText
android:id="#+id/input1"
style="#style/edittext"
android:nextFocusForward="#id/input2"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/input2"
style="#style/edittext"
android:nextFocusLeft="#id/input1"
android:layout_weight="1"
android:nextFocusRight="#id/input3"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
and Kotlin code:
input1.onEditorAction(EditorInfo.IME_ACTION_NEXT)
Now focus moves to the input2 Edittext.

You can do this by
edittext.imeOptions = EditorInfo.IME_ACTION_DONE //for done button
or
edittext.imeOptions = EditorInfo.IME_ACTION_NEXT //for next button
But...
you need to understand that if you are using filters for edittext then you need to set
edittext.setSingleLine()

You can jump to the next field by using the following code:
BaseInputConnection inputConnection = new BaseInputConnection(editText, true);
inputConnection.performEditorAction(EditorInfo.IME_ACTION_NEXT);
//Use EditorInfo.IME_ACTION_UNSPECIFIED if you set android:imeOptions on the EditText

Related

EditText | Android TV | Softkeyboard

I have tried many different ways to get NEXT button on the keyboard, but nothing worked.
Below are few Stackoverflow links i have followed.
Setting EditText imeOptions to actionNext has no effect
imeOptions "actionNext" programmatically - how to jump to next field?
I have seen few Android TV application, using below keyboard for inputs.
Any idea how can i get it?
EDIT:
Here is my XML
<EditText
android:id="#+id/etNric"
android:layout_width="#dimen/three_hundred"
android:layout_height="wrap_content"
android:hint="#string/hint_nirc"
android:singleLine="true"
android:imeOptions="actionNext"
android:maxLines="1"
android:minLines="1"
android:nextFocusDown="#id/etCaptcha" />
you need to have to use android:nextFocusForward by the way you are also giving the id in a wrong way. and I believe the real problem is also with that way of giving id. although you can use nextFocusDown too
Use #+id/ instead of #id/
android:imeOptions="actionNext"
android:nextFocusForward="#+id/yourNextEdittext"
And you need to specify the inputType="yourInputType" also to make an edittext work.

Android Keyboard style with OK Button

I am creating an app and I need help.
I want to create a EditText and when the user open the keyboard to fill the edit text, that keyboard must have the "OK"or "Done" button inside it.
Example:
Thank you so much
You need to use ime options action done
You need to use single line (although deprecated)
example:
<EditText
android:id="#+id/saveDialogEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyz1234567890-_ ."
android:hint="#string/enter_palette_name_hint"
android:imeOptions="actionDone"
android:inputType="textAutoComplete"
android:singleLine="true"/>

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 automatically pop-up keyboard?

I have Edit Text field where I have to input a password, but I have to push this field. How to automatically pop-up keyboard without touching the Edit Text?
There is an Edit text xml field:
<EditText
android:id="#+id/editPasswd"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:password="true"/>
Use this code in the point where you want to display the keyboard (may be in oCreate?)
EditText myEditText = (EditText) findViewById(R.id.editPasswd);
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);
I'm assuming you mean to automatically pop it up when the activity starts. If so, adding this to the activity-tag in the manifest solved it for me (unlike Erfan's answer):
android:windowSoftInputMode="stateVisible"
One line in the Java class:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

How do I make an Android EditView 'Done' button and hide the keyboard when clicked?

When the user clicks on the EditView, Android opens the keyboard so that user can write in the EditView.
The problem is, when the user is done writing, there is no way to hide the keyboard. The user has to press the back button to hide the keyboard.
Is there a way to display a Done button on the keyboard that will hide the keyboard?
First you need to set the android:imeOptions attribute equal to actionDone for your target EditText as seen below. That will change your ‘RETURN’ button in your EditText’s soft keyboard to a ‘DONE’ button.
<EditText
android:id="#+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter some text"
android:imeOptions="actionDone"
android:singleLine="true"
/>
Use TextView.setImeOptions and pass it actionDone.
like textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
Include both imeOptions and singleLine:
<EditText
android:id="#+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
android:imeActionLabel="Done"
android:singleLine="true"
In the XML file works just fine. But this will also cause the editText to keep typing in a single line which you may not want. So adding following to your code will make sure that you won't end up typing everything on a single line.
mainText.setHorizontallyScrolling(false);
mainText.setMaxLines("Maximum integer value that you want to provide");
For getting the done Button
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
and
android:inputType="text" in the xml
For handling on done clicked from keyboard
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
if(actionId == EditorInfo.IME_ACTION_DONE){
// Your action on done
return true;
}
return false;
}
});
`
Use this:
android:singleLine="true"
Use These two lines to your EditText
android:imeActionLabel="Done"
android:singleLine="true"
or you can achieve it Programmatically by this line.
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
Kotlin Solution
The direct way to handle the hide keyboard + done action in Kotlin is:
// Set action
edittext.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Hide Keyboard
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
true
}
false
}
Kotlin Extension
Use this to call edittext.onDone {/*action*/} in your main code. Keeps it more readable and maintainable
edittext.onDone { edittext.hideKeyboard() }
fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
fun EditText.onDone(callback: () -> Unit) {
// These lines optional if you don't want to set in Xml
imeOptions = EditorInfo.IME_ACTION_DONE
maxLines = 1
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
callback.invoke()
true
}
false
}
}
Additional Keyboard Extensions
If you'd like more ways to simplify working with the keyboard (show, close, focus): Read this post
Don't forget to add these options to your edittext Xml, if not in code
<EditText ...
android:imeOptions="actionDone"
android:inputType="text"/>
Need inputType="textMultiLine" support? Read this post and don't add imeOptions or inputType in Xml
Use:
android:imeActionLabel="Done"
android:singleLine="true"
If the property does not change for the widget it may be better to use like
android:imeOptions="actionDone" in the layout xml file.
I have to point that out as a lot of people can struggle into that without knowing the problem.
If you want the kb to hide when clicking Done, and you set android:imeOptions="actionDone" & android:maxLines="1" without setting your EditText inputType it will NOT work as the default inputType for the EditText is not "text" as a lot of people think.
so, setting only inputType will give you the results you desire whatever what you are setting it to like "text", "number", ...etc.
For the code:
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
ActionDone is use when click in next button in the keyboard that time
keyboard is hide.Use in Edit Text or AppcompatEdit
XML
1.1 If you use AppCompatEdittext
<android.support.v7.widget.AppCompatEditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"/>
1.2 If you use Edittext
<EditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"/>
JAVA
EditText edittext= (EditText) findViewById(R.id.edittext);
edittext.setImeOptions(EditorInfo.IME_ACTION_DONE);
If you are using
android:imeOptions="actionDone"
then you must use
android:inputType="text"
then only you can see Action Done button in Keyboard.
Actually you can set custom text to that little blue button. In the xml file just use
android:imeActionLabel="whatever"
on your EditText.
Or in the java file use
etEditText.setImeActionLabel("whatever", EditorInfo.IME_ACTION_DONE);
I arbitrarily choose IME_ACTION_DONE as an example of what should go in the second parameter for this function. A full list of these actions can be found here.
It should be noted that this will not cause text to appear on all keyboards on all devices. Some keyboards do not support text on that button (e.g. swiftkey). And some devices don't support it either. A good rule is, if you see text already on the button, this will change it to whatever you'd want.
use this in your view
<EditText
....
....
android:imeOptions="actionDone"
android:id="#+id/edtName"
/>
If you don't want any button at all (e.g. you are developing a GUI for blind people where tap cannot be positional and you rely on single/double/long taps):
text.setItemOptions(EditorInfo.IME_ACTION_NONE)
Or in Kotlin:
text.imeOptions = EditorInfo.IME_ACTION_NONE

Categories

Resources