Search code examples
androidandroid-edittextandroid-spinnerandroid-xml

How to merge an edittext with spinner in Android?


EditText with Spinner/DropDown

How can I make an EditText with a drop-down in Android? For reference i have added an image. I am trying to combine an edittext with spinner but I am not able to achieve the same.

Can anyone add suggestion or xml mockup?


Solution

  • Well, You can use a LinearLayout with a horizontal orientation and create a custom drawable for the rounded border.

    Implementation

    First, you need to draw the rounded border by creating a new Drawable resource file.

    In your res/drawables folder create rounded_border.xml

    rounded_border.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <corners android:radius="4dp" />
                <stroke android:width="2dp" android:color="#2E2E2E" />
            </shape>
        </item>
    </selector>
    

    Then, you can add the LinearLayout to your layout as follows.

    your_layout.xml

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@drawable/rounded_border"
        android:orientation="horizontal">
    
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@null"
            android:paddingHorizontal="16dp"
            android:text="(504) 596-3245"
            android:layout_weight="2"/>
    
        <View
            android:layout_width="2dp"
            android:background="#2E2E2E"
            android:layout_height="match_parent"/>
    
        <Spinner
            android:layout_width="0dp"
            android:layout_height="match_parent"
            tools:listitem="@layout/spinner_item_text"
            android:layout_weight="1"/>
    
    </LinearLayout>
    

    Remarks

    • You can Customize the border width, color, and radius from the rounded_border.xml.

    Final Results

    Phone Layout