Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining Multiple Button Styles

I found many solutions for button styling, but I have a small problem about defining multiple button styles. I am coding a calculator and I want to have different button styles for operation buttons and number buttons. So I had to define 2 different button styles. In my styles.xml file, I added following codes:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="buttonStyle">@style/OperationButtons</item>
    <item name="buttonStyle">@style/NumberButtons</item>
</style>

<style name="OperationButtons" parent="android:Widget.Button">
    <item name="android:background">#263238</item>
    <item name="android:paddingTop">15dp</item>
    <item name="android:paddingBottom">15dp</item>
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">.2</item>
    <item name="android:textColor">#fff</item>
    <item name="android:layout_margin">10dp</item>
    <item name="android:textSize">18sp</item>
</style>
<style name="NumberButtons" parent="android:Widget.Button">
    <item name="android:background">#607D8B</item>
    <item name="android:paddingTop">15dp</item>
    <item name="android:paddingBottom">15dp</item>
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">.2</item>
    <item name="android:textColor">#fff</item>
    <item name="android:layout_margin">10dp</item>
    <item name="android:textSize">18sp</item>
</style>

</resources>

However, it seems that I'm defining two different button styles and I am getting an error like "Resource entry AppTheme already has bag item buttonStyle."

How can I solve this problem?

like image 887
rawsly Avatar asked Aug 30 '25 17:08

rawsly


1 Answers

The buttonSyle that you define in your AppTheme is the default (so you can have only one).
But you can use the second style in your layouts doing this:

<Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 style="@style/NumberButtons"/>

Don't forget to remove:

<item name="buttonStyle">@style/NumberButtons</item>
like image 123
Juan Cruz Soler Avatar answered Sep 02 '25 08:09

Juan Cruz Soler