1

What is happening: When i click on inputName line is appearing on it

What I am trying: To display the line regardless focus is there or not

Code:

<android.support.design.widget.TextInputEditText
            android:id="@+id/inputName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:singleLine="true"
            android:textColor="@color/warm_grey"
            android:hint="Enter Mobile Number"
            android:layout_weight="1"/>

Image:

enter image description here

I just want to have two colours for focused and unfocused states

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Devrath
  • 42,072
  • 54
  • 195
  • 297

3 Answers3

3

You need to use StateListDrawable for this

  • A StateListDrawable is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the state of the object.

SAMPLE CODE

Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Hint Text">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/edt_bg"
            android:visibility="visible" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Hint Text">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/edt_bg"
            android:visibility="visible" />

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

@drawable/edt_bg

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/edt_bg_selected" android:state_focused="true" />
    <item android:drawable="@drawable/edt_bg_normal" android:state_focused="false" />
</selector>

@drawable/edt_bg_selected

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="1dp"
        android:left="-3dp"
        android:right="-3dp"
        android:top="-3dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="2dp"
                android:color="#0fe4e4" />

            <solid android:color="#00ffffff" />

        </shape>
    </item>
</layer-list>

@drawable/edt_bg_normal

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="1dp"
        android:left="-3dp"
        android:right="-3dp"
        android:top="-3dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="2dp"
                android:color="#000" />

            <solid android:color="#00ffffff" />

        </shape>
    </item>
</layer-list>

OUTPUT

enter image description here

NOTE

Use android:width="2dp" and android:color="#000" as per your requirement to set Thickness width and color of line in your TextInputEditText

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

This little function may help you

fun hintRemover(textInputLayout: TextInputLayout, textInputEditText: TextInputEditText, text: String) {
    textInputEditText.setOnFocusChangeListener { _, hasFocus ->
        if (hasFocus) {
            textInputLayout.hint = null
        }
        else {
            textInputLayout.hint = text
        }
    }
}
-2

Try this properties in your AppTheme or just create AnotherTheme and use in your EditText.

<item name="colorControlNormal">Color code for default</item>
<item name="colorControlActivated">Color code for focused one</item>
<item name="colorControlHighlight">Color code for focused one</item>

Possibly Duplicate Changing EditText bottom line color with appcompat v7

Note:- If you are using the AppCompat v22 support library, you can specify the theme in the EditText like: android:theme="@style/Theme.App.Base. This will ensure the style won't also affect other views in your layouts that you don't want to change.

sushildlh
  • 8,986
  • 4
  • 33
  • 77