5

I'm trying to create something like this in my app(taken from google docs):

enter image description here

enter image description here

Now, I tried to create a TextInputLayout element and try to put border around it but I can't manage to get this to look like the images I posted.

Here is my code for the TextInputLayout:

<android.support.design.widget.TextInputLayout
        android:id="@+id/shipper_layout"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/text_layout_stroke_normal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp">

        <EditText
            android:id="@+id/shipper_field"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="15dp"
            android:background="@null"
            android:gravity="top"
            android:hint="@string/shipper_field"
            android:inputType="textMultiLine"
            android:overScrollMode="always"
            android:scrollbarStyle="insideInset"
            android:scrollbars="vertical" />
    </android.support.design.widget.TextInputLayout>

Here is how it looks: (Before focused)

enter image description here

enter image description here

You can see the border isn't change as expected and the hint is just minimized

David Lasry
  • 829
  • 3
  • 12
  • 31

5 Answers5

7

Try this code:

in Xml:

 <android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/shipper_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <EditText
        android:id="@+id/shipper_field"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@drawable/text_layout_stroke_normal"
        android:gravity="top"
        android:hint="HELLO"
        android:inputType="textMultiLine"
        android:overScrollMode="always"
        android:padding="15dp"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical" />
</android.support.design.widget.TextInputLayout>

Now in code programetically change border and hint color of Edittext using setOnFocusChangeListener

 edittext.setOnFocusChangeListener(new OnFocusChangeListener() {          
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) {
                GradientDrawable drawable = (GradientDrawable)edittext.getBackground();
                drawable.setStroke(2, Color.RED);
                edittext.setHintTextColor(Color.RED);
            }
        }
    });
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
5

try this:

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:hint="textArea">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:inputType="textMultiLine"
        android:gravity="top"
        android:lines="4"
        android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>

make sure you are using newest material library:

implementation 'com.google.android.material:material:1.2.1'
Masoud Mohammadi
  • 1,721
  • 1
  • 23
  • 41
2

Try this

Create an xml file named custom_borders.xml in drawable folder and paste the below code

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <stroke android:color="#ff0000" android:width="2dp"></stroke>
    <corners android:radius="5dp"></corners>

</shape>

and apply it to your TextInputLayout like this

android:background="@drawable/custom_borders"
AbhayBohra
  • 2,047
  • 24
  • 36
2

Try this

add this line to TextInputLayout

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

android has already define styles.

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
R. Pradhan
  • 21
  • 2
1

I guess your question is how to change the background depending on it's state. You can simply edit your current text_layout_stroke_normal.xml in your drawable folder. Put this code into that file, and it will change depending on the focused state:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="false">
        <shape>
            <stroke android:width="1dp" android:color="@android:color/secondary_text_light" />
            <corners android:radius="5dp" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <stroke android:width="2dp" android:color="@color/colorAccent" />
            <corners android:radius="5dp" />
        </shape>
    </item>
</selector>
Alex
  • 1,650
  • 16
  • 15