CheckBox টেক্সট এবং ব্যাকগ্রাউন্ড রঙ পরিবর্তন
এই সহজ উদাহরণটি দেখায় কিভাবে প্রোগ্রামেটিক্যালি চেকবক্সের টেক্সট রঙ এবং ব্যাকগ্রাউন্ড রঙ পরিবর্তন করা যায়।
ধাপ ১: প্রজেক্ট তৈরি
ধাপ ২: লেআউট
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click the check box" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check box"
android:layout_below="@id/tv"
android:paddingRight="15dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Color"
android:layout_below="@id/checkbox" />
</RelativeLayout>
ধাপ ৩: MainActivity.java
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CheckBox checkBox = findViewById(R.id.checkbox);
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
checkBox.setTextColor(Color.RED);
checkBox.setBackgroundColor(Color.parseColor("#cbff75"));
});
}
}
উন্নত কাস্টমাইজেশন (API 21+)
শুধু টেক্সট/ব্যাকগ্রাউন্ড রঙ না বদলে, চেকবক্সের টিকмар্ক (buttonDrawable) রঙ পরিবর্তন করতে:
XML-এ (Theme/Style ব্যবহার করে)
<CheckBox
android:buttonTint="@color/your_color"
android:textColor="@color/your_text_color" />
Java-এ (CompoundButtonCompat ব্যবহার করে)
import androidx.core.widget.CompoundButtonCompat; CompoundButtonCompat.setButtonTintList(checkBox, ColorStateList.valueOf(Color.RED));
ড্রয়েবল ব্যবহার করে পূর্ণ কাস্টম চেকবক্স
res/drawable/custom_checkbox.xml তৈরি করুন:
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checked_icon" android:state_checked="true"/>
<item android:drawable="@drawable/unchecked_icon" android:state_checked="false"/>
</selector>
তারপর android:button="@drawable/custom_checkbox" ব্যবহার করুন।