Move to another EditText when Soft Keyboard Next is clicked on Android
Focus Handling
Focus movement is based on an algorithm which finds the nearest
neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer.
Change default behaviour of directional navigation by using following XML attributes:
android:nextFocusDown="@+id/.."
android:nextFocusLeft="@+id/.."
android:nextFocusRight="@+id/.."
android:nextFocusUp="@+id/.."
Besides directional navigation you can use tab navigation. For this you need to use
android:nextFocusForward="@+id/.."
To get a particular view to take focus, call
view.requestFocus()
To listen to certain changing focus events use a View.OnFocusChangeListener
Keyboard button
You can use android:imeOptions
for handling that extra button on your keyboard.
Additional features you can enable in an IME associated with an editor
to improve the integration with your application. The constants here
correspond to those defined by imeOptions.
The constants of imeOptions includes a variety of actions and flags, see the link above for their values.
Value example
ActionNext :
the action key performs a "next" operation, taking the user to the
next field that will accept text.
ActionDone :
the action key performs a "done" operation, typically meaning there is nothing more to input and the IME will be closed.
Code example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="16dp"
android:imeOptions="actionNext"
android:maxLines="1"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="24dp"
android:imeOptions="actionDone"
android:maxLines="1"
android:ems="10" />
</RelativeLayout>
If you want to listen to imeoptions events use a TextView.OnEditorActionListener
.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
Android: Enter key move to next EditText
You can add imeOptions
attribute for EditText's
in your xml:
android:imeOptions="actionNext"
But not forget to specify inputType
:
For example, android:inputType="text"
For more details check this question - Move to another EditText when Soft Keyboard Next is clicked on Android
Move to next Edittext automatically after entering number
Follow these steps it will give you your desired result :-
Update your XML with ids in EditTexts
like this :-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id ="@+id/firstET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/four_pin_background"
android:inputType="number"
android:textAlignment="center"
android:maxLength="1"
android:imeOptions="actionNext"
android:cursorVisible="false"/>
<EditText
android:id ="@+id/secondET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/four_pin_background"
android:inputType="number"
android:textAlignment="center"
android:maxLength="1"
android:imeOptions="actionNext"
android:cursorVisible="false"/>
</LinearLayout>
Then inside your JAVA Code ,
Take Reference of these EditTexts
, and add this code inside your onCreate()
method of your Activity like :-
EditText firstET , secondET;
firstET = (EditText) findViewByID(R.id.firstET);
secondET = (EditText) findViewByID(R.id. secondET);
firstET.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// add a condition to check length here - you can give here length according to your requirement to go to next EditTexts.
if(firstET.getText().toString().trim().length() >2){
firstET.clearFocus();
firstET.requestFocus();
}
}
});
Moving to the next EditText in android
You can check the key pressed for 1st EditText and if it was "Enter" key,then move focus to next EditText.
This may help you: KeyCode_Enter to next edittext
move to nearest edittext element when next button is pressed in keyboard
just add this to your EditText
android:imeOptions="actionNext"
android:nextFocusDown="@+id/yourNextEditText"
like this one.
<EditText
android:inputType="textPostalAddress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/PD_Street"
android:hint="Street"
android:layout_weight="50"
android:textColor="@color/darkgray"
android:textSize="@dimen/textSizeCommon"
android:fontFamily="@string/fontFamily"
android:singleLine="true"
android:imeOptions="actionNext" <!--ADD THIS LINE -->
android:nextFocusDown="@+id/PD_Barangay" <!--ADD THIS LINE -->
android:nextFocusForward="@+id/PD_Baranggay" <!--REMOVE THIS LINE-->
android:nextFocusRight="@+id/PD_Baranggay" <!--REMOVE THIS LINE-->
android:maxLength="@string/genericLongLength" />
and do this in other EditText
component that you need to have focus on pressing Next
button.
Android keyboard next button issue on EditText
add this lines below your lines of code you provided:
txtUserid.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
txtUserid.clearFocus();
txtUserPasword.requestFocus();
return true;
}
return false;
}
});
txtUserPasword.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
// check for username - password correctness here
return true;
}
return false;
}
});
How i to move cursor from one edit text another when keyboard backspace key pressing Android
You can use requestFocus()
method in text change listener:
opt1 = findViewById(R.id.opt1);
opt1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() == 1) {
opt2.requestFocus();
}
}
});
opt2 = findViewById(R.id.opt2);
opt2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() == 1) {
opt3.requestFocus();
}
if (s.toString().length() == 0) {
opt1.requestFocus();
}
}
});
opt3 = findViewById(R.id.opt3);
opt3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() == 0) {
opt2.requestFocus();
}
}
});
Related Topics
Android No on Screen Keybord ( Only Voice )
How to Convert Android.Media.Image to Bitmap Object
How to Reduce an Image File Size Before Uploading to a Server
How to Auto Fit Recyclerview Items to the Width of Screen Android
Retrieve Stored Image from Firebase Storage
Notify Observer When Item Is Added to List of Livedata
Viewmodel Onchange Gets Called Multiple Times When Back from Fragment
How to Lazy Load Images in Listview in Android
Onmeasure Custom View Explanation
Send Text to Specific Contact Programmatically (Whatsapp)
Java.Io.Filenotfoundexception Open Failed Eacces (Permission Denied) on Device
Android: Rotate Image in Imageview by an Angle
How to Retrieve Data from Sqlite Database in Android and Display It in Textview
How to Create an Android View Pager With a Dots Indicator
Architecture Components: Observer Keep Observing Even After Removing It on Ondestroy