Monday, May 27, 2013

How to change the background color of Android activity

Here I'm going to show the way to change the background color of the Android activity.



In order to change the activity's background color we need to do two changes.
First we need to add color property to strings.xml. Open the strings.xml in res >> values directory


Then create color property and add the value for color as shown below. (Color codes)

#000000

Now your strings.xml should like this.


    BgColor
    Settings
    Hello world!
    #000000

After changing strings.xml file we need to add android:background property to the activity layout xml file.
First open the activity_main.xml in the res >> layout directory.

Then add this line in to your layout xml
android:background="@color/black"
Here is the complete code for activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/black"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
</RelativeLayout>

Resources:
 http://developer.android.com/guide/topics/resources/more-resources.html#Color

That's it, :)
Happy coding!
Your ideas are always welcome!!!

1 comment: