How to create check box with custom background color.
Check box is simple item selection mark box and with the background color it will look more specific and nice. In this tutorial we are setting up background color on check box from MainActivity.java programming file using setBackgroundColor() method. So here is the complete step by step tutorial for How to set background color for checkbox in android programmatically.
How to set background color for checkbox in android programmatically.
Code for MainActivity.java file.
package com.checkboxbackgroundcolor_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 checkbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkbox = (CheckBox)findViewById(R.id.checkBox1);
//Setting up background color on checkbox.
checkbox.setBackgroundColor(Color.parseColor("#00e2a5"));
}
}
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.checkboxbackgroundcolor_android_examples.com.MainActivity" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="CheckBox" android:paddingRight="10dp" /> </RelativeLayout>
Screenshot: