Search

Wednesday, June 24, 2009

Applying Style and themes attribute for widgets Buttons, text view and more in android

define styles and themes in XML for Android widgets such as editbox, spinner etc, styles can be written at once so that it can be reused for similar widgets instead of writing same code again and again but you have to note few points when you start implementing styles and themes

1. You cannot see the result of style property in the Eclipse or adt editor it can only be seen in run time on emulator or device

2. use default android:theme if you want to apply only standard size and width properties

3. you can find the style properties to be applied in below links

http://developer.android.com/reference/android/R.styleable.html
http://developer.android.com/reference/android/R.style.html

below is the sample code on how to apply a theme for button write it in styles.xml in values folder


<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SampleButtStyle" parent="android:Widget.Button">
<item name="android:layout_width">fill_parent</item>
<item name="android:textColor">#ff000000</item>
<item name="android:layout_height">25dip</item>
<item name="android:layout_marginTop">10dip</item>
<item name="android:layout_marginRight">55dip</item>
<item name="android:layout_marginLeft">15dip</item>
<item name="android:gravity">left|center</item>
</style>
</resources>

Now you can call the style where ever we want with similar style in the layout so that property of all similar buttons looks same

<Button
android:id="@+id/SampleButton"
style="@style/SampleButtStyle"
android:text="Click"
android:layout_below="@+id/relayout1" >
</Button>

Basic Description

In the above code <style> contains one or more items to describe the common properties to be applied which is referred to as themes

-->name property is used to identify the theme id
-->parent is optional it is used to inherit specified theme to be inherited into this theme make sure the package name is correct else you might get error

<item> here item tag contains the properties of widgets it can be size, text, color value or any other widget property

Similarly you can apply themes and styles for almost all the widgets you can find the xml file which contains the theme ids in the following path \..yoursdkFolder\platforms\..yoursdkversion1.5\data\res\values\themes.xml in your SDK folder

0 comments:

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google