Android : clear focus on edittext when activity starts
Actually, the first focusable view in the activity receives initial focus. If that happens to be your EditText
, it will be initially focused.
If you don't want that, here's your options:
Focus another view
Programmatically identify what view you do want to give initial focus to.
@Override
protected void onStart() {
super.onStart();
findViewById( R.id.yourOtherViewId ).requestFocus();
}
Make an earlier view in your layout focusable
If you rather it appears as though "no view has initial focus" you could make the parent view group focusable. In the following example, I make my LinearLayout
focusable by setting android:focusableInTouchMode="true"
:
<LinearLayout
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
...
If Come back from other activity edittext get focused.
put these line onStart() or on onReusme()
RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
focuslayout.requestFocus();
If your EditText is a child of your RelativeLayout you can use android:descendantFocusability
to request the focus:
<RelativeLayout
android:id="@+id/RequestFocusLayout"
android:focusable="true"
android:layout_width="0px"
android:layout_height="0px"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" />