यह उदाहरण दर्शाता है कि Kotlin में प्रोग्रामेटिक रूप से CheckBox के टेक्स्ट रंग और पृष्ठभूमि रंग को कैसे बदला जाए।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio: File → New Project।
चरण 2 - 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="चेक बॉक्स पर क्लिक करें"
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="चेक बॉक्स" />
<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="रंग बदलें" />
</RelativeLayout>
चरण 3 - 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 = findViewById<CheckBox>(R.id.checkbox)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
// टेक्स्ट रंग बदलें
checkBox.setTextColor(Color.MAGENTA)
// पृष्ठभूमि रंग बदलें (हेक्स कोड से)
checkBox.setBackgroundColor(Color.parseColor("#cbff75"))
// वैकल्पिक: बटन टिंट बदलें (API 21+)
// checkBox.buttonTintList = ColorStateList.valueOf(Color.RED)
}
}
}
चरण 4 - AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.checkboxcolor">
<application ...>
<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>
ऐप चलाएं
ऐप चलाएं। "रंग बदलें" बटन दबाने पर CheckBox का टेक्स्ट मैजेंटा और पृष्ठभूमि हल्का हरा हो जाएगा।
बेहतर दृष्टिकोण (XML में):app:buttonTint="@color/your_color"औरandroid:textColor="@color/your_color"का उपयोग करके XML में ही रंग सेट करें। यह अधिक कुशल और रखरखाव योग्य है।
Material Components: यदि आप Material Components लाइब्रेरी उपयोग कर रहे हैं (MaterialCheckBox), तोapp:buttonTintऔरandroid:textColorबेहतर काम करते हैं।