Reputation: 58063
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?
Upvotes: 145
Views: 161637
Reputation: 128428
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"
/>
Upvotes: 190
Reputation: 181
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
Upvotes: 0
Reputation: 1486
If you are using
android:imeOptions="actionDone"
then you must use
android:inputType="text"
then only you can see Action Done button in Keyboard.
Upvotes: 4
Reputation: 419
use this in your view
<EditText
....
....
android:imeOptions="actionDone"
android:id="@+id/edtName"
/>
Upvotes: 0
Reputation: 47097
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
}
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
}
}
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 addimeOptions
orinputType
in Xml
Upvotes: 11
Reputation: 2320
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;
}
});
`
Upvotes: 39
Reputation: 8633
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");
Upvotes: 36
Reputation: 9103
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.
Upvotes: 6
Reputation: 477
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);
Upvotes: 5
Reputation: 71
If the property does not change for the widget it may be better to use like
android:imeOptions="actionDone"
in the layout xml
file.
Upvotes: 7
Reputation: 29739
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"
/>
Upvotes: 88
Reputation: 1068
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);
Upvotes: 12
Reputation: 13236
Use TextView.setImeOptions and pass it actionDone.
like textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
Upvotes: 160
Reputation: 1054
For the code:
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
Upvotes: 5
Reputation: 34360
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.
Upvotes: 2