0

If I have an EditText that allows the user to enter only 6 digits. How can I create a line under each digit, with a small space between each one.

Like this

Other questions are addressing scenarios for a dashed line as a divider in a layout only.

Question 1

Question 2

Motassem Jalal
  • 1,254
  • 1
  • 22
  • 46

3 Answers3

6

First create a drawable file (myline.xml) which create the line.

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

<item
    android:bottom="1dp"
    android:left="-2dp"
    android:right="-2dp"
    android:top="-2dp">
    <shape android:shape="rectangle">
        <stroke
            android:width="1dp"
            android:color="@android:color/black"
            <!-- Change Gap & Width accroding to your need. -->
            android:dashGap="3.1dp"
            android:dashWidth="7dp" />
    </shape>
</item>

Now in your layout.xml use myline.xml in edit text background.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Digit: "
        android:textColor="#000"
        android:textSize="18dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/myline"
        android:lines="1"
        android:maxLength="6"
        android:text="123456"
        android:textColor="#000"
        android:textSize="18dp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Digit: "
        android:textColor="#000"
        android:textSize="18dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/myline"
        android:lines="1"
        android:maxLength="6"
        android:text="123"
        android:textColor="#000"
        android:textSize="18dp" />
</LinearLayout>

And its done see the output in screenshot below.

enter image description here

Andy Developer
  • 3,071
  • 1
  • 19
  • 39
  • Well this is good answer, but it will be too complicated because of sizes. I mean the same sizes you entered didn´t work for me. I believe it wont be the same for each device. – Motassem Jalal Jul 24 '17 at 09:46
3

Here is the one library that will resolve your problem.

https://android-arsenal.com/details/1/5999

Chirag
  • 56,621
  • 29
  • 151
  • 198
  • This library provides exactly what I need, but I have one more question regarding it. It is written that I should add a listener to get the text. Is it possible to get in another way like a normal editText (using getText() method)? – Motassem Jalal Jul 24 '17 at 09:47
  • It may possible but for that you need to check the library code which is on github. I have not checked the code. – Chirag Jul 24 '17 at 10:01
1

If EditText`s input is only number use this.

String zeroLeading = String.format("%06d", number)

This return zero leading with 6 lenth text. Then replace the zeros

zeroLeading = zeroLeading.replaceAll("0","_");

In some cases _ _ 2 0 4 5 like this. Bit different

String zeroLeading = String.format("%06d", number);
    int numberLen = (number+"").length();
    String underlineLeading = zeroLeading.substring(0, numberLen).replaceAll("0", "_")+zeroLeading.substring(numberLen, 6);
Fr099y
  • 734
  • 7
  • 15