Listener for Done button on EditText and clear EditText when user not press Done button?
To check user pressed "Done" button of soft keyboard, use the code below:
edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if(i== EditorInfo.IME_ACTION_DONE){
Toast.makeText(getApplicationContext(),"Done pressed",Toast.LENGTH_SHORT).show();
}
return false;
}
});
To clear the text of edittext once focus has been changed, use the code below:
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus){
edittext.setText("");
}
}
});
How do I make an Android EditView 'Done' button and hide the keyboard when clicked?
Use TextView.setImeOptions and pass it actionDone.
like textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
Android imeOptions=actionDone not working
Just add android:inputType="..."
to your EditText. It will work!! :)
Use Done button on Keyboard in Databinding
I won't claim to be an expert in onEditorAction()
or soft keyboard. That said, assuming you use the solution to the stack overflow question Firoz Memon suggested, you can make it happen. Even if there is another solution that works better, this can give you an idea on how to add your own event handlers.
You'd need a binding adapter that takes some kind of handler. Let's assume you have an empty listener like this:
public class OnOkInSoftKeyboardListener {
void onOkInSoftKeyboard();
}
Then you need a BindingAdapter:
@BindingAdapter("onOkInSoftKeyboard") // I like it to match the listener method name
public static void setOnOkInSoftKeyboardListener(TextView view,
final OnOkInSoftKeyboardListener listener) {
if (listener == null) {
view.setOnEditorActionListener(null);
} else {
view.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public void onEditorAction(TextView v, int actionId, KeyEvent event) {
// ... solution to receiving event
if (somethingOrOther) {
listener.onOkInSoftKeyboard();
}
}
});
}
}
How to trigger a event when user press done button in keyboard
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do your stuff here
}
return false;
}
});
Implicit Submit after hitting Done on the keyboard at the last EditText
Try this:
In your layout put/edit this:
<EditText
android:id="@+id/search_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:singleLine="true"
android:imeOptions="actionDone" />
In your activity put this (e. g. in onCreate):
// your text box
EditText edit_txt = (EditText) findViewById(R.id.search_edit);
edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
submit_btn.performClick();
return true;
}
return false;
}
});
Where submit_btn
is your submit button with your onclick handler attached.
Related Topics
How to Set Versionname in APK Filename Using Gradle
Difference Between /Res and /Assets Directories
Android Command Line Tools Sdkmanager Always Shows: Warning: Could Not Create Settings
Create Custom Notification, Android
Is There Any Reason to Use the Support.V4 Library in Android
How to Pass Integer from One Activity to Another
Android Webview, How to Handle Redirects in App Instead of Opening a Browser
Making a Triangle Shape Using Xml Definitions
Facebook Key Hash Does Not Match Any Stored Key Hashes
Android - Local Image in Webview
Android Get Image from Gallery into Imageview
Sqliteopenhelper Problem with Fully Qualified Db Path Name
Android Listview Refresh Single Row
How to Turn Off 3G/Data Programmatically on Android