2

I am designing a game and need to make my app compatible back to API 16. I found how to do the AppCompatButton and set the style but how do I change the color to a more pleasing color like a light blue?

 <android.support.v7.widget.AppCompatButton android:id="@+id/button7" style="@style/Widget.AppCompat.Button.Colored" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:elevation="1dp" android:lines="2" android:text="Button"/> 

thanks

4 Answers 4

2

If you'll go into AppCompatButton class you'll see there this javadoc:

<ul> <li>Supports {@link R.attr#textAllCaps} style attribute which works back to {@link android.os.Build.VERSION_CODES#GINGERBREAD Gingerbread}.</li> <li>Allows dynamic tint of it background via the background tint methods in {@link android.support.v4.view.ViewCompat}.</li> <li>Allows setting of the background tint using {@link R.attr#backgroundTint} and {@link R.attr#backgroundTintMode}.</li> </ul> 

So you can set backgroundTint attribute to tour button in XML file. Like this:

 <android.support.v7.widget.AppCompatButton android:id="@+id/button7" style="@style/Widget.AppCompat.Button.Colored" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:elevation="1dp" android:lines="2" android:text="Button" app:backgroundTint="#555000"/> <-- Here 
Sign up to request clarification or add additional context in comments.

Comments

1

Add android:background attribute to your button declaration with value referring to the colour resource

android:background="@color/button_color" 

Or specifying color

android:background="#000000" 

1 Comment

If you'll set background to color - you'll lose all state (state_pressed, state_enabled and so on) and ripple effects (on 21+) that is provided in button by default.
0

Define a style like this:

<!-- put this in res/values/styles.xml --> <style name="StyledButton" parent="Widget.AppCompat.Button.Colored"> <item name="android:textColor">@color/button_text</item> <item name="colorButtonNormal">@color/button_background</item> </style> 

Apply to the button as usual:

<!-- apply style to button --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/StyledButton"/> 

This should be compatible with all the API levels.

1 Comment

Thank you for your response but I needed to set the colorAccent in the colors.xml
0

Figured it out, I needed to define the colorAccent:

 <color name="colorAccent">#448AFF</color> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.