In my application I have a single EditText
together with some TextViews
, button and a spinner. My EditText receives focus since it is the only focu
Since I was in a widget and not in an activity I did:
`getRootView().clearFocus();
You only have to set the ViewGroup with the attribute:
android:focusableInTouchMode="true"
The ViewGroup is the layout that includes every child view.
<EditText android:layout_height="wrap_content" android:background="@android:color/transparent" android:layout_width="match_parent"
android:clickable="false"
android:focusable="false"
android:textSize="40dp"
android:textAlignment="center"
android:textStyle="bold"
android:textAppearance="@style/Base.Theme.AppCompat.Light.DarkActionBar"
android:text="AVIATORS"/>
If I understand your question correctly, this should help you:
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1 .setFocusable(false);
if Edittext
parent layout is Linear
then add
android:focusable="true"
android:focusableInTouchMode="true"
like below
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText/>
............
when Edittext parent layout is Relative then
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
like
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<EditText/>
............
I've tryed much to clear focus of an edit text. clearfocus() and focusable and other things never worked for me. So I came up with the idea of letting a fake edittext gain focus:
<LinearLayout
...
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
...
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--here comes your stuff-->
</LinearLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fake"
android:textSize="1sp"/>
</LinearLayout>
then in your java code:
View view = Activity.this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
fake.requestFocus();
}
it will hide the keyboard and remove the focus of any edittext that has it. and also as you see the fake edittext is out of screen and can't be seen