Prithvi Bhola
Prithvi Bhola

Reputation: 3151

EditText inside TextInputLayout with prefix

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?

Upvotes: 3

Views: 2822

Answers (3)

suganya
suganya

Reputation: 21

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

EditText.setSelection(EditText.getText().length());

Upvotes: 0

Ashish
Ashish

Reputation: 1025

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

Upvotes: 2

Abhinav Gupta
Abhinav Gupta

Reputation: 2265

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) {

                }
            });

Upvotes: 0

Related Questions