Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear edittext focus and hide keyboard when button is pressed

I'm making an app with edittext and a button. When I enter something into edittext and then click a button, I want the keyboard and focus on edittext to disappear but I can't seem to do it.

I inserted these 2 lines of code in XML:

android:focusable="true"
android:focusableInTouchMode="true"

I also tried to add this in the button click method:

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

And it just won't work, after I press the button, the keyboard remains there and edittext still has focus.

like image 749
Guy Avatar asked Jul 05 '13 14:07

Guy


People also ask

How do I clear focus in editText?

Remove focus but remain focusable: editText. setFocusableInTouchMode(false); editText. setFocusable(false); editText.

How do I hide the keyboard on my Android?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.

How do I turn off focus on Android?

On Android's Digital Wellbeing screen, tap Focus Mode. Choose the Set a Schedule option to block out the timeout you need from your phone. In the “Your distracting apps” list, select the programs you'd like to disable when Focus Mode is on.


1 Answers

To hide the keyboard call this:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

You can also check this solution: https://stackoverflow.com/a/15587937/786337

like image 90
Tarun Avatar answered Sep 28 '22 22:09

Tarun