skip to main | skip to sidebar

Android Development Tutorial

Pages

  • Home
 
  • RSS
  • Twitter
Related Posts Plugin for WordPress, Blogger...
Tuesday, March 6, 2012

How to Create Custom Styles in Android

Posted by Tanmay Jhawar at 11:22 AM – 0 comments
 
This article helps in explaining how to change the style of an UI element like Button, TextView etc. in an android application and different ways in which you can change the styling of different UI elements in your android application. Moreover, this post helps you in understanding the method of creating your own custom styles and how to use custom styles in an android application.

Styling your android application definitely helps in making your android app more attractive and interactive. By default, an android system provides a number of styling options for each UI element e.g. you can change the background colour, text colour, width, height and so many other properties of various UI elements like Buttons, TextView etc. There are a number of ways in which you can play with the styling of your android app or even individual elements present in your android app. Let’s take a brief look at all of them.

Different Ways of Changing Style of UI Element in Android App -

  • Styling each UI element separately –
This is the basic method in which you set the styling of each UI element separately using the Property View in eclipse. To set the style using this process all you have to do is to follow these steps –
  1. Right click on the UI element for which you have to set the style.
  2. Now go to “Show In” -> “Properties”. This will open the Properties View.
  3. In this property view you’ll be able to see a bunch of styling options for that UI element that you can change to modify the style of that UI element.
E.g.  You can set the text color to say green by scrolling down to “text color” property and setting its value to #00FF00 or width to say 30dp etc.
While this method is all good and simple yet its use should be avoided when you have a big app with lots of UI elements having the same style. This is obvious as this method requires you to set styling of individual UI elements separately. So, this method shouldn’t be used when you have many UI elements for which you want the same styling. 

  • Creating Custom Styles –
What if your app has say 10 buttons and all of height 30dp, text color red and background color green. So in this case setting up the styling values for each button separately would be time consuming and inefficient. For this case it would be better to have a single style and reuse that style whenever and wherever required, wouldn't it? In android this thing can be achieved by creating your own custom styles and use them as and when you require.To create your custom styles, the steps are as follows –

  • First create a new XML file say style.xml and select its Resource Type to Values. This will create a style.xml file inside the res -> values folder in your project hierarchy. Take a look at the snap below.



  • Open this style.xml file and go to the xml editor in eclipse. Although you can use the graphic editor but it’s better to make changes in the xml part by yourself.
  • Now it’s time to edit the style.xml file. So, to make every button of height 30dp, text color red and background color green you’ll have to add the following lines in you style.xml file.
            <style name="mystyle" parent="@android:style/ButtonBar">
                <item name="android:background">#00FF00</item>   
                <item name="android:textColor">#FF0000</item>    
                <item name="android:height">50dp</item>      
            </style>


Let’s understand the above xml code in detail –
  • The first element is a style tag telling the android system that this is a custom style and the values contained in this file are specifying the style of a particular (or a group) of UI elements.
  • Inside the style tag there’s an attribute “name”. This is used as a reference to this style. So if you want an UI element to use this style all you need to do is to set the style for that element to “mystyle” i.e. your custom style. Take a look at the snap below to see how to do this.
  • There’s another attribute of style tag namely “parent”. By specifying a parent of a custom style, we are telling that this custom style should inherit all the default styling from the parent and then we can override the styling of certain properties as we need. It’s like inheritance, the base class – sub class relation.
  • Now we need to override certain styling properties (or items) that we want different. For this create an item tag between the style opening and closing tags as shown above. In the “name” attribute of item tag you need to specify which property of an UI element you want to set. E.g. “android:background” should be used to set the background color and “android:textColor” is used for changing the text color.
 
Tip: Don’t worry about how to find these values like different values for the “parent” attribute or item’s name. Just use eclipse’s auto suggestions (start typing and press ctrl + space).

Now that we’ve created our custom style we need to tell the android system to use this style for a particular UI Element. To do this, go to your layout file and right click on the UI element whose style you want to set to this custom style i.e. “mystyle”. Now go to “Show In” -> “Properties”. This will open the Properties View. Scroll down to “style” and set its value to your custom style (i.e. the name attribute of style tag that I mentioned before and in this case it's "mystyle").

How to Link a Custom Style to an UI Element.


  • Using an existing custom style to create a new custom style –
You can also use a custom style to create another custom style. E.g. suppose you want a couple of buttons in your app to have a white background but the rest of the properties same as your custom style “mystyle” in style.xml that we created before. For this all you have to do is to create another style element in your style.xml (below you first style tag “mystyle”) and make the override for the properties you want to change. So to make changes in the background property you have to make changes like this –

            <style name="mystyle.bgwhite">
                <item name="android:background">#FFFFFF</item>  
            </style>

Notice the name of this style is “mystyle.bgwhite”. Here “mystyle” is telling the parent custom style for this new style. This style can be referred to in the same way as we did before.
Labels: Android Custom Style, Android Theme Style, Creating Custom Styles in Android Email This BlogThis! Share to X Share to Facebook

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments (Atom)

More Technical Blogs

  • Tech Savvy - Technology Tips
  • PHP Video Tutorial
  • Java Programs with Output
  • C Programming Tutorial
  • Linux Tutorial
  • Language Tutorial
  • Web Development tutorial
  • Popular
  • Recent
  • Archives

Popular Posts

  • Test Application for Samsung Android devices
    This test application write the traces from device to confirm the device behaviour. import java.io.File; import android.app.Activity; ...
  • What are Intents in Android?
    What are Intents in Android System? Different types of Intents in Android System. Example of how to create different types of Intents.
  • Android Video Tutorial - Learn How to Unit Test Your Android Application (with Robolectric)
    Tyler Schultz, from Pivotal Labs, introduces you to Robolectric - an open source unit testing framework that makes it possible to run unit...
  • Installing Android SDK Plugin for Eclipse IDE
    Short video clip that explains how to install the Android SDK plugin for the Eclipse IDE. When following the guidelines make sure you ente...
  • Android Video Tutorial: Android Application Development - Databases
    Class 3, Part 3. This video comes from Marakana's 5-Day Android Bootcamp Training Course which Marko Gargenta taught in San Jose, CA e...
  • Reordering of listview
    Listview rows order can be changed using drag and drop functionality.Dragging should be enabled beyond the visible listview position. ...
  • Extract all java classes from APK
    Step 1: Download "dex2jar-0.0.7.8-SNAPSHOT.zip" package from web. This package can be downloaded from " http://code.google...
  • Android Video Tutorial: Android Application Development - BroadcastReceivers
    Class 5, Part 3. This video comes from Marakana's 5-Day Android Bootcamp Training Course which Marko Gargenta taught in San Jose, CA e...
  • Get User name using Andriod
    import android.accounts.Account; import android.accounts.AccountManager; import android.content.Context; class Utils { private static ...
  • How to Create Multiple Activities in Android
    How to create multiple activities in android? Example of multiple activities in android.
Powered by Blogger.

Archives

  • ▼  2012 (44)
    • ►  October (31)
    • ►  September (3)
    • ▼  March (1)
      • How to Create Custom Styles in Android
    • ►  February (9)
  • ►  2011 (69)
    • ►  December (69)
 

Followers

Labels

  • Activities (9)
  • Andoird Menu (2)
  • Android timelineActivity. (1)
  • Android Adapter (1)
  • Android app (9)
  • Android App Inventor (1)
  • Android App Publishing (2)
  • Android Application Components (3)
  • Android Application Fundamental (2)
  • Android Architecture (1)
  • Android AsyncTask (1)
  • Android Basic (7)
  • Android Bootcamp Training (18)
  • Android Button Widget (3)
  • Android Custom Style (1)
  • Android Dialog (1)
  • Android Drawable (2)
  • Android Environment (1)
  • Android example (9)
  • Android File System (2)
  • Android Geolocation (2)
  • Android ImageView (1)
  • Android Installation (8)
  • Android intents (5)
  • Android lifecycle (1)
  • Android LIst (4)
  • Android Listener (4)
  • Android Manifest (3)
  • Android Market (1)
  • Android Notification (1)
  • Android Object (1)
  • Android Package File (1)
  • Android Platform (1)
  • Android service (4)
  • Android StatusActivity (1)
  • Android Theme Style (3)
  • Android Traceview (1)
  • Android UI (6)
  • Android Unit Testing (1)
  • Android Widget (4)
  • AndroidManifest.xml (4)
  • Application Icon (1)
  • Broadcast Receiver (2)
  • Content Providers (1)
  • Creating Activities (1)
  • Creating Custom Styles in Android (1)
  • Creating Multiple Activities (1)
  • Database (3)
  • draw9patch (1)
  • Eclipse (12)
  • Explicit Intents (2)
  • Explicit Intents Example (1)
  • Hello world with Android (1)
  • Helloworld with Android (5)
  • Implicit Intents (2)
  • Implicit Intents Example (1)
  • Layout View (3)
  • lifemichael (8)
  • Location Sensor (1)
  • Multiple Activities (2)
  • Netbeans (1)
  • OpenGL ES Graphics (1)
  • Passing Values with Intents (2)
  • Project Structure (1)
  • Retrieving Image URI from Intents (1)
  • Setting Android Environment (1)
  • SQLite (3)
  • TGENT (8)
  • UserGroupAtGoogle (8)
  • XML (1)
  • xtensive arts Training (11)
 
 
© 2011 Android Development Tutorial | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger