[android] Move to another EditText when Soft Keyboard Next is clicked on Android

When I press the 'Next', the focus on the User EditText must be move to the Password. Then, from Password, it must move to the right and so on. Can you help me on how to code it?

enter image description here

<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="User Name*" />

    <EditText
        android:id="@+id/txt_User"
        android:layout_width="290dp"
        android:layout_height="33dp"
        android:singleLine="true" />

</LinearLayout>


<LinearLayout
    android:id="@+id/LinearLayout02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password*" />

    <EditText
        android:id="@+id/txt_Password"
        android:layout_width="290dp"
        android:layout_height="33dp"
        android:singleLine="true"
        android:password="true" />

    <TextView
        android:id="@+id/confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password*" />

    <EditText
        android:id="@+id/txt_Confirm"
        android:layout_width="290dp"
        android:layout_height="33dp"
        android:singleLine="true"
        android:password="true" />

</LinearLayout>

This question is related to android android-edittext

The answer is


In some cases you may need to move the focus to the next field manually :

focusSearch(FOCUS_DOWN).requestFocus();

You might need this if, for example, you have a text field that opens a date picker on click, and you want the focus to automatically move to the next input field once a date is selected by the user and the picker closes. There's no way to handle this in XML, it has to be done programmatically.


Simple way :

  • Auto move cursor to next edittext
  • If edittext is last input -> hidden keyboard

Add this to edittext field in .xml file

android:inputType="textCapWords"

android:inputType="text"

should bring the same effect. After hiting next to bring the focus to the next element.

android:nextFocusDown="@+id/.."

use this in addition if you dont want the next view to get the focus


Inside Edittext just arrange like this


<EditText
    android:id="@+id/editStWt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:imeOptions="actionNext" //now its going to rightside/next field automatically
    ..........
    .......

</EditText>

If you want to use a multiline EditText with imeOptions, try:

android:inputType="textImeMultiLine"

just use the following code it will work fine and use inputType for every edittext and the next button will appear in keyboard.

android:inputType="text" or android:inputType="number" etc

<AutoCompleteTextView
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/user"
                android:hint="@string/username"
                android:inputType="text"
                android:maxLines="1"
                android:imeOptions="actionNext"
                android:singleLine="true" />

These three lines do the magic

            android:maxLines="1"
            android:imeOptions="actionNext"
            android:singleLine="true"

If you have the element in scroll view then you can also solve this issue as :

<com.google.android.material.textfield.TextInputEditText
                android:id="@+id/ed_password"
                android:inputType="textPassword"
                android:focusable="true"
                android:imeOptions="actionNext"
                android:nextFocusDown="@id/ed_confirmPassword" />

and in your activity:

edPassword.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                focusOnView(scroll,edConfirmPassword);
                return true;
            }
            return false;
        }
    });

public void focusOnView(ScrollView scrollView, EditText viewToScrollTo){
    scrollView.post(new Runnable() {
        @Override
        public void run() {
            scrollView.smoothScrollTo(0, viewToScrollTo.getBottom());
            viewToScrollTo.requestFocus();
        }
    });
}

add your editText

android:imeOptions="actionNext"
android:singleLine="true"

add property to activity in manifest

    android:windowSoftInputMode="adjustResize|stateHidden"

in layout file ScrollView set as root or parent layout all ui

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.ukuya.marketplace.activity.SignInActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

       <!--your items-->

    </ScrollView>

</LinearLayout>

if you do not want every time it adds, create style: add style in values/style.xml

default/style:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="editTextStyle">@style/AppTheme.CustomEditText</item>
    </style>

<style name="AppTheme.CustomEditText"     parent="android:style/Widget.EditText">
        //...
        <item name="android:imeOptions">actionNext</item>
        <item name="android:singleLine">true</item>
    </style>

Try Using android:imeOptions="actionNext" tag for every editText in the View it will automatically focus to the next edittext when you click on Next of the softKeyboard.


Use the following line

android:nextFocusDown="@+id/parentedit"

parentedit is the ID of the next EditText to be focused.

The above line will also need the following line.

android:inputType="text"

or

android:inputType="number"

Thanks for the suggestion @Alexei Khlebnikov.


Add inputType to edittext and on enter it will go the next edittext

android:inputType="text"
android:inputType="textEmailAddress"
android:inputType="textPassword" 

and many more.

inputType=textMultiLine does not go to the next edittext on enter


<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="666dp"
android:background="#1500FFe5"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editGrWt"
    android:layout_marginTop="14dp"
    android:layout_toLeftOf="@+id/textView3"
    android:ems="6"
    android:text="    Diamond :"
    android:textColor="@color/background_material_dark"
    android:textSize="15sp" />
  <EditText
    android:id="@+id/editDWt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/TextView02"
    android:layout_alignLeft="@+id/editGrWt"
    android:background="@color/bright_foreground_inverse_material_light"
    android:ems="4"
    android:hint="Weight"
    android:inputType="numberDecimal"
    android:nextFocusLeft="@+id/editDRate"
    android:selectAllOnFocus="true"
    android:imeOptions="actionNext"

    />
 <requestFocus />


<TextView
    android:id="@+id/TextView03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/TextView02"
    android:layout_below="@+id/TextView02"
    android:layout_marginTop="14dp"
    android:ems="6"
    android:text="    Diamond :"
    android:textColor="@color/background_material_dark"
    android:textSize="15sp" />

<EditText
    android:id="@+id/editDWt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/TextView03"
    android:layout_alignBottom="@+id/TextView03"
    android:layout_alignLeft="@+id/editDWt"
    android:background="@color/bright_foreground_inverse_material_light"
    android:ems="4"
    android:hint="Weight"
    android:inputType="numberDecimal"
    android:text="0"
    android:selectAllOnFocus="true"
    android:imeOptions="actionNext"/>
 <requestFocus />

<TextView
    android:id="@+id/TextView04"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editDWt1"
    android:layout_marginTop="14dp"
    android:layout_toLeftOf="@+id/textView3"
    android:ems="6"
    android:text="         Stone :"
    android:textColor="@color/background_material_dark"
    android:textSize="15sp" />

<EditText
    android:id="@+id/editStWt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/TextView04"
    android:layout_alignBottom="@+id/TextView04"
    android:layout_alignLeft="@+id/editDWt1"
    android:background="@color/bright_foreground_inverse_material_light"
    android:ems="4"
    android:hint="Weight"
    android:inputType="numberDecimal"
    android:nextFocusForward="@+id/editStRate1"
    android:imeOptions="actionNext" />
 <requestFocus />
  <TextView
     android:id="@+id/TextView05"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/TextView04"
     android:layout_below="@+id/editStRate1"
     android:layout_marginTop="14dp"
     android:ems="6"
     android:text="         Stone :"
     android:textColor="@color/background_material_dark"
     android:textSize="15sp" />


</RelativeLayout>

</ScrollView>

In Kotlin I have used Bellow like..

  1. xml:

    <EditText
      android:id="@+id/et_amount"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:imeOptions="actionNext"
      android:inputType="number"
      android:singleLine="true" />
    
  2. in kotlin:

    et_amount.setOnEditorActionListener { v, actionId, event ->
    
        if (actionId == EditorInfo.IME_ACTION_NEXT) {
            // do some code
            true
        } else {
            false
        }
    }
    

android:inputType="textNoSuggestions"
android:imeOptions="actionNext"
android:singleLine="true"
android:nextFocusForward="@+id/.."

Adding extra field

android:inputType="textNoSuggestions"

worked in my case!


In your onEditorAction handler, keep in mind that you must return a boolean that indicates if you are handling the action (true) or if you applied some logic and want the normal behaviour (false), as in the following example:

EditText te = ...
te.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
        if (actionId == EditorInfo.IME_ACTION_NEXT) {
            // Some logic here.
            return true; // Focus will do whatever you put in the logic.
        }
        return false;  // Focus will change according to the actionId
    }
});

I found this when I returned true after performing my logic since focus did not move.


Simple way, when you have just few fields one by one:

Need to set

android:maxLines="1"

android:imeOptions="actionNext"

android:inputType="" <- Set your type of text, in other case it will be Multiline and prevent to go next

Sample:

<EditText android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textSize="@dimen/text_large"
              android:maxLines="1"
              android:inputType="textEmailAddress"
              android:imeOptions="actionNext"
              android:layout_marginLeft="@dimen/element_margin_large"
              android:layout_marginRight="@dimen/element_margin_large"
              android:layout_marginTop="0dp"/>