248

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>
androidBoomer
  • 3,357
  • 6
  • 32
  • 42

18 Answers18

523

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;
    }
});

Bhavesh Bansal
  • 101
  • 1
  • 8
Tobrun
  • 18,291
  • 10
  • 66
  • 81
  • 24
    In my case `android:nextFocusForward="@+id/.."` did the trick. – Mr.Moustard Nov 17 '14 at 12:28
  • 30
    I put `android:imeOptions="actionNext"` to all my EditTexts and the problem is gone. Lot of thanks. – Joaquin Iurchuk Jun 08 '15 at 16:29
  • 3
    I had to use both joaquin and Mr.Moustard's for each EditText. – SMBiggs Jan 19 '16 at 16:33
  • 11
    For my app, just using android:imeOptions="actionNext" didn't work. The EditText needed android:inputType="[something]" before it would listen to the imeOptions flag. – Chris Kelly Aug 11 '16 at 22:39
  • agree with you @ChrisKelly – Abhishek Singh Jan 04 '17 at 12:00
  • 4
    How does it know which one to focus when pressing NEXT ? Does it check the "right" and then the "down" ? Is there a function to get the next view's id to be focused, if the user would press the NEXT key ? – android developer Feb 16 '17 at 11:32
  • 1
    @androiddeveloper When pressing NEXT, the focus goes to down. You can set the next view id by, `android:nextFocusDown="@+id/NextFocusItem"`. – Sameera Nandasiri Aug 02 '17 at 12:59
  • @SameeraNandasiri What about when there is a focusable view on the right? and, is there a function to get the next view's id to be focused, if the user would press the NEXT key ? – android developer Aug 02 '17 at 14:51
  • @androiddeveloper Even if the view is on the right, you need to set it as nextFocusDown in order to focus changed by pressing NEXT. Its behavior is, focus go down when pressing NEXT. So by manually changing nextFocusDown parameter, we can override that behavior. It may be not the right way. But it works! – Sameera Nandasiri Aug 04 '17 at 12:05
  • @SameeraNandasiri I see. But what about the other question I asked: is there a function to get the next view's id to be focused, if the user would press the NEXT key ? – android developer Aug 06 '17 at 11:26
  • @androiddeveloper Yes, You can get the next view's id to be focused by `.getNextFocusDownId();` – Sameera Nandasiri Aug 08 '17 at 10:41
  • @SameeraNandasiri What would happen if the other EditText doesn't have ID, or if it has an ID, but we just didn't set the attribute of "android:nextFocusDown" ? – android developer Aug 08 '17 at 10:43
  • 1
    @androiddeveloper In that case, I don't know a method to do it. If I wanted that functionality, I would set nextFocusDown attribute. I would like to know if there is a way to get Next Focus when nextFocusDown is not set. – Sameera Nandasiri Aug 08 '17 at 11:18
  • That was the question. How to get the next view that will get the focus... Anyway, thank you. – android developer Aug 08 '17 at 12:01
  • This works if the field type is EditText. I was using AutocompleteTextView, there it didn't work, later on I had to change field type to EditText to make it work. Thanks. – kamal_prd Apr 17 '18 at 13:58
  • i used same for next focus but issue with EditText with password inputType ,when i press next button from keyboard focus goes to passwordToggle(hide and show) eye icon. what to do for focus next edittext instead of that eye icon. – Arbaz.in Aug 27 '19 at 11:33
70
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

Matthias H
  • 1,300
  • 13
  • 19
39

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>
Abror Esonaliev
  • 11,797
  • 2
  • 23
  • 20
24

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.

Mohammedsalim Shivani
  • 1,793
  • 3
  • 19
  • 30
Spring Breaker
  • 8,233
  • 3
  • 36
  • 60
13
android:inputType="textNoSuggestions"
android:imeOptions="actionNext"
android:singleLine="true"
android:nextFocusForward="@+id/.."

Adding extra field

android:inputType="textNoSuggestions"

worked in my case!

Tripathee Gaurav
  • 371
  • 3
  • 11
9

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.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
user3701500
  • 91
  • 1
  • 1
9
<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"
Hitesh Kushwah
  • 1,351
  • 13
  • 23
8

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
        }
    }
    
Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
6

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.

Kuldeep Rathee
  • 282
  • 2
  • 7
5

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
pawan kumar
  • 81
  • 1
  • 1
5

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"/>
PsyhoLord
  • 69
  • 1
  • 3
4

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.

Mickäel A.
  • 9,012
  • 5
  • 54
  • 71
2

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

1
<?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>
1

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

android:inputType="textImeMultiLine"
ZGski
  • 2,398
  • 1
  • 21
  • 34
Chris
  • 11
  • 2
1

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"
vuhoanghiep1993
  • 715
  • 1
  • 8
  • 15
0
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>
Kishore Reddy
  • 2,394
  • 1
  • 19
  • 15
0

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();
        }
    });
}