How to Make an Android Editview 'Done' Button and Hide the Keyboard When Clicked

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

How to hide soft keyboard on android after clicking outside EditText?

The following snippet simply hides the keyboard:

public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
if(inputMethodManager.isAcceptingText()){
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(),
0
);
}
}

You can put this up in a utility class, or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this).

The trickiest part is when to call it. You can write a method that iterates through every View in your activity, and check if it is an instanceof EditText if it is not register a setOnTouchListener to that component and everything will fall in place. In case you are wondering how to do that, it is in fact quite simple. Here is what you do, you write a recursive method like the following, in fact you can use this to do anything, like setup custom typefaces etc... Here is the method

public void setupUI(View view) {

// Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(MyActivity.this);
return false;
}
});
}

//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}

That is all, just call this method after you setContentView in your activity. In case you are wondering what parameter you would pass, it is the id of the parent container. Assign an id to your parent container like

<RelativeLayoutPanel android:id="@+id/parent"> ... </RelativeLayout>

and call setupUI(findViewById(R.id.parent)), that is all.

If you want to use this effectively, you may create an extended Activity and put this method in, and make all other activities in your application extend this activity and call its setupUI() in the onCreate() method.

Hope it helps.

If you use more than 1 activity define common id to parent layout like
<RelativeLayout android:id="@+id/main_parent"> ... </RelativeLayout>

Then extend a class from Activity and define setupUI(findViewById(R.id.main_parent)) Within its OnResume() and extend this class instead of ``Activity in your program


Here is a Kotlin version of the above function:

@file:JvmName("KeyboardUtils")

fun Activity.hideSoftKeyboard() {
currentFocus?.let {
val inputMethodManager = ContextCompat.getSystemService(this, InputMethodManager::class.java)!!
inputMethodManager.hideSoftInputFromWindow(it.windowToken, 0)
}
}

Android:Hide keyboard after button click

You could instead set it to your layout, ie:

LinearLayout mainLayout;

// Get your layout set up, this is just an example
mainLayout = (LinearLayout)findViewById(R.id.myLinearLayout);

// Then just use the following:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainLayout.getWindowToken(), 0);

This is an example assuming that your layout will be created regardless of how many EditText objects (or other objects) are placed on it.

Edit: Also, something I find very useful is to make sure that the keyboard is hidden when an activity first launches (ie: if an EditText is the first thing focused). To do that, I put this in onCreate() method of Activity:

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

how to hide keyboard after typing in EditText in android?

This should work.

InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);

Just make sure that this.getCurrentFocus() does not return null, which it would if nothing has focus.

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 to hide the soft keyboard on OK on an EditText and show it again

I fixed my problem by doing:

   kip_time.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i("OK", "Enter pressed");
reflexion_time = Integer.parseInt(kip_time.getText().toString());
reflexion_time = reflexion_time * 1000;
hide_keyboard();
}
return false;
}
});

with the hide keyboard:

   private void hide_keyboard() {
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(kip_time.getWindowToken(), 0);
}

no need for a show_keyboard()

Hide Soft Keyboard on Done Keypress in Android?

Use android:imeOptions="actionDone", like that:

<EditText
...
android:imeOptions="actionDone" />

How can I get a done button in softkeyboard?

add this to your EditText xml:

android:imeOptions="actionDone"

or, to set it from code:

yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);

for more, read this

Android Use Done button on Keyboard to click button

You can use this one also (sets a special listener to be called when an action is performed on the EditText), it works both for DONE and RETURN:

max.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i(TAG,"Enter pressed");
}
return false;
}
});

Also, showing a nice send button in the keyboard itself would make it better. Add below line in EditText xml:

android:imeOptions="actionSend"
android:inputType="text"


Related Topics



Leave a reply



Submit