1

I am trying to create a custom design for EditText. See the attached image below,

enter image description here

I am trying to achieve this using a custom shape like the one defined below.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/places_ic_search"
        android:width="30dp">
        <shape
            android:shape="rectangle"></shape>
    </item>

    <item
        android:left="30dp"
        android:drawable="@color/white">
        <shape android:shape="rectangle" >
            <gradient android:startColor="#000000" android:endColor="#FFFFFF" android:angle="90"/>
        </shape>
    </item>

</layer-list>

But the search icon is not displayed. How can I achieve this?

Thanks.

I have applied the answer by tiny sunlight and the EditBox looks like this, Search icon is no aligned to the left.

enter image description here

Zach
  • 9,989
  • 19
  • 70
  • 107

3 Answers3

1

I edit my answer. Edit Shape.xml as below:

enter image description here

Shape.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle" >
            <stroke android:width="5dp" android:color="#FF0000" />
        </shape>
    </item>

    <item
        android:left="3dp"
        android:right="3dp"
        android:top="3dp"
        android:bottom="3dp"
        >
        <shape android:shape="rectangle" >
            <stroke android:width="1dp" android:color="#FF0000" />\
            <solid android:color="#FFFFFF" />
            <corners android:radius="3dp" />
        </shape>
    </item>

    <item android:gravity="left" android:left="10dp">
        <bitmap android:src="@android:drawable/ic_menu_search" android:gravity="left">

        </bitmap>
    </item>
</layer-list>
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
Kae10
  • 619
  • 2
  • 6
  • 17
0
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <gradient android:startColor="#FFFFFF" android:endColor="#FFFFFF" android:angle="90"/>
            <corners android:radius="3dp"></corners>
        </shape>
    </item>
    <item android:drawable="@drawable/places_ic_search"
          android:gravity="center_vertical|left"
          android:height="10dp"
          android:width="30dp">
    </item>
</layer-list>
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
  • Can you please check the edited answer, I have added the screenshot after applying your answer. The icon is not aligned properly. – Zach Jan 20 '16 at 02:13
0

I would do it like this :

  1. For the border part, you could do it with a drawable like you did
  2. For the search icon, instead of trying to put it in the drawable file, why not set it in you EditText xml layout, using android:drawableLeft, you can then set padding using android:paddingLeft and android:drawablePadding
Nguyen Quang Anh
  • 315
  • 1
  • 3
  • 12