How to change Input Method Action Button in Android? - GeeksforGeeks

14 Jul, 2021
In this article, IME(Input Method Action) Option is changed in android according to our requirement. Input Method Action Button is located in the bottom right corner of the soft keyboard. By default, the system uses this button for either a Next or Done action unless your text field allows multi-line text, in which case the action button is a carriage return. However, you can specify additional actions that might be more appropriate for your text fields, such as Go, Send, Previous, None, etc. The IME option for Next and Done are shown below. 
Action Next
Action DoneAdd the following code in activity_main.xml file.In this file we specify IME option to the EditText according to the need. Here an imageview and two edittexts are added and and IME option is specified for actionDone.activity_main.xmlactivity_main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout    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"    android:orientation="vertical"    tools:context=".MainActivity">      <ImageView        android:layout_gravity="center"        android:layout_marginTop="50dp"        android:layout_width="200dp"        android:layout_height="200dp"        android:src="@drawable/gfg"        />      <EditText        android:layout_marginTop="50dp"        android:layout_marginStart="10dp"        android:layout_marginEnd="10dp"        android:id="@+id/editText1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Username"        android:paddingBottom="20dp"        android:inputType="text" />      <EditText        android:paddingTop="20dp"        android:layout_margin="10dp"        android:id="@+id/editText2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Password"        android:imeOptions="actionDone"        android:inputType="textPassword" />  </LinearLayout>Now add the following code in MainActivity.java file. In this file we add listener to our EditText which will correspond to our action accordingly. Here we override onEditorAction method and show toast for ACTION DONE and ACTION NEXT IME options.MainActivity.javaMainActivity.javapackage org.geeksforgeeks.imeoption;  import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.KeyEvent;import android.view.inputmethod.EditorInfo;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          EditText login = findViewById(R.id.editText1);        EditText password = findViewById(R.id.editText2);          login.setOnEditorActionListener(actionListener);        password.setOnEditorActionListener(actionListener);    }      // whenever the user clicks on IME button this listener will    // get invoked automatically.    private TextView.OnEditorActionListener actionListener        = new TextView.OnEditorActionListener() {              @Override              public boolean onEditorAction(TextView v,                                            int actionId, KeyEvent event)              {                  switch (actionId) {                    case EditorInfo.IME_ACTION_NEXT:                      Toast.makeText(MainActivity.this,                                     "Next", Toast.LENGTH_SHORT)                          .show();                      break;                    case EditorInfo.IME_ACTION_DONE:                      Toast.makeText(MainActivity.this,                                     "Login", Toast.LENGTH_SHORT)                          .show();                      break;                  }                  return false;              }          };}My Personal Notes
arrow_drop_upSave
Add the following code in activity_main.xml file.In this file we specify IME option to the EditText according to the need. Here an imageview and two edittexts are added and and IME option is specified for actionDone.activity_main.xmlactivity_main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout    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"    android:orientation="vertical"    tools:context=".MainActivity">      <ImageView        android:layout_gravity="center"        android:layout_marginTop="50dp"        android:layout_width="200dp"        android:layout_height="200dp"        android:src="@drawable/gfg"        />      <EditText        android:layout_marginTop="50dp"        android:layout_marginStart="10dp"        android:layout_marginEnd="10dp"        android:id="@+id/editText1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Username"        android:paddingBottom="20dp"        android:inputType="text" />      <EditText        android:paddingTop="20dp"        android:layout_margin="10dp"        android:id="@+id/editText2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Password"        android:imeOptions="actionDone"        android:inputType="textPassword" />  </LinearLayout>
Add the following code in activity_main.xml file.In this file we specify IME option to the EditText according to the need. Here an imageview and two edittexts are added and and IME option is specified for actionDone.activity_main.xmlactivity_main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout    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"    android:orientation="vertical"    tools:context=".MainActivity">      <ImageView        android:layout_gravity="center"        android:layout_marginTop="50dp"        android:layout_width="200dp"        android:layout_height="200dp"        android:src="@drawable/gfg"        />      <EditText        android:layout_marginTop="50dp"        android:layout_marginStart="10dp"        android:layout_marginEnd="10dp"        android:id="@+id/editText1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Username"        android:paddingBottom="20dp"        android:inputType="text" />      <EditText        android:paddingTop="20dp"        android:layout_margin="10dp"        android:id="@+id/editText2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Password"        android:imeOptions="actionDone"        android:inputType="textPassword" />  </LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout    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"    android:orientation="vertical"    tools:context=".MainActivity">      <ImageView        android:layout_gravity="center"        android:layout_marginTop="50dp"        android:layout_width="200dp"        android:layout_height="200dp"        android:src="@drawable/gfg"        />      <EditText        android:layout_marginTop="50dp"        android:layout_marginStart="10dp"        android:layout_marginEnd="10dp"        android:id="@+id/editText1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Username"        android:paddingBottom="20dp"        android:inputType="text" />      <EditText        android:paddingTop="20dp"        android:layout_margin="10dp"        android:id="@+id/editText2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Password"        android:imeOptions="actionDone"        android:inputType="textPassword" />  </LinearLayout>
Now add the following code in MainActivity.java file. In this file we add listener to our EditText which will correspond to our action accordingly. Here we override onEditorAction method and show toast for ACTION DONE and ACTION NEXT IME options.
MainActivity.java
package org.geeksforgeeks.imeoption;  import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.KeyEvent;import android.view.inputmethod.EditorInfo;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          EditText login = findViewById(R.id.editText1);        EditText password = findViewById(R.id.editText2);          login.setOnEditorActionListener(actionListener);        password.setOnEditorActionListener(actionListener);    }      // whenever the user clicks on IME button this listener will    // get invoked automatically.    private TextView.OnEditorActionListener actionListener        = new TextView.OnEditorActionListener() {              @Override              public boolean onEditorAction(TextView v,                                            int actionId, KeyEvent event)              {                  switch (actionId) {                    case EditorInfo.IME_ACTION_NEXT:                      Toast.makeText(MainActivity.this,                                     "Next", Toast.LENGTH_SHORT)                          .show();                      break;                    case EditorInfo.IME_ACTION_DONE:                      Toast.makeText(MainActivity.this,                                     "Login", Toast.LENGTH_SHORT)                          .show();                      break;                  }                  return false;              }          };}
ruhelaa48
Android-Misc
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Android Listview in Java with Example
Retrofit with Kotlin Coroutine in Android
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples