-5

How can i create a text field with a button at the left side like whatsapp's smiley button and i also want my text to start after the button. enter image description here

3 Answers3

1

Set icon by drawableLeft of EditText and to perform click on that icon write down below code.

 editText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getRawX() <= (back.getCompoundDrawables()[0].getBounds().width())) {
                    // Your Code
                    return true;
                }
                return false;
            }
        });
Rajesh Tiwari
  • 410
  • 5
  • 22
0

Position them one after another in a LinearLayout and you are good to go. You don't need it to be inside. Structure is as follows:

<LinearLayout>
    <Button/>
    <EditText/>
</LinearLayout>
Robert K.
  • 1,043
  • 1
  • 8
  • 20
0

Here is the working code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    android:fitsSystemWindows="true">


    <!-- CONTENT -->

    <!-- BOTTOM INPUT SECTION -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/darker_gray"
        android:padding="8dp">

        <ImageButton
            android:id="@+id/button_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_group"
            android:background="@android:color/transparent"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/button_group"
            android:layout_marginRight="8dp"
            android:background="@android:color/white"
            android:padding="8dp">

            <ImageButton
                android:id="@+id/button_emoji"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_smile_face"
                android:background="@android:color/transparent"/>

            <ImageButton
                android:id="@+id/button_camera"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_camera"
                android:background="@android:color/transparent"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/button_emoji"
                android:layout_toLeftOf="@id/button_camera"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_centerVertical="true"
                android:hint="Type a message"
                android:background="@null" />

        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

OUTPUT:

enter image description here

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61