কম্পিউটার টিউটোরিয়াল

Kotlin-এ Android CheckBox-এর রঙ পরিবর্তন করার পদ্ধতি

পূর্ব প্রস্তুতি

এই উদাহরণটি দেখায় কিভাবে বাটন ক্লিক করলে CheckBox-এর টেক্সট রঙ এবং ব্যাকগ্রাউন্ড রঙ প্রোগ্রামেটিক্যালি পরিবর্তন করা যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project নির্বাচন করুন।

ধাপ ২: activity_main.xml লেআউট

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Click the check box"
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="16sp"
        android:textStyle="bold" />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:layout_centerInParent="true"
        android:text="Check box" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/checkbox"
        android:layout_centerInParent="true"
        android:text="Change Color" />

</RelativeLayout>

ধাপ ৩: MainActivity.kt-এ রঙ পরিবর্তন লজিক

import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        val checkBox: CheckBox = findViewById(R.id.checkbox)
        val button: Button = findViewById(R.id.button)

        button.setOnClickListener {
            checkBox.setTextColor(Color.MAGENTA)
            checkBox.setBackgroundColor(Color.parseColor("#cbff75"))
        }
    }
}

ধাপ ৪: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.checkboxcolor">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

পরীক্ষা করুন

অ্যাপ চালান। "Change Color" বাটনে ক্লিক করলে CheckBox-এর টেক্সট রঙ মজেন্টা এবং ব্যাকগ্রাউন্ড রঙ লাইট গ্রিন হবে।

নোট

চেকবক্সের টিক মার্কের রঙ পরিবর্তন করতে `buttonTint` বা `CompoundButtonCompat.setButtonTintList()` ব্যবহার করুন।

  1. এন্ড্রয়েডে DatePicker ডায়ালগের রঙ পরিবর্তন করার পদ্ধতি

  2. অ্যান্ড্রয়েডে মেনু আইটেমের টেক্সট রঙ পরিবর্তন করার পদ্ধতি

  3. ActionBarActivity-এর ActionBar-এর ব্যাকগ্রাউন্ড রঙ কীভাবে পরিবর্তন করবেন?

  4. অ্যান্ড্রয়েডে অপশন মেনুর ব্যাকগ্রাউন্ড রঙ পরিবর্তন করার পদ্ধতি