Share via


How to disable auto focus on Edit text ?

Question

Sunday, March 3, 2013 11:29 AM

Hello guys,

When i launch my application, the first edit text is automatically selected and the keyboard appears:

http://www.imagup.com/data/1176975519.html

I want to disable this. I already unchecked "Focusable" in Behavior property but when i click on my textbox, i can't edit it.

Someone can help me ?

Best Regards, Michel. Ps: Sorry for my bad English, i'm 17 years old and i'm French.

All replies (6)

Sunday, March 3, 2013 7:54 PM

This is quite easily obtainable as an answer to a similar question on Stack Overflow suggests.

In the layout where you have your two EditText elements you can do:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/rootLayout"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true">
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="IP"
        android:id="@+id/editTextIp" />
    <EditText
        android:inputType="number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Port"
        android:id="@+id/editTextPort" />
    <Button
        android:text="Connexion"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/buttonConnect" />
</LinearLayout>

Here you should notice the two lines:

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

This makes the root layout focusable and gain focus before the EditText elements gain focus. This way you don't disturb the focus handlers on the EditText elements, and focusing them should bring up the keyboard as intended.

This can also be done by code leaving the two lines out, but keeping the android:id attribute, such that you can find it in your activity, and call RequestFocus() on it like so:

var root = FindViewById<LinearLayout>(Resource.Id.rootLayout);
root.RequestFocus();

Monday, March 4, 2013 5:39 AM

Thank you very much, it's working fine ! I used the method with the xml file.


Wednesday, November 5, 2014 5:09 PM

is it possible to do it programmatically instead of writing it in the axml file. is, then, any difference? Thank you.


Wednesday, November 5, 2014 5:32 PM

Yes.


Thursday, July 14, 2016 3:04 AM

Hi

I get solution for disable editbox in xamarin android using following code:

editbox edit =findbyId(resource.id.edit) edit.focusable =false;

Regards, N.Y.


Thursday, May 16, 2019 6:00 AM

Works perfectly! Thanks Cheesebaron