I have an Activity
in Android, with two elements:
EditText
ListView
When my Activity
I needed to clear focus from all fields programmatically. I just added the following two statements to my main layout definition.
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.
This is the perfect and most easiest solution.I always use this in my app.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
( Reference from Silver https://stackoverflow.com/a/8639921/15695 )
and remove
<requestFocus />
at EditText
( Reference from floydaddict https://stackoverflow.com/a/9681809 )
Yeah I did the same thing - create a 'dummy' linear layout which gets initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it any more after scrolling once:
<LinearLayout 'dummy'>
<EditText et>
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
a lot of work just to get rid of focus on a view..
Thanks
Add android:windowSoftInputMode="stateAlwaysHidden"
in the activity tag of the Manifest.xml
file.
Source
At onCreate
of your Activity, just add use clearFocus()
on your EditText element.
For example,
edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();
And if you want to divert the focus to another element, use requestFocus()
on that.
For example,
button = (Button) findViewById(R.id.button);
button.requestFocus();