1

enter image description here

Below is what I tried to do both icon and text in the center of a button but icon gets top of the image. I want them to be in the center.

 <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#fff"
                    android:layout_weight="1"
                    android:gravity="center"
                   >
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/baseline_home_24"
                    />
                    <TextView
                        android:id="@+id/btnCleaner"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Cleaner"/>
                </RelativeLayout>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841

7 Answers7

1

Try this:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:layout_weight="1"
    android:gravity="center"
    >
    <ImageView
        android:id="@+id/image" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/baseline_home_24"
        />
    <TextView
        android:id="@+id/btnCleaner"
        android:layout_toRightOf="@id/image"
        android:layout_marginLeft="8dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cleaner"/>
</RelativeLayout>
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
0

Do Simply like this:-

 <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#fff">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/baseline_home_24"
                        android:layout_centerInParent="true"  />
                    <TextView
                        android:id="@+id/btnCleaner"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"/>
/>

Hope this will help.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30
0

What about this one?

<FrameLayout style="?android:attr/buttonStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        style="?android:attr/buttonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@null"
        android:clickable="false"
        android:drawableLeft="@android:drawable/ic_delete"
        android:focusable="false"
        android:gravity="center"
        android:minHeight="0dp"
        android:minWidth="0dp"
        android:text="Button Challenge" />
</FrameLayout>

Output:

enter image description here

Tejashwi Kalp Taru
  • 2,994
  • 2
  • 20
  • 35
0

Try this it's relevant to your requirement:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/grey_back"
    android:clickable="true"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="10dp">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_margin="5dp"
        android:text="Do stuff" />
</LinearLayout>
InsaneCat
  • 2,115
  • 5
  • 21
  • 40
  • If my answer helps you then please mark as a right my answer from tick mark otherwise i'm gonna remove this answer brother : https://www.google.com/search?q=right+mark+as+an+answer+stackoverflow&sxsrf=ACYBGNQVsfXj9VeT9WRKWerxeSRoAMe2uQ:1579936139192&source=lnms&tbm=isch&sa=X&ved=2ahUKEwiQk-DimJ7nAhWBc30KHdm9CGAQ_AUoAXoECAwQAw&biw=1517&bih=730#imgrc=9_JN49MpzDykBM: – InsaneCat Jan 25 '20 at 07:48
0

You can do this using only Textview like below:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="center">
           <TextView
            android:id="@+id/btnCleaner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cleaner"
            android:layout_centerInParent="true"
            android:drawableLeft="@drawable/yourDrawable" // your image 
            android:drawablePadding="3dp"/>
</RelativeLayout>

if you want image at left of text then use drawableLeft & if at right of text then use drawableRight. Also you can give padding to image from text by drawablePadding.

Rishav Singla
  • 485
  • 4
  • 10
0

This can be achieved with the new MaterialButton class, found in Google's Material Components

For example:

<com.google.android.material.button.MaterialButton
    android:id="@+id/material_icon_button"
    style="@style/Widget.MaterialComponents.Button.Icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/icon_button_label_enabled"
    app:icon="@drawable/icon_24px"
    app:iconPadding="8dp"/>

Would display a button with an icon that has padding

The source code of this button and the rest of these components can be found on the project's Github here

Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
0

You can use the MaterialButton provided by the official Material Components Library.

You have to apply the app:iconGravity="textStart" attribute.
Something like:

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        app:icon="@drawable/..."
        app:iconGravity="textStart"
        android:text="@string/..."/>

enter image description here

Also you can change the padding between the icon and the text with the attribute app:iconPadding="xxdp".

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841