1

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Huw Davies
  • 791
  • 7
  • 19
  • https://stackoverflow.com/questions/35219281/how-to-set-material-design-style-to-disabled-edittext-android can help you – Jyot Apr 17 '18 at 06:24
  • 1
    Possible duplicate of [How to set Material design style to disabled EditText (android)?](https://stackoverflow.com/questions/35219281/how-to-set-material-design-style-to-disabled-edittext-android) – AskNilesh Apr 17 '18 at 06:27
  • well found, but I can't believe there isn't a simpler way ! – Huw Davies Apr 17 '18 at 06:33

1 Answers1

1

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

Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44