145

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?

1

17 Answers 17

190

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"
    />
7
  • @Michael removed that link. Thanks Oct 9, 2013 at 12:15
  • 25
    I had to add android:singleLine="true" to get this to work via xml
    – Jacksonkr
    Mar 28, 2016 at 16:07
  • 6
    android:singleLine is deprecated. Use android:maxLines="1". Jan 22, 2017 at 0:51
  • Yes @FariborZ singleLine is deprecated now. Jan 23, 2017 at 6:11
  • 1
    Important note: singleLine is not the same as maxLines. This misconception causes a lot of issues for everyone, we need to note that everywhere. stackoverflow.com/questions/30879471/…
    – milosmns
    Mar 8, 2017 at 15:47
160

Use TextView.setImeOptions and pass it actionDone. like textView.setImeOptions(EditorInfo.IME_ACTION_DONE);

3
  • 8
    textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
    – sulai
    Jun 2, 2014 at 14:07
  • 19
    I had to add textView.singleLine(true) to get this to work programmatically.
    – Jacksonkr
    Mar 28, 2016 at 16:06
  • but this is deprecated since android 2.3 Sep 15, 2017 at 13:09
88

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"
   />
39

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;
        }
    });

`

36
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");
2
  • 1
    Thanks, Demo was on 12 , time was 11:58 and you fixed my issue on 11:59 :p Apr 5, 2019 at 7:45
  • android:singleLine = "true" has been deprecated. So use, android:lines="1"
    – Gowtham. R
    Sep 17, 2019 at 13:22
15

Use this:

android:singleLine="true"
2
  • Yes...otherwise how is one going to distinguish going to the next line versus closing the keyboard? Jun 26, 2014 at 4:09
  • This is what ended up working for me. I decided to error with having actionDone in there as well, just in case some devices react differently.
    – Jacksonkr
    Mar 28, 2016 at 16:10
12

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);
11

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

0
7

Use:

android:imeActionLabel="Done"
android:singleLine="true" 
7

If the property does not change for the widget it may be better to use like android:imeOptions="actionDone" in the layout xml file.

6

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.

5

For the code:

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
5

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);
4

If you are using

android:imeOptions="actionDone" 

then you must use

android:inputType="text"

then only you can see Action Done button in Keyboard.

0
2

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.

0

use this in your view

<EditText 
    ....
    ....
    android:imeOptions="actionDone" 
    android:id="@+id/edtName"
    />
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.