Android: Force Edittext to Remove Focus

Android: Force EditText to remove focus?

You can add this to onCreate and it will hide the keyboard every time the Activity starts.

You can also programmatically change the focus to another item.

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

android edittext remove focus after clicking a button

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.

How to remove focus from single editText

check this question and the selected answer: Stop EditText from gaining focus at Activity startup It's ugly but it works, and as far as I know there's no better solution.

Force Edit text to remove focus after submitting list in recyclerview

Kindly replace your notifyDataSetChanged() with the notifyItemChanged(positionOfYourList).

How to stop EditText from gaining focus when an activity starts in Android?

Adding the tags android:focusableInTouchMode="true" and android:focusable="true" to the parent layout (e.g. LinearLayout or ConstraintLayout) like in the following example, will fix the problem.

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>

how can full clear focus in editText , just like press button DONE on softkeyboard

Firstly, we can use cleareFocus method to remove the focus.

editTextView.clearFocus()

But when this method is called, as source code comment says:

When not in touch-mode, the framework will try to give focus to the first focusable View from the top after focus is cleared. Hence, if this
View is the first from the top that can take focus, then all callbacks
related to clearing focus will be invoked after which the framework will
give focus to this view.

so after you call it, the first element will stay get the focus. so we should set the touch-mode true in parent layout.

<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<EditText android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

EditText, clear focus on touch outside

I tried all these solutions. edc598's was the closest to working, but touch events did not trigger on other Views contained in the layout. In case anyone needs this behavior, this is what I ended up doing:

I created an (invisible) FrameLayout called touchInterceptor as the last View in the layout so that it overlays everything (edit: you also have to use a RelativeLayout as the parent layout and give the touchInterceptor fill_parent attributes). Then I used it to intercept touches and determine if the touch was on top of the EditText or not:

FrameLayout touchInterceptor = (FrameLayout)findViewById(R.id.touchInterceptor);
touchInterceptor.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (mEditText.isFocused()) {
Rect outRect = new Rect();
mEditText.getGlobalVisibleRect(outRect);
if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
mEditText.clearFocus();
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return false;
}
});

Return false to let the touch handling fall through.

It's hacky, but it's the only thing that worked for me.



Related Topics



Leave a reply



Submit