user3422722
user3422722

Reputation: 17

Deriving a theme based on theme.appcompat

I want to derive a theme from theme.appcompat. In that theme i want to set custom buttons styles. The code of buttons is given below. How do i add them to a theme i create and how do i create that theme.In which files do i need to include that theme besided res/values/style.The code below is in res/drawable folder with name red_button.xml.

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
    android:drawable="@drawable/componentstyles_btn_default_pressed_holo_light" />

<item android:state_focused="true" 
    android:drawable="@drawable/componentstyles_btn_default_focused_holo_light" />

<item
    android:drawable="@drawable/componentstyles_btn_default_normal_holo_light" />
</selector>

Now the code in res/value/styles is

<resources>
<style name="MyTheme" parent="Theme.AppCompat.Light">
<item name="android:buttonStyle">@style/MyButton</item>
</style>

<style name="MyButton" parent="android:Widget.Button">
<item name="android:background">@drawable/red_button</item>
</style>
</resources>

Manifest is like this:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyTheme" >

Upvotes: 0

Views: 284

Answers (1)

Tadej
Tadej

Reputation: 2921

In your res/values folder, you want to create a file styles.xml, specifying

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--Custom styles here -->
</style>

The selector you're showing could be put in the res/drawable folder. If you want to apply it to all your buttons, you'd want to do something like:

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:buttonStyle">@style/MyButton</item>
</style>

<style name="MyButton" parent="android:Widget.Button">
    <item name="android:background">@drawable/your_selector</item>
</style> 

To enable your custom theme, apply it in the AndroidManifest.xml to a specific Activity, or the whole app, e.g.:

<application android:label="@string/app_name"
             android:icon="@drawable/ic_launcher"
             android:theme="@style/MyTheme">

Upvotes: 1

Related Questions