Create custom checkbox with custom text color using java coding file at run time.
Dynamically setting up text color means here we are applying text color using MainActivity.java programming file with setTextColor() method. So here is the complete step by step tutorial for How to Change CheckBox text color in android programmatically.
How to Change CheckBox text color in android programmatically.
Code for MainActivity.java file.
package com.changecheckboxtextcolorprogrammatically_android_examples.com; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.widget.CheckBox; public class MainActivity extends Activity { CheckBox one,two,three; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); one = (CheckBox)findViewById(R.id.checkBox1); two = (CheckBox)findViewById(R.id.checkBox2); three = (CheckBox)findViewById(R.id.checkBox3); //Adding color to checkbox text. one.setTextColor(Color.parseColor("#e80173")); two.setTextColor(Color.parseColor("#96039f")); three.setTextColor(Color.parseColor("#039f83")); } }
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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="com.changecheckboxtextcolorprogrammatically_android_examples.com.MainActivity" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="171dp" android:text="ONE" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/checkBox1" android:layout_below="@+id/checkBox1" android:text="TWO" /> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/checkBox2" android:layout_below="@+id/checkBox2" android:text="THREE" /> </RelativeLayout>
Screenshot: