Search code examples
androidmaterial-components-androidandroid-textinputlayoutmaterial-components

How to align text in TextInputLayout in android?


In a RelativeLayout I have a TextInputLayout where I have EditText inside it to type phone number and a Textview inside a LinearLayout where I am showing the country code. When we start typing `TextInputLayout slide to top, but I want it to slide to as in image 3.

I am facing issue in this can somebody help me? This is my code:

<RelativeLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                >
        
                                <LinearLayout
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_alignStart="@+id/error_mobile_no"
                                    android:layout_alignTop="@+id/error_mobile_no"
                                    android:layout_alignBottom="@+id/error_mobile_no"
                                    android:layout_marginTop="0dp"
                                    android:layout_marginBottom="0dp"
                                    android:gravity="center"
                                    android:orientation="horizontal">
        
                                    <TextView
                                        android:id="@+id/country_code_textview"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:fontFamily="@font/lorin_regular"
                                        android:text="+971"
                                        android:layout_marginBottom="2dp"
                                        android:textColor="@color/black"
                                        android:textSize="14sp" />
                                </LinearLayout>
        
                                <com.google.android.material.textfield.TextInputLayout
                                    android:id="@+id/error_mobile_no"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginEnd="20dp"
                                    android:layout_marginStart="20dp"
                                    app:errorEnabled="true">
        
                                    <EditText
                                        android:id="@+id/etPhoneNumberDetailedActivity"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_gravity="start"
                                        android:autofillHints=""
                                        android:paddingStart="35dp"
                                        android:backgroundTint="#6283a3"
                                        android:fontFamily="@font/lorin_regular"
                                        android:hint="@string/mobile_number"
                                        android:inputType="phone"
                                        android:textColor="@color/black"
                                        android:textColorHint="#6283a3" android:textSize="14sp" />
                                </com.google.android.material.textfield.TextInputLayout>
                            </RelativeLayout>

This is an image of the field before entering text.enter image description here

This is the image after entering text.enter image description here

But I am trying to get it to look like this. enter image description here


Solution

  • You can use the app:prefixText to display the prefix:

        <com.google.android.material.textfield.TextInputLayout
            android:hint="mobile_number"
            app:prefixText="+971"
            ...>
    
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:autofillHints=""
                android:inputType="phone"
                .../>
    
        </com.google.android.material.textfield.TextInputLayout>
    

    enter image description here