3

I have an EditText inside a TextInputLayout with +91 TextView prefix to EditText.

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_vertical_margin">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/layoutMobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/etPhoneNumber"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/txt_hint_phone"
                    android:imeOptions="actionDone"
                    android:inputType="phone"
                    android:maxLength="10"
                    android:maxLines="1"
                    android:minLines="1"
                    android:paddingLeft="30dp"
                    android:layout_marginBottom="10dp"
                    android:text="9736625345"
                    android:textColorHint="@color/colorBlack"
                    android:textSize="@dimen/sixteen_sp" />

            </android.support.design.widget.TextInputLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerInParent="true"
                android:text="+91"
                android:textSize="16sp" />
        </RelativeLayout>

But as android:paddingLeft="30dp" is given to EditText the hint and error message of TextInputLayout also get shifted!!

enter image description here

I want hint and error message of TextInputLayout to stay left align while EditText should have a non-editable prefix.

What is the correct way of implementing this?

Prithvi Bhola
  • 3,041
  • 1
  • 16
  • 32
  • You can use a [TextWatcher](https://stackoverflow.com/a/8543479/996493) for this – Lucifer Mar 13 '18 at 06:22
  • Does this answer your question? [Put constant text inside EditText which should be non-editable - Android](https://stackoverflow.com/questions/14195207/put-constant-text-inside-edittext-which-should-be-non-editable-android) – Mahozad Sep 23 '20 at 09:38

3 Answers3

2

If you don't want to give padding may be this can help you to adjust your view.

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="@dimen/activity_vertical_margin">

         <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+91"
            android:textSize="16sp" />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layoutMobile"
            android:layout_width="0dp"
            android:weight="1"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/etPhoneNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/txt_hint_phone"
                android:imeOptions="actionDone"
                android:inputType="phone"
                android:maxLength="10"
                android:maxLines="1"
                android:minLines="1"
                android:text="9736625345"
                android:textColorHint="@color/colorBlack"
                android:textSize="@dimen/sixteen_sp" />
        </android.support.design.widget.TextInputLayout>

For error indication use this java code

mEditTextObject.setError("Error!")//to show error
mEditTextObject.setError(null)//to Remove Error
Ashish
  • 997
  • 6
  • 20
0

Try this using java:-

   layoutMobile.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 (make condition here according to you) {
                        // set error here <layoutMobile.setError();>
                    }
                }

                public void afterTextChanged(Editable edt) {

                }
            });
Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
0

Try Setting the selection length for the edittext using the code below,

EditText.setSelection(EditText.getText().length());
Prateek
  • 3,923
  • 6
  • 41
  • 79
suganya
  • 21
  • 3