Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Force EditText to remove focus? [duplicate]

I would like to be able to remove the focus from the EditText. For example if the Keyboard appears, and the user hides it with the back button, I would like the focus and the cursor to disappear. How can it be done?

like image 533
Alex1987 Avatar asked Feb 20 '11 11:02

Alex1987


3 Answers

You can make cursor and focus disappear by

edittext.clearFocus();

But detect when the key board hide is a hard work.

like image 118
xtr Avatar answered Oct 18 '22 23:10

xtr


Add LinearLayout before EditText in your XML.

<LinearLayout 
    android:focusable="true"
    android:focusableInTouchMode="true" 
    android:clickable="true"
    android:layout_width="0px"
    android:layout_height="0px" />

Or you can do this same thing by adding these lines to view before your 'EditText'.

<Button
    android:id="@+id/btnSearch"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"
    android:text="Quick Search"
    android:textColor="#fff"
    android:textSize="13sp"
    android:textStyle="bold" />

<EditText
    android:id="@+id/edtSearch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dp"
    android:gravity="left"
    android:hint="Name"
    android:maxLines="1"
    android:singleLine="true"
    android:textColorHint="@color/blue"
    android:textSize="13sp"
    android:textStyle="bold" />
like image 45
Singh Arjun Avatar answered Oct 18 '22 23:10

Singh Arjun


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);
like image 21
Wayner Avatar answered Oct 18 '22 23:10

Wayner