Android Use Done button on Keyboard to click button
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
}
};
mpeerman
Updated on July 09, 2020Comments
-
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); } } }; }
-
Nishant about 12 yearscheck this stackoverflow.com/questions/5330243/…
-
mpeerman about 12 yearsThat refers to the textbox updating.
-
mpeerman about 12 yearsWhat i want to do is for it too trigger a button i already have. I dont want to get rid of the button because some people may be using keyboards that dont have a done button.
-
Elltz about 9 yearspossible duplicate of Android detect Done key press for OnScreen Keyboard
-
-
Roman Black about 12 yearsWhen you push any button your view generates event onKey, and when keyCode matched against the right button it does something you want
-
mpeerman about 12 yearsHow can i set it to trigger the on click i already have running off of the submit button public void onClick(View view) {
-
vladexologija about 12 yearsYou can either move all that code into single function and then call it or use performClick()
-
James about 10 yearsThis 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 almost 9 yearsbutton has to have focus
-
Matwosk about 8 yearsThis one works perfect for me. Be careful about return TRUE. Keyboard remains displayed. If you want to hide keyboard return FALSE.
-
jAC almost 7 yearsCommenting on your code always improves the readability of an answer...
-
Dev-iL almost 7 yearsWhile 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 almost 5 yearsWorks like gem!
-
gmk57 almost 3 yearsThis listener in fact always returns
false
(Kotlin puzzler ;)), but it is fine: we typically want keyboard to close. -
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).