Button with image and text

Asked

Viewed 6,619 times

3

I have a button that has round borders and when it is selected changes color. A certain text is associated with that button. It turns out I want the button to have an image plus the text and I can’t do it.

<Button
    android:text="Texto"
    android:background="@drawable/roundshape4_selector"
    android:layout_width="85dp"
    android:layout_height="85dp"
    android:gravity="center"
    android:id="@+id/texto"
    android:textStyle="bold"
    android:onClick="onClick"
    android:textColor="#B56EB6"
    android:textSize="12dp"/>

Here is the code of roundshape4_selector used for when the button is pressed to change color

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/roundshape4_pressed" />
    <item android:drawable="@drawable/roundshape4" />
</selector>

roundshape4_pressed

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/pink"/>
    <corners android:radius="10dip" />
</shape>

This is where I’m trying to tag the image <img scr="@drawable/imagem48"></img> but only the text appears.

roundshape4

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <img scr="@drawable/imagem48"></img>
    <corners android:radius="10dip" />
    <stroke android:color="@color/pink"
        android:width="6dip"></stroke>
    <solid android:color="@color/white"></solid>
</shape>

How can I make the button to show me the image and text at the same time?

1 answer

3


You can add an image to a button using the following attributes:

android:drawableLeft
android:drawableRight
android:drawableTop
android:drawableBottom

Example to place an image positioned on the left side(left) of the text:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:drawableLeft="@drawable/button_icon"
    ... />

More info on documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.