Sudarshan
Sudarshan

Reputation: 1036

Can't inherit button color on style

I am trying to define button properties on style. My code is

<style name="button">
    <item name="android:paddingTop">5dp</item>
    <item name="android:paddingBottom">5dp</item>
    <item name="android:textAllCaps">false</item>
    <item name="colorButtonNormal">#f37022</item>
    <item name="android:textColor">#ffffff</item>
</style>

And using as

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Prepaid Package"
        style="@style/button"/>

But the color of the button's normal state is not changing.

This button style is only for some buttons. Not for all butons.

What to do now?

Upvotes: 1

Views: 356

Answers (1)

user3956566
user3956566

Reputation:

edit after question clarification

Save you style.xml in res/values/

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="custom_button" parent="@android:style/Widget.Button">
        <item name="android:paddingTop">5dp</item>
        <item name="android:paddingBottom">5dp</item>
        <item name="android:textAllCaps">false</item>
        <item name="colorButtonNormal">#f37022</item>
        <item name="android:textColor">#ffffff</item>
    </style>

Apply it like so:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Prepaid Package"
    style="@style/custom_button"/>

first answer

You need to inherit the parent style.

<style name="Button" parent="@android:style/Widget.Button">
    <item name="android:paddingTop">5dp</item>
    <item name="android:paddingBottom">5dp</item>
    <item name="android:textAllCaps">false</item>
    <item name="colorButtonNormal">#f37022</item>
    <item name="android:textColor">#ffffff</item>
</style>

That way all your Button's that are not styled will have your custom style. i.e. no need to put in the style value.

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Prepaid Package"/>

Upvotes: 1

Related Questions