10

I want to have a Button or a clickable View in my EditText so that I can perform some action on click of it. I was able to put a drawable inside my EditText thanks to Marcosbeirigo for this answer. But, now I want to make it clickable so that I can perform some action on it. I know this is possible as HTC uses buttons inside EditText in their stock sms app as shown in the following image-

Send button in EditText

In my case, the button will be positioned anywhere, can be in the center also. But the main thing is how can I put a button in EditText?

Community
  • 1
  • 1
Rajkiran
  • 15,845
  • 24
  • 74
  • 114
  • 1
    You can make a [portion clickable](http://adilatwork.blogspot.com/2011/07/android-how-to-make-only-part-of.html) – Imran Rana May 31 '12 at 07:44

6 Answers6

11

Use RelativeLayout. The Send and Attach buttons are simple Android Buttons and the 32/160 is a TextView. Put the buttons and the textview on the EditText object, play with the layout arrangments and set some right padding to the EditText object so that the text inside it won't go under the buttons.

You don't need any drawable and listening to the click event of the android buttons is not a question anymore. Its absolutely correct

Swapnil
  • 3
  • 3
papaiatis
  • 4,231
  • 4
  • 26
  • 38
7

Use this , It worked for me -

 <RelativeLayout android:layout_width="match_parent"
     android:layout_height="wrap_content">

            <EditText android:id="@+id/id_search_EditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:paddingRight="40dp"
                android:hint="Enter your feelings and connect" />

        <ImageButton android:id="@+id/id_search_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/id_search_EditText"
            android:layout_alignBottom="@+id/id_search_EditText"
            android:layout_alignRight="@+id/id_search_EditText"
            android:background="@drawable/ic_launcher"/>

      </RelativeLayout>
Gauraw
  • 304
  • 2
  • 5
  • 13
2

I think you try to search set click event for compound drawable. You can find some solutions here

handling-click-events-on-a-drawable-within-an-edittext

Community
  • 1
  • 1
Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87
0

there is no button inside editText It's just an editText with white background so you don't see it's margins and a simple layout arrangement.

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
0

The only possible solution is to use a RelativeLayout that allow you to put also Views in overlay to others. Other Layout wont allow you to do such things.

Also I found this tutorial: http://developer.android.com/resources/articles/layout-tricks-merge.html maybe it can helps you :)

StErMi
  • 5,389
  • 5
  • 48
  • 71
0

output

Use this this code may work.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/REFReLayTellFriend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingTop="5dp">

        <EditText
            android:id="@+id/txtSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/editext_rounded"
            android:drawableLeft="@android:drawable/ic_menu_search"
            android:gravity="start"
            android:hint="Search"
            android:singleLine="true"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/txtSearch"
            android:background="@drawable/ic_action_content_filter_list"/>

    </RelativeLayout>

</LinearLayout>
Benoit Duffez
  • 11,839
  • 12
  • 77
  • 125
Ganesh Giri
  • 1,135
  • 11
  • 18