यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android ListView के रंग और फ़ॉन्ट को कैसे अनुकूलित किया जाए।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और सभी आवश्यक विवरण भरें।
चरण 2 - activity_main.xml लेआउट जोड़ें
res/layout/activity_main.xml में निम्न कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:textColor="@android:color/holo_orange_dark"
android:textSize="24sp"
android:textStyle="italic|bold" />
</LinearLayout>
चरण 3 - MainActivity.kt में कोड जोड़ें
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private var operatingSystem = arrayOf(
"Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Mac 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 = object : ArrayAdapter<String>(
this, R.layout.list_item, R.id.textView, operatingSystem
) {}
listView.adapter = adapter
}
}
चरण 4 - list_item.xml लेआउट बनाएं
एक नया Layout Resource File (list_item.xml) बनाएं और निम्न कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:textColor="@android:color/holo_orange_dark"
android:textSize="24sp"
android:textStyle="italic|bold" />
</LinearLayout>
चरण 5 - AndroidManifest.xml कॉन्फ़िगर करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.listviewdemo">
<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 आइकन पर क्लिक करें, अपना डिवाइस चुनें और ऐप लॉन्च करें। ListView आइटम अब नारंगी रंग में, कंडेंस्ड फ़ॉन्ट में, बोल्ड और इटैलिक स्टाइल में दिखाई देंगे।