Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Material design style to disabled EditText (android)?

I'm trying to make my disabled EditText's style like in guidelines:

https://www.google.com/design/spec/components/text-fields.html#text-fields-labels

<EditText
        android:id="@+id/account_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/test_account"
        android:enabled="false" />

I've already implemented material design styles, so all my widgets look material.

The question is: Is there any way to get this dotted underline using styles, without putting additional views?

My styles.xml:

<style name="Theme.Main" parent="@style/Theme.AppCompat.Light.NoActionBar">         
    <item name="android:editTextStyle">@style/android:Widget.Material.EditText</item>
</style>
like image 478
Vasily Kabunov Avatar asked Feb 05 '16 08:02

Vasily Kabunov


People also ask

How do I remove the default padding in TextInputLayout?

You can just set the start and end padding on the inner EditText to 0dp. Here's a screenshot with Show Layout Bounds turned on so you can see that the hints go all the way to the edge of the view. Save this answer.

How do you set editable false for editText in android programmatically?

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


1 Answers

Finally I managed to do as you've shown, It looks like sample

Create dotted.xml inside drawable folder and paste these

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="0.5dp"
                android:color="@android:color/black" />    
            <solid android:color="#ffffff" />
            <stroke
                android:width="1dp"
                android:color="#030310"
                android:dashGap="5dp"
                android:dashWidth="5dp" />
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />
        </shape>
    </item>
</layer-list>

Then simply set the android:background attribute to dotted.xml we just created. Your EditText looks like this.

<EditText
    android:id="@+id/account_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="test account"
    android:background="@drawable/dotted"
    style="@style/Theme.Main" <!--Its your custom style-->
    android:enabled="false" />
like image 151
Shree Krishna Avatar answered Nov 15 '22 01:11

Shree Krishna