10

I want to apply border to TextInputLayout as shown in image.

right now i have implemented like this.

enter image description here

but, I want as following as shown in image (i.e. label is placed within border).

enter image description here

The code i have implemented for EditText is as below.

<customviews.MyEditText
             android:id="@+id/email"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="@drawable/edittext_border_background"
             android:layout_marginEnd="30dp"
             android:layout_marginStart="30dp"
             android:layout_marginTop="45dp"
             android:gravity="start"
             android:hint="@string/hint_username"
             android:imeOptions="actionNext|actionDone"
             android:inputType="textEmailAddress|text"
             android:padding="10dp"
             android:textColor="@color/primary_text"
             android:textColorHint="@color/secondary_text"
             android:textSize="16sp"
            />

and, for border i have applied background editetext_border_background.xml is as follow

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="20dp">
    <solid android:color="#FFFFFF"/>
    <corners
        android:bottomRightRadius="0dp"
        android:bottomLeftRadius="0dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp"/>
    <stroke 
        android:width="1dip"
        android:color="@color/primary" />

</shape>

when I tried to apply border to TextInputLayout it doesn't give expected output.

enter image description here

Can anyone help me??

Riddhi Daftary
  • 301
  • 2
  • 14

4 Answers4

10

You should use Material Design style for Outline Box. Just simple use:

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

in TextInputLayout. See Text Field for Android in Material Design Guide

enter image description here

Francis
  • 5,871
  • 3
  • 42
  • 59
0

With "android:state_focused" option, you can archive your goal.

Details: https://stackoverflow.com/a/23417544/850347

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_enabled="true"
        android:state_focused="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        android:padding="20dp">
        <solid android:color="#FFFFFF"/>
        <corners
        android:bottomRightRadius="0dp"
        android:bottomLeftRadius="0dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp"/>
    <stroke 
        android:width="1dip"
        android:color="#0000FF" />

</shape>
</item>
<item android:state_enabled="true">
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        android:padding="20dp">
        <solid android:color="#FFFFFF"/>
        <corners
        android:bottomRightRadius="0dp"
        android:bottomLeftRadius="0dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp"/>
    <stroke 
        android:width="1dip"
        android:color="@color/primary" />

</shape>
</item>

And, in java code:

editText.setBackgroundResource(R.drawable.yourFile);

Stanley Ko
  • 2,930
  • 3
  • 31
  • 53
0

Change color and border as per your use and where ever in any of your views where you want to apply this border and it in background property like... android:background="@drawable/border"

Use this code to make drawable file with name border in android drawable folder in resource directory.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <solid android:color="#ffffff" />
    <stroke
       android:width="1dip"
       android:color="#000000" />
    <corners android:radius="4dip" />
    <padding
        android:bottom="0dip"
        android:left="0dip"
        android:right="0dip"
        android:top="0dip" />
</shape>
AYUSH ARYA
  • 113
  • 8
0
This will work 'hint' is inside in box .I  changed this property and add roundbox in background
  app:boxBackgroundMode="none" and app:boxCollapsedPaddingTop="5dp"
This is layout and change design according to your need.

<com.google.android.material.textfield.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="52dp"
                android:layout_marginTop="30dp"
                android:background="@drawable/round_box"
                app:boxBackgroundMode="none"
                app:boxCollapsedPaddingTop="5dp">
                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:gravity="center_vertical"
                    android:hint="hint"
                    android:inputType="textMultiLine"
                    android:maxLines="6"
                    android:paddingStart="5dp"
                    android:textSize="14dp" />
            </com.google.android.material.textfield.TextInputLayout>

round_box.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview_background_shape">
    <stroke android:width="2dp"
        android:color="#000000"
        />
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="4dp" />
    <solid android:color="#2C000000" />
</shape>

    enter code here

sorry for my bad english