परिचय
यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके एंड्रॉइड ऐप में स्पिनर के टेक्स्ट साइज और कलर को कस्टम लेआउट के माध्यम से कैसे बदला जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: मुख्य लेआउट XML कॉन्फ़िगर करें
res/layout/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"
android:gravity="center"
tools:context=".MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
चरण 3: Kotlin कोड लागू करें
src/MainActivity.kt में निम्नलिखित कोड जोड़ें:
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var spinner: Spinner
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
spinner = findViewById(R.id.spinner)
val superHero = arrayOf("बैटमैन", "सुपरमैन", "फ्लैश", "एक्वामैन", "शाज़म")
// कस्टम लेआउट का उपयोग करके एडैप्टर बनाएं
val arrayAdapter = ArrayAdapter(this, R.layout.spinner_list, superHero)
arrayAdapter.setDropDownViewResource(R.layout.spinner_list)
spinner.adapter = arrayAdapter
}
}
चरण 4: कस्टम स्पिनर आइटम लेआउट बनाएं
res/layout/spinner_list.xml नाम से एक नया लेआउट रिसोर्स फाइल बनाएं:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@android:color/holo_red_light"
android:textSize="16sp"
android:textStyle="bold|italic" />
चरण 5: मैनिफेस्ट कॉन्फ़िगर करें
androidManifest.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.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 Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप का डिफॉल्ट स्क्रीन देखें।
अतिरिक्त: ड्रॉपडाउन और सेलेक्टेड व्यू के लिए अलग लेआउट
यदि आप ड्रॉपडाउन और चयनित आइटम के लिए अलग-अलग स्टाइल चाहते हैं:
// सेलेक्टेड व्यू के लिए val adapter = ArrayAdapter(this, R.layout.spinner_selected_item, items) // ड्रॉपडाउन के लिए adapter.setDropDownViewResource(R.layout.spinner_dropdown_item)