24

I have an Activity with an EditText and a Button. When the User clicks on the EditText, the keyboard is shown and he can type in some Text - fine. But when the user clicks on the Button I want the EditText to be no more in focus i.e. the keyboard hides til the user clicks again on the EditText.

What can I do to 'hide the focus' of the EditText, after the Button is clicked. Some Code I can add in the OnClick Method of the Button to do that?

EDIT:

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/edt_SearchDest" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="0.8" android:textSize="18sp" android:hint="Enter your look-up here.." /> <Button android:id="@+id/btn_SearchDest" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="0.2" android:text="Search" /> </LinearLayout> 

Best Regards

2
  • clear the focus of Edittext on click of the button using m_editText.clearFocus(); Commented Aug 24, 2013 at 5:09
  • 2
    didn't solved the issue. Commented Aug 24, 2013 at 5:31

12 Answers 12

45

Put this in your button listener:

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

EDIT

The solution above will break your app if no EditText is focused on. Modify your code like this:

add this method to you class:

public static void hideSoftKeyboard (Activity activity, View view) { InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0); } 

Then, in your button listener, call the method like this:

hideSoftKeyboard(MainActivity.this, v); // MainActivity is the name of the class and v is the View parameter used in the button listener method onClick. 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. This is the only solution that has worked for me.
16

One workaround is to create a fake view to transfer focus to when you clearFocus in your edittext:

<EditText android:id="@+id/edt_thief" android:layout_width="0dp" android:layout_height="0dp" android:focusable="true" android:focusableInTouchMode="true" 

Note that this view is invisible so it doesn't require any space in the layout.

In the control class, you can add a method like the following to trigger this focus transfer:

public void clearFocus(){ yourEdittext.clearFocus(); edtThief.requestFocus(); } 

You can then minimize the keyboard once edtThief has focus:

 public static void hideKeyboard(final View view) { InputMethodManager imm = (InputMethodManager) view.getContext() .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } 

3 Comments

I had a similar issue and I've ended up with a solution similar to this, in my case I had to request focus on the hidden view first and then clear focus,
One other thing that's very important on the EditText layout dont forget the android:imeOptions="flagNoExtractUi" so when you rotate the phone you don't get the edit text edit screen extracted.
this worked for me, the above accepted solution did not. I have multiple edittexts that i need to make sure lose focus when a button is clicked, so in my method i clear focus on all edittext's, then i just use requestFocus() on the button that was clicked...this way you dont need to create an invisible view for no reason
10

I've successfully used the following in the onClick button code:

editText.setEnabled(false); editText.setEnabled(true); 

Somewhat less complex than other methods...

1 Comment

In this case you just hide the keyboard, but cursor will still be at the EditText. Moreover onFocusChangedListener will not be called.
8

The most elegant solution that I could find is this:

You can save this method in your utility class:

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

By simply calling hideSoftKeyboad() method it will hide the keyboard but as you can see, the focus will still be present.

In order to remove the focus we will use a simple trick. Right above your input controls, add a dummy view like this:

<View android:id="@+id/dummy" android:layout_width="1dp" android:layout_height="1dp" android:focusable="true" android:focusableInTouchMode="true" /> 

Then, write this line of code at the place where you call the focus-hiding method:

 theTextView.clearFocus(); 

Since the app needs to pass the focus to the next control it will be passed to our invisible view.

1 Comment

After trying lots of methods The bottom part of your answer solved the issue I was having. Thanks :) "Dummy View...Right above your input controls" specifically.
1

How i solved it.

// xml file <LinearLayout ... android:id="@+id/linear_layout" android:focusableInTouchMode="true"> // 1. make this focusableInTouchMode... </LinearLayout> // Activity file private LinearLayout mLinearLayout; // 2. parent layout element private Button mButton; mLinearLayout = findViewById(R.id.linear_layout); mButton = findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mLinearLayout.requestFocus(); // 3. request focus } }); 

I hope this helps you :)

Comments

1

The top answer definitely works but would add a lot of unnecessary codes in my use case, where there are many buttons and every one of them will need a setOnClickListener code block to remove focus from the EditText.

Instead, my approach is to write a BindingAdapter to perform both focus change and the intended click action.

BindingAdapter

@BindingAdapter("onClickWithFocusChange") fun View.setOnClickWithFocusChangeListener(clickListener: View.OnClickListener?) { clickListener?.also { setOnClickListener(OnClickWithFocusChangeListener(it)) } ?: setOnClickListener(null) } class OnClickWithFocusChangeListener( private val clickListener: View.OnClickListener ) : View.OnClickListener { override fun onClick(v: View?) { v?.requestFocusFromTouch() clickListener.onClick(v) v?.clearFocus() } } 

In xml (databinding can now be used instead of programmatically setting every one of the clicklisteners):

<!-- parentview of the EditText --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:focusableInTouchMode="true"> <ImageButton ... onClickWithFocusChange="@{() -> viewModel.buttonClicked()}" ... /> 

In activity/fragment:

editText.setOnFocusChangeListener { v, hasFocus -> if (!hasFocus) { requireContext().hideKeyboard(v) v.clearFocus() } } 

And lastly the extension function:

fun Context.hideKeyboard(view: View) { val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0) } 

Hope this helps some one!

Comments

1
<LinearLayout android:id="@+id/parent" android:focusableInTouchMode="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/edt" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="0.8" android:textSize="18sp" android:hint="Enter your look-up here.." /> <Button android:id="@+id/btn" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="0.2" android:text="Search" /> </LinearLayout> 
  1. set android:focusableInTouchMode="true" to the parent layout.
  2. on button click transfer the focus to parent.
 binding.btn.setOnClickListener { binding.parent.requestFocus() // your stuff here } 

Comments

0
private void hideDefaultKeyboard() { activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);//u have got lot of methods here } 

EDIT:

LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN 

6 Comments

I can't import a WindowManager..?
I would have suggested to just add this to the button listener method: getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); but that doesn't remove the keyboard. what I just said does work whenever I use it to remove focus when my app loads.
Somehow... WindowManager is red underlined and there is no Import suggestions.
The code works now, but unfortunetly does not hide the keyboard. It is still focused. I don't want the focus to be removed when the app loads first time. I want to remove it after a button is clicked.
call that method onclick of that button
|
0
mTextView.setEnabled(!mTextView.isEnabled()); mTextView.setEnabled(!mTextView.isEnabled()); 

Comments

0

Sorry late to the answer, but I hope this will be the right answer, as I fixed it using

try { InputMethodManager imm = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); if (v != null) { imm.hideSoftInputFromWindow(v.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); } } catch (Exception ignored) { } mEditText.setSelected(false); mEditText.setFocusable(false); mEditText.setFocusableInTouchMode(true); 

Write the following snippet on your button click.

Comments

0

From a personal experience, one may not need more than a simple clear focus to get this done, but one thing that could be tricky is when your clear focus is placed ahead of the workloads in the onClick method of your button (I experienced this for a button that has lot of checks, processes and a network call). Here is the simple workaround:

 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // start of button workload, network call, etc. process ... ... ... // end of process // clear the EditText focus here editText.clearFocus(); } }); 

Comments

-1

Why not just disable the EditText in the Button code? That should get rid of the keyboard and the focus.

edt_SearchDest.setEnabled(false);

1 Comment

then you can't focus on it ever; even when you touch it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.