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?
In styles.xml
, under theme
customization,
<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.
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"
/>
© 2024 OneMinuteCode. All rights reserved.