Search code examples
androidandroid-edittext

Custom EditText in Android with separated bottom lines


I need to make EditText with separated bottom lines looks like in the image below The EditText

I tried to put EditText and four ImageView into RelativeLayout.

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:gravity="center">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:gravity="center"
                    android:orientation="horizontal">


                    <EditText
                        android:id="@+id/et_code"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@null"                                   
                        android:singleLine="true"
                        android:textColor="@color/black"
                        android:textSize="16dp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:layout_height="1px"

                        android:alpha="0.3"
                        android:background="@color/black" />

                    <ImageView
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:layout_height="1px"
                        android:alpha="0.3"
                        android:background="@color/black" />

                    <ImageView
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:layout_height="1px"
                        android:alpha="0.3"
                        android:background="@color/black" />

                    <ImageView
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:layout_height="1px"
                        android:alpha="0.3"
                        android:background="@color/black" />

                </LinearLayout>

            </RelativeLayout>
        </RelativeLayout>

This is very bad solution because in small screens numbers begin to move out.


Solution

  • Use this library.

    As its GitHub page says:

    <com.github.glomadrian.codeinputlib.CodeInput
    android:layout_marginTop="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:underline_color="#457ad1"
    app:underline_selected_color="#9e1ace"
    app:text_color="#b12eff"
    app:hint_color="#77ce9d"
    app:hint_text="Pin code"
    app:codes="4"
    />
    

    Add in dependencies

    compile 'com.github.glomadrian:CodeInput:1.1@aar'