Android Use Done button on Keyboard to click button

141,590

Solution 1

You can use this one also (sets a special listener to be called when an action is performed on the EditText), it works both for DONE and RETURN:

max.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                Log.i(TAG,"Enter pressed");
            }    
            return false;
        }
    });

Solution 2

You can try with IME_ACTION_DONE .

This action performs a “done” operation for nothing to input and the IME will be closed.

 Your_EditTextObj.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                  /* Write your logic here that will be executed when user taps next button */


                    handled = true;
                }
                return handled;
            }
        });

Solution 3

Kotlin Solution

The base way to handle the done action in Kotlin is:

edittext.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Call your code here
        true
    }
    false
}

Kotlin Extension

Use this to call edittext.onDone {/*action*/} in your main code. Keeps it more readable and maintainable

edittext.onDone { submitForm() }

fun EditText.onDone(callback: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            callback.invoke()
            true
        }
        false
    }
}

Don't forget to add these options to your edittext

<EditText ...
    android:imeOptions="actionDone"
    android:inputType="text"/>

If you need inputType="textMultiLine" support, read this post

Solution 4

Try this:

max.setOnKeyListener(new OnKeyListener(){
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event){
        if(keyCode == event.KEYCODE_ENTER){
            //do what you want
        }
    }
});

Solution 5

Try this for Xamarin.Android (Cross Platform)

edittext.EditorAction += (object sender, TextView.EditorActionEventArgs e) {
       if (e.ActionId.Equals (global::Android.Views.InputMethods.ImeAction.Done)) {
           //TODO Something
       }
};
Share:
141,590
mpeerman
Author by

mpeerman

Updated on July 09, 2020

Comments

  • mpeerman
    mpeerman almost 4 years

    Ok in my app I have a field for the user to input a number. I have the field set to only accept numbers. When the user clicks on the field it brings up the keyboard. On the keyboard (on ICS) there is a done button. I would like for the done button on the keyboard to trigger the submit button i have in my application. My code is as follows.

    package com.michaelpeerman.probability;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnCancelListener;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import java.util.Random;
    
    public class ProbabilityActivity extends Activity implements OnClickListener {
    
    private Button submit;
    ProgressDialog dialog;
    int increment;
    Thread background;
    int heads = 0;
    int tails = 0;
    
    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.main);
        submit = ((Button) findViewById(R.id.submit));
        submit.setOnClickListener(this);
    }
    
    public void onClick(View view) {
        increment = 1;
        dialog = new ProgressDialog(this);
        dialog.setCancelable(true);
        dialog.setMessage("Flipping Coin...");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setProgress(0);
        EditText max = (EditText) findViewById(R.id.number);
        int maximum = Integer.parseInt(max.getText().toString());
        dialog.setMax(maximum);
        dialog.show();
        dialog.setOnCancelListener(new OnCancelListener(){
    
              public void onCancel(DialogInterface dialog) {
    
                  background.interrupt();
                  TextView result = (TextView) findViewById(R.id.result);
                    result.setText("heads : " + heads + "\ntails : " + tails);
    
    
              }});
    
    
        background = new Thread(new Runnable() {
            public void run() {
                heads=0;
                tails=0;
                for (int j = 0; !Thread.interrupted() && j < dialog.getMax(); j++) {
                    int i = 1 + new Random().nextInt(2);
                    if (i == 1)
                        heads++;
                    if (i == 2)
                        tails++;
                    progressHandler.sendMessage(progressHandler.obtainMessage());
                }
            }
        });
        background.start();
    }
    
    Handler progressHandler = new Handler() {
        public void handleMessage(Message msg) {
    
            dialog.incrementProgressBy(increment);
            if (dialog.getProgress() == dialog.getMax()) {
                dialog.dismiss();
                TextView result = (TextView) findViewById(R.id.result);
                result.setText("heads : " + heads + "\ntails : " + tails);
    
    
            }
        }
    
    };
    
    }
    
  • Roman Black
    Roman Black about 12 years
    When you push any button your view generates event onKey, and when keyCode matched against the right button it does something you want
  • mpeerman
    mpeerman about 12 years
    How can i set it to trigger the on click i already have running off of the submit button public void onClick(View view) {
  • vladexologija
    vladexologija about 12 years
    You can either move all that code into single function and then call it or use performClick()
  • James
    James about 10 years
    This did not work for me when hitting a DONE button, the KeyEvent was always null.. the actionId however was set to EditorInfo.IME_ACTION_DONE and I was able to use that instead. (This is in android 4.2.2 and does not even match the android sdk documentation at this time)
  • Jeffrey Blattman
    Jeffrey Blattman almost 9 years
    button has to have focus
  • Matwosk
    Matwosk about 8 years
    This one works perfect for me. Be careful about return TRUE. Keyboard remains displayed. If you want to hide keyboard return FALSE.
  • jAC
    jAC almost 7 years
    Commenting on your code always improves the readability of an answer...
  • Dev-iL
    Dev-iL almost 7 years
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add an explanation, and give an indication of what limitations and assumptions apply. It also doesn't hurt to mention why this answer is more appropriate than others.
  • sam
    sam almost 5 years
    Works like gem!
  • gmk57
    gmk57 almost 3 years
    This listener in fact always returns false (Kotlin puzzler ;)), but it is fine: we typically want keyboard to close.
  • jj.
    jj. almost 3 years
    @gmk57 Try adding an else to that if statement and have it contain the false. I believe you are always seeing false because the if statement sets a true, but then it flows directly to the false on the next line (outside the if).