How to place button inside of edit text

Arshad Ali picture Arshad Ali · Oct 31, 2012 · Viewed 61.1k times · Source

I want to place an image button inside of EditText, but I don't have Idea please tell me how to do so as shown in the figure . Thanks

enter image description here

Answer

Niranj Patel picture Niranj Patel · Oct 31, 2012

If You dont want click on that Image, Then you can use drawableRight property for EditText..

android:drawableRight="@drawable/icon"

If you want click then use below code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter search key" />

    <ImageButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/search"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:text="Button"/>

</RelativeLayout>