Grady McGhee
Grady McGhee

Reputation: 351

AppCompatButton and Color

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

Upvotes: 2

Views: 3293

Answers (4)

Grady McGhee
Grady McGhee

Reputation: 351

Figured it out, I needed to define the colorAccent:

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

Upvotes: 0

SpaceBison
SpaceBison

Reputation: 3131

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.

Upvotes: 0

Vadims Savjolovs
Vadims Savjolovs

Reputation: 2668

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"

Upvotes: 1

 Ekalips
Ekalips

Reputation: 1491

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

Upvotes: 2

Related Questions