Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable EditText editability and focus (like TextView)

Is there a way to make EditText behaviors like TextView in Android (XML is prefered)? I have tried the following:

 android:editable="false"
 android:focusable="false"
 android:focusableInTouchMode="false"
 android:cursorVisible="false"
 android:longClickable="false"

This works but I still can touch the EditText to get focus (the orange boarder), though the focus lost as soon as I remove my finger. I'm not sure what focusableInTouchMode does, but it doesn't remove the focus when I keep touching.

And the reason why I don't use a white background TextView instead is that the TextView's background is ugly. EditText's background has round corners and shadow effect.

Thanks in advance.

like image 668
Chris Avatar asked Mar 19 '11 01:03

Chris


People also ask

What is the difference between EditText and TextView?

EditText is used for user input. TextView is used to display text and is not editable by the user. TextView can be updated programatically at any time.

How do you make EditText Uneditable?

In your xml code set focusable="false" , android:clickable="false" and android:cursorVisible="false" and this will make your EditText treat like non editable.

Which of the following is the correct code for disabling an EditText?

To disable an EditText while keeping this properties, just use UI. setReadOnly(myEditText, true) from this library.


1 Answers

EditText and TextView are quite similar. The only difference I see hard-coded into EditText.java is to set the editable default to true, which you've set manually. Other than that, the EditText style is:

<style name="Widget.EditText">
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:clickable">true</item>
    <item name="android:background">@android:drawable/edit_text</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
    <item name="android:textColor">@android:color/primary_text_light</item>
    <item name="android:gravity">center_vertical</item>
</style>

and TextView is:

<style name="Widget.TextView">
    <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>

My guess is that @android:drawable/edit_text is the source of the orange box. Indeed, it contains:

<item android:state_pressed="true" android:drawable="@drawable/textfield_pressed"/>

The simplest way might be to set its background to the default one:

android:background="@android:drawable/textfield_default"

like image 70
Matthew Willis Avatar answered Oct 30 '22 04:10

Matthew Willis