Search code examples
androidandroid-layoutandroid-edittextandroid-xmlandroid-textinputedittext

TextInputEditText fix character length with custom design underline


I want to design like below :

Before enter user input :

enter image description here

User input numbers

enter image description here

Is it possible to extend TextInputEditText to achieve this request?

I have been looking for the design discussion,

how can i underline each character in edittext?

Can I underline text in an android layout?

But it seems not quite match what I want.

First I thought that I would use the letterSpacing, but the underline has the problem. Also for different screen resolution and font size, the letterSpacing can be the problem, it cannot fit into one line. I need to make sure it would be in one line.

Any suggestion to achieve that? If had better way, i would like to avoid using four edit text to achieve, is it possible to modify in one editText?


Solution

  • try this my friend

    create one xml layout file like this

                    <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="30dp"
                    android:layout_marginLeft="60dp"
                    android:layout_marginRight="60dp"
                    android:layout_marginTop="20dp"
                    android:orientation="horizontal"
                    android:weightSum="4">
    
                    <EditText
                        android:id="@+id/edDigit1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:hint="1"
                        android:imeOptions="actionNext"
                        android:inputType="number"
                        android:maxLength="1" />
    
                    <EditText
                        android:id="@+id/edDigit2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:hint="2"
                        android:imeOptions="actionNext"
                        android:inputType="number"
                        android:maxLength="1" />
    
                    <EditText
                        android:id="@+id/edDigit3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:hint="3"
                        android:imeOptions="actionNext"
                        android:inputType="number"
                        android:maxLength="1" />
    
                    <EditText
                        android:id="@+id/edDigit4"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:hint="4"
                        android:imeOptions="actionNext"
                        android:inputType="number"
                        android:maxLength="1" />
    
                </LinearLayout>
    
    **now in your activity file**
    
          EditText edDigit1, edDigit2, edDigit3, edDigit4;
          edDigit1 = (EditText) findViewById(R.id.edDigit1);
          edDigit2 = (EditText) findViewById(R.id.edDigit2);
          edDigit3 = (EditText) findViewById(R.id.edDigit3);
          edDigit4 = (EditText) findViewById(R.id.edDigit4);
    
         edDigit1.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (edDigit1.getText().toString().equals("")) {
                        edDigit1.requestFocus();
                    } else {
                        edDigit2.requestFocus();
                    }
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            });
            edDigit2.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (edDigit2.getText().toString().equals("")) {
                        edDigit2.requestFocus();
                    } else {
                        edDigit3.requestFocus();
                    }
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            });
            edDigit3.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (edDigit3.getText().toString().equals("")) {
                        edDigit3.requestFocus();
                    } else {
                        edDigit4.requestFocus();
                    }
    
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            });