Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize Text Input Layout

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??

like image 370
Riddhi Daftary Avatar asked Nov 08 '17 05:11

Riddhi Daftary


People also ask

How can I customize the appearance of the textinputlayout?

You can customize the appearance of the TextInputLayout and its embedded EditText by defining custom styles in your styles.xml. The defined styles can either be added as styles or themes to your TextInputLayout.

Why use textinputedittext instead of edittext?

The TextInputEditText class is provided to be used as the input text child of this layout. Using TextInputEditText instead of an EditText provides accessibility support for the text field and allows TextInputLayout greater control over the visual aspects of the text field.

How to avoid unintended behavior when using textinputlayout on edittext?

To avoid unintended behavior, call setHint (CharSequence) and getHint () on TextInputLayout, instead of on EditText. If the EditText child is not a TextInputEditText, make sure to set the EditText 's android:background to null when using an outlined or filled text field.

How do I create a material text field in XML?

To create a material text field, add a TextInputLayout to your XML layout and a TextInputEditText as a direct child. Note: A TextInputEditText should be used instead of an EditText as your input text component.


1 Answers

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

like image 77
Francis Avatar answered Sep 21 '22 03:09

Francis