F. Dev
F. Dev

Reputation: 23

Android styles doesn't allow colors

I'm new on android development . I'm making a program and I want to change the button style , and I can do that.

here the picture about what is the button style recommend : enter image description here

as you see it is smooth and pretty ! But when I want to change the color of the button ( with a style ) I got problem ! : enter image description here

how can I change the style ( Button color ) and keep the button smooth and good-looking ?

Upvotes: 0

Views: 394

Answers (4)

Manishika
Manishika

Reputation: 5564

Use style as theme :

 <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:textColor="@android:color/white"
        android:theme="@style/blackButton"
    />

And add this style in your styles.xml

<style name="blackButton" parent="Widget.AppCompat.Button.Colored">
        <item name="colorButtonNormal">@android:color/black</item>
        <item name="colorControlHighlight">@color/colorAccent</item>
    </style>

Upvotes: 3

ramji
ramji

Reputation: 1992

Add this code in styles.xml

<resources>
 <style name="App.Button.Style" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:background">@color/red</item>
        <item name="android:textColor">@android:color/white</item>
    </style>
</resources>

And change your button style

 <Button
     android:id="@+id/btnPlayVideo"
     android:text="Play Video"
     style="@style/App.Button.Style"/>

Upvotes: 0

Nikita Shah
Nikita Shah

Reputation: 156

Make one style which extend default button style and set background color to it. In styles.xml

<style name="black_button_style"
       parent="Base.Widget.AppCompat.Button.Colored">
             <item name="android:layout_width">match_parent</item>
             <item name="android:layout_height">wrap_content</item>
             <item name="android:typeface">sans</item>
             <item name="android:background">#000000</item>
</style>

In Your Activity xml.

   <Button 
    style="@style/black_button_style/>

Upvotes: 0

KlajdPaja
KlajdPaja

Reputation: 959

Changing the background color removes the default material design style, you should instead create your own style for that button with parent theme Widget.AppCompat.Button.Colored :

 <!--fix to keep material design for buttons and add background color-->
    <style name="blackButton" parent="Widget.AppCompat.Button.Colored">
        <item name="colorButtonNormal">@color/colorBlack</item>
    </style>

and than apply this theme to button using android:theme="@style/blackButton"

Upvotes: 0

Related Questions