Custom Views in the application (Android)

Go To StackoverFlow.com

1

I've a drawable for some items like buttons, checkboxes, progress bars and I'd like to define those drawables for beeing the default style of all the buttons my app uses.

Is that possible without declaring the style in each View individually?

If the answer is to use a Theme for the app I'd appreciate an example (changing the style of all the buttons for example) because I don't understand how to define it.

Thanks in advance

2012-04-04 19:15
by Addev


2

The answer is to use a theme as you were expecting. Good documentation can be found on android developer site: http://developer.android.com/guide/topics/ui/themes.html

For the button you can do like this: Define a new theme:

<style name="MyTheme" parent="@android:style/Theme.Holo">
    <item name="android:buttonStyle">@style/button</item>
</style>

Where the button style is defined as:

<style name="button" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/button_background_selector</item>
    <item name="android:textColor">@drawable/button_text_selector</item>
</style>

The button_background_selector is defined like this:

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

    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/button_disabled" android:state_enabled="false"></item>
    <item android:drawable="@drawable/button_default"></item>

</selector>

All those drawables can be your images - button_pressed can be a png in the drawable folders. You also have to apply the theme to your application in the manifest like this:

<application
    android:theme="@style/MyTheme" >
</application>

Hope this helps :)

2012-04-04 19:23
by azertiti
Ads