Huw Davies
Huw Davies

Reputation: 811

Changing EditText Underline when disabled

Let's say I have a edittext input for date of birth:

dob_enabled

I want to dynamically change its state to then be disabled, and for it to have a dotted underline to show that it's disabled:

dob_disabled

How would I go about doing that? It would be according to the google material design guidelines:

material_guidelines

Upvotes: 1

Views: 395

Answers (1)

Navneet Krishna
Navneet Krishna

Reputation: 5017

Create a drawable underline.xml in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<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="1dp"
            android:color="@android:color/black"
        android:dashGap="3.0dp"
        android:dashWidth="1dp" />
    </shape>
</item>

and then programatically apply this drawable and set it as disabled

editText=(EditText)findViewById(R.id.your_edittext);
editText.setEnabled(false);
editText.setBackgroundResource(R.drawable.underline);

Output :

enter image description here

Upvotes: 1

Related Questions