The value always begins with a pound (#) character and then followed by the Alpha-Red-Green-Blue information in one of the following formats:
- #RGB
- #ARGB
- #RRGGBB
- #AARRGGBB
create /res/values/colors.xml to add our Color Resources in XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background_color">#f0f0f0</color>
<color name="text_color_red">#ff0000</color>
</resources>
Modify main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_color"
>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
Java Code
package com.exercise.AndroidColorResources;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidColorResources extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView Text = (TextView)findViewById(R.id.text);
Text.setTextColor(getResources().getColor(R.color.text_color_red));
}
}
Download the files.