यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android में ListView आइटम्स के बैकग्राउंड कलर को क्लिक पर कैसे बदला जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: activity_main.xml में ListView जोड़ें
<?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"
android:padding="8dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
चरण 3: MainActivity.kt में क्लिक लिस्टनर जोड़ें
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
class MainActivity : AppCompatActivity() {
var operatingSystem: Array<String> = arrayOf("Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val listView: ListView = findViewById(R.id.listView)
val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, operatingSystem)
listView.adapter = adapter
listView.setOnItemClickListener { _, view, _, _ ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
view.setBackgroundColor(getColor(R.color.colorAccent))
}
}
}
}
चरण 4: colors.xml में कलर परिभाषित करें
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorAccent">#FF4081</color>
</resources>
चरण 5: AndroidManifest.xml कॉन्फ़िगर करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<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>
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर डिफ़ॉल्ट स्क्रीन देखें।
उन्नत विकल्प: सेलेक्टर ड्रॉएबल का उपयोग करके स्टेट-आधारित कलर:
<!-- res/drawable/list_item_selector.xml -->
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/colorAccent" />
<item android:state_selected="true" android:drawable="@color/colorPrimary" />
<item android:drawable="@android:color/transparent" />
</selector>
फिर ListView में: android:listSelector="@drawable/list_item_selector"