When the user clicks on the EditView
, Android opens the keyboard so that user can write in the EditView
.
The problem is, when the user is done writing, there is no way to hide the keyboard. The user has to press the back button to hide the keyboard.
Is there a way to display a Done
button on the keyboard that will hide the keyboard?
First you need to set the android:imeOptions
attribute equal to actionDone
for your target EditText as seen below. That will change your ‘RETURN’ button in your EditText’s soft keyboard to a ‘DONE’ button.
<EditText
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter some text"
android:imeOptions="actionDone"
android:singleLine="true"
/>
Use TextView.setImeOptions and pass it actionDone. like textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
textView.singleLine(true)
to get this to work programmatically.
Include both imeOptions
and singleLine
:
<EditText
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
android:imeActionLabel="Done"
android:singleLine="true"
In the XML file works just fine. But this will also cause the editText
to keep typing in a single line which you may not want. So adding following to your code will make sure that you won't end up typing everything on a single line.
mainText.setHorizontallyScrolling(false);
mainText.setMaxLines("Maximum integer value that you want to provide");
For getting the done Button
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
and
android:inputType="text"
in the xml
For handling on done clicked from keyboard
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
if(actionId == EditorInfo.IME_ACTION_DONE){
// Your action on done
return true;
}
return false;
}
});
`
Use this:
android:singleLine="true"
actionDone
in there as well, just in case some devices react differently.
Use These two lines to your EditText
android:imeActionLabel="Done"
android:singleLine="true"
or you can achieve it Programmatically by this line.
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
Use:
android:imeActionLabel="Done"
android:singleLine="true"
If the property does not change for the widget it may be better to use like android:imeOptions="actionDone"
in the layout xml
file.
I have to point that out as a lot of people can struggle into that without knowing the problem.
If you want the kb to hide when clicking Done
, and you set android:imeOptions="actionDone"
& android:maxLines="1"
without setting your EditText inputType
it will NOT work as the default inputType
for the EditText is not "text"
as a lot of people think.
so, setting only inputType
will give you the results you desire whatever what you are setting it to like "text"
, "number"
, ...etc.
Kotlin Solution
The direct way to handle the hide keyboard + done action in Kotlin is:
// Set action
edittext.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Hide Keyboard
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
true
}
false
}
Kotlin Extension
Use this to call edittext.onDone {/*action*/}
in your main code. Keeps it more readable and maintainable
edittext.onDone { edittext.hideKeyboard() }
fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
fun EditText.onDone(callback: () -> Unit) {
// These lines optional if you don't want to set in Xml
imeOptions = EditorInfo.IME_ACTION_DONE
maxLines = 1
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
callback.invoke()
true
}
false
}
}
Additional Keyboard Extensions
If you'd like more ways to simplify working with the keyboard (show, close, focus): Read this post
Don't forget to add these options to your edittext Xml, if not in code
<EditText ...
android:imeOptions="actionDone"
android:inputType="text"/>
Need inputType="textMultiLine" support? Read this post and don't add imeOptions or inputType in Xml
For the code:
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
ActionDone is use when click in next button in the keyboard that time keyboard is hide.Use in Edit Text or AppcompatEdit
XML
1.1 If you use AppCompatEdittext
<android.support.v7.widget.AppCompatEditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"/>
1.2 If you use Edittext
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"/>
JAVA
EditText edittext= (EditText) findViewById(R.id.edittext);
edittext.setImeOptions(EditorInfo.IME_ACTION_DONE);
Actually you can set custom text to that little blue button. In the xml file just use
android:imeActionLabel="whatever"
on your EditText.
Or in the java file use
etEditText.setImeActionLabel("whatever", EditorInfo.IME_ACTION_DONE);
I arbitrarily choose IME_ACTION_DONE as an example of what should go in the second parameter for this function. A full list of these actions can be found here.
It should be noted that this will not cause text to appear on all keyboards on all devices. Some keyboards do not support text on that button (e.g. swiftkey). And some devices don't support it either. A good rule is, if you see text already on the button, this will change it to whatever you'd want.
If you are using
android:imeOptions="actionDone"
then you must use
android:inputType="text"
then only you can see Action Done button in Keyboard.
use this in your view
<EditText
....
....
android:imeOptions="actionDone"
android:id="@+id/edtName"
/>
If you don't want any button at all (e.g. you are developing a GUI for blind people where tap cannot be positional and you rely on single/double/long taps):
text.setItemOptions(EditorInfo.IME_ACTION_NONE)
Or in Kotlin:
text.imeOptions = EditorInfo.IME_ACTION_NONE
Success story sharing
android:singleLine="true"
to get this to work via xml