I want to change the background color of the Material Design button on Android.

Asked 2 years ago, Updated 2 years ago, 104 views

I wanted to change the background color of the button on Android, so I set it as follows.

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="Button"/>

However, it has a square shape as shown in the image.
How do I change the background color in its original shape?

Normal button
square button

android material-design

2022-09-30 10:40

2 Answers

In styles.xml, under themecustomization,

<item name="android:colorButtonNormal">@color/colorPrimary</item>

(android: to "colorButtonNormal" for .

If the background is designated, both the buttons and the background of the buttons will change color, so that's what happens.It's not that the shape has changed, it's just that it's melted into the background.


2022-09-30 10:40

By setting different themes for each button, you can only change the color of any button.
For example, in styles.xml, suppose you specify:

<style name="Button1">
    <item name="android:colorButtonNormal">#FF0000</item>
</style>

<style name="Button2">
    <item name="android:colorButtonNormal">#00FF00</item>
</style>

In layout.xml, if you specify the style you created as them, the color will change by that button.

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android: id="@+id/button1"
        android:text="button1"
        android:theme="@style/Button1"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android: id="@+id/button2"
        android:text="button2"
        android:theme="@style/Button2"
        />


2022-09-30 10:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.