How do I make an Android EditView 'Done' button and hide the keyboard when clicked?

AndroidKeyboard

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

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

`

Solution 6 - Android

Use this:

android:singleLine="true"

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

Use:

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

Solution 10 - Android

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.

Solution 11 - Android

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

Solution 12 - Android

For the code:

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

Solution 13 - Android

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


   

Solution 14 - Android

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.

Solution 15 - Android

If you are using

android:imeOptions="actionDone" 

then you must use

android:inputType="text"

then only you can see Action Done button in Keyboard.

Solution 16 - Android

use this in your view

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

Solution 17 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiond-manView Question on Stackoverflow
Solution 1 - AndroidParesh MayaniView Answer on Stackoverflow
Solution 2 - AndroiddmazzoniView Answer on Stackoverflow
Solution 3 - AndroidOded BreinerView Answer on Stackoverflow
Solution 4 - AndroidSantosh PillaiView Answer on Stackoverflow
Solution 5 - AndroidShangeeth SivanView Answer on Stackoverflow
Solution 6 - AndroidFrank NguyenView Answer on Stackoverflow
Solution 7 - AndroidNarenderNishadView Answer on Stackoverflow
Solution 8 - AndroidTomView Answer on Stackoverflow
Solution 9 - AndroidKiran MahaleView Answer on Stackoverflow
Solution 10 - AndroidMuhammed RefaatView Answer on Stackoverflow
Solution 11 - AndroidGiboltView Answer on Stackoverflow
Solution 12 - AndroidSteven.NguyenView Answer on Stackoverflow
Solution 13 - AndroidRajView Answer on Stackoverflow
Solution 14 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 15 - AndroidAditya PatilView Answer on Stackoverflow
Solution 16 - AndroidMarium JawedView Answer on Stackoverflow
Solution 17 - AndroidalbaspazioView Answer on Stackoverflow