Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : clear focus on edittext when activity starts

I've some settings page in my app. Once the activity gets starts directly it focus to edittext and i used following code to clear foucs.

<RelativeLayout 
     android:id="@+id/RequestFocusLayout"
     android:focusable="true"
     android:focusableInTouchMode="true"
     android:layout_width="0px"
     android:layout_height="0px"/>

and in java code

 RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
 focuslayout.requestFocus();

The above code is working fine when activity starts at first time and if same activity starts again, automatically edittext get focus.

Can anyone help me to solve this issue.

like image 290
Maddy Avatar asked Jun 22 '12 13:06

Maddy


2 Answers

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
        ...
like image 129
lhunath Avatar answered Nov 15 '22 08:11

lhunath


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();
like image 30
Dheeresh Singh Avatar answered Nov 15 '22 09:11

Dheeresh Singh