Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android style specify button color

I am trying to apply a button text color by default in styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme">
   <item name="android:windowFullscreen">true</item>
   <item name="android:windowBackground">@color/black</item>
   <item name="android:textColor">@color/green</item>
</style> 

How do I make the style change button color, and apply throughout the entire application? My mainfest includes:

    <application
    android:icon="@drawable/ic_launcher"
    android:label="Hack the World"
    android:theme="@style/AppTheme">

This changes all text (such as a TextView), but does not change the text in a button. If I use the ColorThemes style from above and place it in the xml for the button like this:

    <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:onClick="loadSavedgame"
    android:text="Load Saved Game" 
    android:textColor="@color/green" />"

Then it works perfectly. Why doesn't this apply universally because of the style? All different versions of styles.xml have the same code.

like image 920
Rilcon42 Avatar asked Dec 24 '12 16:12

Rilcon42


1 Answers

I eventually discovered that a button is, in fact, a 9-patch image that is located at @android:drawable/btn_default in order to change the background you must modify this image. In order to change button text color I had to set a button widget style and import it into my app theme like so:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" >
   <item name="android:windowFullscreen">true</item>
   <item name="android:windowBackground">@color/black</item>
   <item name="android:textColor">@color/green</item>
   <item name="android:buttonStyle">@style/ButtonText</item>
</style> 

    <style name="ButtonText" parent="@android:style/Widget.Button">
   <item name="android:textColor">@color/green</item>
   <item name="android:background">@android:drawable/btn_default</item><!-- only way to change this is to change the src image -->
   <item name="android:layout_width">80.0px</item>
   <item name="android:layout_height">40.0px</item> 
</style> 
</resources>
like image 113
Rilcon42 Avatar answered Sep 20 '22 04:09

Rilcon42