Background image for a button in Android

advertisements

I am trying to add a background image for a button and it is working, but not as I expected.

1st picture (without background)

2nd picture (with background)

I would like the background to cover only the button. In other words, the image should have the same form as the 2 buttons from above (I'm talking about the round corners and a bit smaller size).

Here is the xml of the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity"
android:orientation="vertical">

<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <Button
        android:onClick="playerStats"
        android:text="@string/plStats"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <Button

        android:text="@string/comp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <Button
        android:background="@drawable/calendar"
        android:text="@string/calendar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>


its easy to do that

you should use ImageButton and remove those Buttons for example

<ImageButton
    android:src="@drawable/calendar"
    android:text="@string/calendar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

also if you want to remove the background of that button you can use :

android:background="@null"