Using color in Android, by XML

In Android, colors can be defined directly, such as "#ff0000" for red. You can also create a resource file under /res/values folder (with any file name), with your own color id defined inside. Such that you can access them in Java code using id. Android also defined a base set in android.R.color.

example:



/res/values/mycolor.xml


#ff0000
#00ff00
#0000ff



/res/layout/main.xml

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/background"
>

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="WHITE"
android:textColor="@android:color/white"
android:id="@+id/whitebutton"
/>


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RED"
android:textColor="#ff0000"
android:id="@+id/redbutton"
/>


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="GREEN"
android:textColor="@color/green"
android:id="@+id/greenbutton"
/>


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BLUE"
android:textColor="@color/blue"
android:id="@+id/bluebutton"
/>





Post a Comment

Previous Post Next Post

Contact Form