'Is it possible to put background image and curved corner in a button in xamarin android?

I am trying to input an image in a regular button, however it does not show up. My code looks like this

    <Button
    android:text="@string/buttonexit"
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:id="@+id/buttonexit"
    android:layout_marginLeft="50dp"
    android:background="@drawable/wood"/>

I also hoped and included src instead of background, however it still did not work.

    <Button
    android:text="@string/buttonexit"
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:id="@+id/buttonexit"
    android:layout_marginLeft="50dp"
    android:src="@drawable/wood"/>

Lastly I also tried to make an image button, however once I added an image I cannot adjust the corners anymore.



Solution 1:[1]

You can use AppCompatButton instead of Button.

For a Button, the primary color defined in the app theme overrides any custom background that you want to apply to a Button, but it is not the case for the AppCompatButton.

For example:

<androidx.appcompat.widget.AppCompatButton
android:text="test"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="@+id/buttonexit"
android:layout_marginLeft="50dp"
android:background="@drawable/bg"/>

Besides, you can also use ImageButton to achieve this function.

You can add an image by property android:src and adjust the corners with property android:background by creating a shape xml file.

You can refer to the following code:

1.create file button_shape.xml in folder drawable:

  <?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--background color-->
    <solid android:color="#20a4e5"/>
    <!--radius-->
    <corners android:radius="10dip"/>
    <!--Sets the width and color of the border line-->
    <stroke android:width="2dp" android:color="#fffff0" />

</shape>

2.set it for android:background of ImageButton

 <ImageButton
    android:text="test"
    android:layout_width="300dp"
    android:layout_height="80dp"
    android:layout_marginLeft="50dp"
    android:background="@drawable/button_shape"
    android:src="@drawable/grass"/>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1