The question:
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?
<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>
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
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
the action key performs a “next” operation, taking the user to the
next field that will accept text.
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;
}
});
Method 2
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
Method 3
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>
Method 4
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.
Method 5
android:inputType="textNoSuggestions"
android:imeOptions="actionNext"
android:singleLine="true"
android:nextFocusForward="@+id/.."
Adding extra field
android:inputType=”textNoSuggestions”
worked in my case!
Method 6
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.
Method 7
<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"
Method 8
In Kotlin I have used Bellow like..
-
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" />
-
in kotlin:
et_amount.setOnEditorActionListener { v, actionId, event -> if (actionId == EditorInfo.IME_ACTION_NEXT) { // do some code true } else { false } }
Method 9
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"/>
Method 10
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
Method 11
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.
Method 12
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.
Method 13
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
Method 14
<?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>
Method 15
If you want to use a multiline EditText
with imeOptions
, try:
android:inputType="textImeMultiLine"
Method 16
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"
Method 17
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>
Method 18
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();
}
});
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0