Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear focus for EditText?

I have an activity with a search box (EditText) on the top, and a ListView below. Whenever the activity starts, the EditText always has focus and bring up the keyboard which partially cover the ListView.

There's no other text view that can have focus. I want the EditText to have focus ONLY when the user touches it and start typing. I try to put clearFocus() in onCreateView, onViewCreated or onCreated, but no luck.

like image 256
EyeQ Tech Avatar asked Jan 20 '13 12:01

EyeQ Tech


People also ask

How do I clear focus in EditText?

Remove focus but remain focusable: editText. setFocusableInTouchMode(false); editText. setFocusable(false); editText.

How do I check my EditText focus?

You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus. This goes in your activity or fragment or wherever you have the EditTexts.


2 Answers

Set in your parent layout next attributes:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/mainLayout"     android:descendantFocusability="beforeDescendants"     android:focusableInTouchMode="true" > 

And now, when activity starts this layout getting default focus.

Also we can remove focus from children views in runtime (e.g. after finishing child editing):

findViewById(R.id.mainLayout).requestFocus(); 

or

Look in the AndroidManifest.xml element.

android:windowSoftInputMode="stateHidden" 

It always hide key board when entering the activity.

like image 136
Ajay S Avatar answered Sep 27 '22 18:09

Ajay S


You could use set focus on container view. Add to container android:focusableInTouchMode="true", android:focusable="true" and tag requestFocus example:

<RelativeLayout     android:id="@+id/container"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:focusableInTouchMode="true"     android:focusable="true">     <requestFocus/>      <EditText         android:id="@+id/edit_text_id"         android:layout_width="match_parent"         android:layout_height="wrap_content"/> </RelativeLayout> 
like image 29
Djek-Grif Avatar answered Sep 27 '22 18:09

Djek-Grif