Monday, November 18, 2024
Google search engine
HomeLanguagesJavaHow to change Input Method Action Button in Android?

How to change Input Method Action Button in Android?

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 Done
    1. 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.xml




      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
          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.neveropen.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;
                  }
              };
    }

    
    
RELATED ARTICLES

Most Popular

Recent Comments