Hasan S.
Hasan S.

Reputation: 41

Android layout XML for button with image background

[android layout activity][1]

** I want to make the edges of the buttons circular and to add different images for each button! if I create a resource XML (custom_button) with corners and add it to each button as background I cant add image anymore**

Upvotes: 0

Views: 226

Answers (2)

Usman Zafer
Usman Zafer

Reputation: 1361

If you want to

  1. Add rounded corners

  2. Add image as background.

I would recommend you to use ImageButton instead of Button.

1. create the xml file named it as circularedge.xml

 <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="#33DDFF" />
            <corners android:radius="4dp" />
        </shape>

change the attributes to your liking.

2. In your ImageButton add this attribute android:background="@drawable/circularedge"

<ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageButton"
                android:src="@drawable/friends"
                android:background="@drawable/circularedge" />

Upvotes: 0

Nour Eldien Mohamed
Nour Eldien Mohamed

Reputation: 961

you can try add your custom shape like this

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" 
          android:padding="5dp">
            <corners
                 android:bottomRightRadius="4dp"
                 android:bottomLeftRadius="4dp"
                 android:topLeftRadius="4dp"
                 android:topRightRadius="4dp"/>
         </shape>
   </item>
   <item android:drawable="@drawable/yourImage" />
</layer-list>

Upvotes: 1

Related Questions