Thursday 8 December 2011

Android - Generic declaration of colours


It is very important that we declare background colours and layouts generically. I made this mistake of hardcoding the colours in the individual xml files. It became really hectic every time I wanted to try different colours. Then finally, I decided to declare them generically. Here is how you do it.

Declare mycustomcolors.xml in the values folder. Define all the colour codes here.


<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="bgcolor">#F8F8F8</color>
<color name="headtxtcolor">#e69b28</color>
<color name="othertxtcolor">#000000</color>
<color name="headerEnd">#999999</color>
<color name="headerStart">#000000</color>
<color name="profileEnd">#E69B28</color>
<color name="profileStart">#FFAE33</color>
<color name="sectionEnd">#FFFFFF</color>
<color name="sectionStart">#CCCCCC</color>
</resources>

   
The above colour codes can be used in shapes and other layouts. This is for a gradient colour button profile_band.xml in drawable-hdpi. 

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<gradient
    android:startColor="@color/profileStart"
    android:endColor="@color/profileEnd"    
    android:angle="90" /> 
    
</shape>
Text colours used in an xml..

 <TextView
        android:id="@+id/profName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/header"
        android:paddingLeft="10dp"
        android:text="profile name"
        android:textColor="@color/othertxtcolor"
        android:textSize="14dp"
        android:layout_gravity="center_vertical"
       />


As mobile apps should have good screen appearance , it is very important that you have the flexibility to try various options.

No comments:

Post a Comment