Click here to Skip to main content
12,950,090 members (49,892 online)
Click here to Skip to main content
Add your own
alternative version

Stats

4K views
5 bookmarked
Posted 19 May 2014

Use Android Themes to Incorporate a DRY Layout

, 19 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple steps needed to globally change a widget property

Keep Your Powder DRY

Most of us are familiar with the DRY (Don't Repeat Yourself) Principle, the last element of the SOLID set of principles.

Here's how you can apply it in Android Layouts to set a property value for all widgets of a certain type in one place, so that if you need/want to change it later, you only need do it in one place, rather than revisit oodles of widgers within possibly multiple Layout files:

  1. Open the \[appName]\app\src\main\res\values\styles.xml file.

    If you don't have one (styles.xml is created automatically for you there if you are using Droidio), create it at that spot, and add this to it to begin with (in Droidio, it's already there):

    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
        </style>
    
    </resources>
  2. Verify that "AppTheme" (you can change the name to "YanquiGoogleDandy" or whatever if you want to) is referenced in AndroidManifest.xml like so:
    android:theme="@style/AppTheme"

    In context, that is:

     <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
    . . .
    
  3. Now you can add a style in styles.xml, like so:
    <style name="NavyTextViewStyle" parent="android:Widget.TextView">
        <item name="android:textColor">#000080</item>
    </style>
    
  4. Now add the reference to it in the "AppTheme" section like so:
    <item name="android:textViewStyle">@style/NavyTextViewStyle</item>
    

    In context:

    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="android:textViewStyle">@style/NavyTextViewStyle</item>
        </style>
    
        <style name="NavyTextViewStyle" parent="android:Widget.TextView">
            <item name="android:textColor">#000080</item>
        </style>
    
    </resources>

So now AppTheme activates the "NavyTextViewStyle" style you added, and since the app uses the "AppTheme" theme, all EditText widgets will inherit the color value ("#000080", or Navy), as can be seen here:

Click to enlarge image

What if you don't want your global value to take effect in a particular case? Just override it by explicitly declaring a different value there, such as this, to use dark red instead:

android:textColor="#8B0000"

Stay Warm and DRY

Now you can use that same methodology to globally set other values for any widgets you use. No need to thank me - just "pay it forward!"

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

B. Clay Shannon
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 19 May 2014
Article Copyright 2014 by B. Clay Shannon
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid