A Detailed Overview On View Styling In Android

Styles are perhaps the hardest things to get right on Android-powered devices. It’s quite easy for designers and developers to get frustrated when it comes to styling the views. How many times have you feared to break your app’s originality in the wake of changing a particular style? I’m sure many a times. In this post, I’ll be offering you some handy tips on styling Views in Android. So, let’s get started and unleash more about the view styling concept in Android.

3d human with a red question mark

What exactly is a Style in Android?

Style basically refers to a collection of properties that determine the look and format for a View or window. Some of the key properties specified by style include padding, height, font size, font color, background color and a lot more. One of the best parts about styles in Android is that they share a philosophy that’s similar to cascading stylesheets used in web design. In other words, they allow the user to separate the design from the original content.

Here’s a sample layout XML:

<TextView

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:textColor=”#00FF00″

android:typeface=”monospace”

android:text=”@string/hello” />

You can convert the aforementioned layout XML into the below XML using style:

<TextView

style=”@style/CodeFont”

android:text=”@string/hello” />

If you carefully observe the above code, you’ll find that all of the style-related attributes have been removed from the layout XML and instead inserted into the style definition that’s called CodeFont. This style definition is then applied with the style attribute.

Styling

When should you use styles in Android?

Being in the profession of Android Programmers, you might be aware of the fact that there are a number of situations when using a style works best instead of using inline attributes. You can use styles under a situation where multiple Views are semantically similar. Also, you can opt for using references within styles when you find doing so appropriate. Here’s a demo for the way in which you can define a style:

<style name=”MyButton”>

<item name=”android:minWidth”>88dp</item>

<item name=”android:minHeight”>48dp</item>

</style>

A brief on defining Styles in Android

When it comes to a style, it is defined in an XML resource which is separated from the XML that actually specifies the respective layout. The XML file holding the style will reside under res/values/directory of the project and contain <resources> as the root node. Although, you can name the XML file arbitrarily, ensure that it uses the .xml extension. In order to define multiple style for a single file, you can opt for <style> tag, with each style having its unique name that would aid in identifying the style.

An insight on using styles in Android

Once you’re done with defining the style, you can opt for using it in your XML layout file with the help of style attribute. Here’s the step-wise process that should be followed for using a style for individual elements:

Step 1: Use Eclipse IDE for creating a new Android application and name it as StyleDemo with package name as com.example.styledemo.

Step 2: Now, modify the src/MainActivity.java file by adding click event listeners and handlers.

Step 3: As per this step, define your style as available within the global style file called res/values/style.xml and define the custom attributes for the button under focus.

Step 4: Now, modify the default content of res/layout/activity_main.xml file to add a set of Android UI controls that would be used for setting the new style.

Step 5: As per the fifth step, define the constants in the res/values/strings.xml file.

Step 6: Finally, run the application for launching the Android emulator, followed by verifying the result of moderation’s that have been done to the Android application.

Some bonus tips on using styles correctly

Although using styles in Android has always been full of fun and excitement, there are times when even the professionals tend to get stuck up with issues in styling the views in Android applications. Here are a few measures that will enable you to stay at bay from such styling errors:

Never create a style if it’s to be used only once. Since styles are referred to being an extra layer of abstraction, they can definitely add complexity to the app functioning. Thus, there’s no point building a style unless you’re not using it in multiple locations.
Never create a style just because multiple Views are using the same attributes. Since styles play a pivotal role in reducing the count of repeated attributes, it is recommended to use a style only when you can edit the same without any side effects.

Wrapping Up

Now you know it all, the basic details about the correct use of styles in Android. Hope the information furnished above would aid you in undertaking all your view styles initiatives in the correct manner.

Victoria Brinsley is a app developer for Appsted Ltd – a leading Android Apps Development Company. In case, you are looking forward to avail Android App Developers for Hire, just get in touch with her for best assistance.

We will be happy to hear your thoughts

Leave a reply

Thetechhacker
Register New Account
Reset Password