Search code examples
androidandroid-edittextandroid-spinnerlayer-list

Edittext as spinner


I have a editext which I would like to show as drop down. SO for that I have background.xml file:

<?xml version="1.0" encoding="utf-8" ?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item>
    <layer-list>
      <item>
        <shape>
          <solid android:color="#ffffff" />
          <stroke android:width="1dp" android:color="#000000" />
          <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
        </shape>
      </item>
      <item>
        <bitmap android:gravity="right" android:src="@drawable/ic_arrow_drop_down" />
      </item>
    </layer-list>
  </item>

</selector>

And added that as background for my edittext. This is what I get: enter image description here

As you can see there is slight gap between the image and editext border. I would like to make my edittext like this: enter image description here

How can I achieve this?


Solution

  • Try this

      <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg"
            android:orientation="horizontal">
    
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@android:color/transparent" />
    
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true"
                android:background="@drawable/bgnew"
                android:src="@drawable/ic_android_black_24dp" />
        </LinearLayout>
    
    </LinearLayout>
    

    Create bg.xml like this

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- Background Color -->
        <solid android:color="#ffffff" />
    
        <!-- Border Color -->
        <stroke android:width="1dp" android:color="#ff9900" />
    
    
    </shape>
    

    create a bgnew.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- Background Color -->
        <solid android:color="#ffffff" />
    
        <!-- Border Color -->
        <stroke android:width="1dp" android:color="@color/colorAccent" />
    
    
    </shape>
    

    Output

    ![image