androidandroid-textinputlayoutmobile-country-code

Customise TextInputEditText with a CountryCode Picker


Hi I want to make edit text look like this enter image description here

I have tried adding country picker in textinputlayout, but it never draws view as shown in layout editor

 <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_login"
            android:layout_marginTop="@dimen/x60"
            android:id="@+id/phone"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_centerHorizontal="true"
            android:orientation="vertical">
            <com.hbb20.CountryCodePicker
                android:layout_width="@dimen/x40"
                android:layout_height="wrap_content"
                app:ccp_defaultNameCode="US"
                app:ccp_defaultLanguage="ENGLISH"
                app:ccp_arrowColor="@color/colorPrimary"
                app:ccp_textGravity="RIGHT"/>
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:hint="Phone Number"
                android:drawablePadding="@dimen/x16"
                android:inputType="phone"
                android:id="@+id/et_phone"
                android:drawableStart="@drawable/ic_phone_outline"
                android:layout_height="wrap_content"/>
        </com.google.android.material.textfield.TextInputLayout>

Country picker goes below the textinputlayout always, even I set orientation to horizontal


Solution

  • First Need to create a background

    1- Create a drawable resource File and name it whatever you want

     <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"   >
        <solid
            android:color="#F8F8F8" >
        </solid>
    
        <corners
            android:radius="10dp"   >
        </corners>
    
        <stroke android:width="1dp" android:color="#C2C2C3"/>
    
    </shape>
    

    2- Now Create Download Pakistan's Flag Image from Here and name it flag_pk

    3- Import Vector Asset call Icon and name it to tmep_call

    4- Now copy-paste the code given below into the layout where you need your editText

    Now comes the real part

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/temp_background">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:orientation="horizontal">
    
            <ImageView
                android:id="@+id/iconPhone"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:src="@drawable/temp_call" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="10dp"
                android:orientation="vertical">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/comfortaa_bold"
                    android:text="Phone Number"
                    android:textColor="@android:color/black"
                    android:textStyle="bold" />
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
    
                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="start"
                        android:src="@mipmap/flag_pk" />
    
                    <TextView
                        android:layout_gravity="center_vertical"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:fontFamily="@font/comfortaa_bold"
                        android:gravity="center_horizontal"
                        android:text="+92"
                        android:textColor="@android:color/black" />
    
                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:layout_marginStart="5dp"
                        android:background="@android:color/darker_gray" />
    
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginStart="5dp"
                        android:background="@null"
                        android:fontFamily="@font/comfortaa_bold"
                        android:hint="12345678900"
                        android:textColor="@android:color/black"
                        android:textColorHint="@android:color/black" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </FrameLayout>
    

    END result will look like this

    5: NOTE:: YOU NEED TO REPLACE THE TEXT "+92" WITH YOUR CODE PICKER"

    enter image description here