परिचय
यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android ListView में कस्टम फुटर व्यू कैसे जोड़ा जाए।
चरण 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:padding="4dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ListView>
</RelativeLayout>
चरण 3: Kotlin कोड लागू करें
src/MainActivity.kt में निम्नलिखित कोड जोड़ें:
import android.os.Bundle
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var listView: ListView
private val country = arrayOf(
"भारत", "ऑस्ट्रेलिया", "दक्षिण अफ्रीका",
"उत्तरी अमेरिका", "दक्षिण अमेरिका", "आइसलैंड", "ग्रीनलैंड"
)
private lateinit var listString: MutableList<String>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
listView = findViewById(R.id.listView)
listString = country.toMutableList()
val adapter = ArrayAdapter(
this,
android.R.layout.simple_list_item_1,
listString
)
// फुटर लेआउट इनफ्लेट करें
val layoutInflater = layoutInflater
val footer = layoutInflater.inflate(R.layout.listview_footer, listView, false) as ViewGroup
listView.addFooterView(footer)
listView.adapter = adapter
}
}
चरण 4: फुटर लेआउट बनाएं
res/layout/listview_footer.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="match_parent"
android:layout_height="wrap_content"
android:padding="16sp"
android:text="फुटर: ListView का अंत!"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
चरण 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 आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप का डिफॉल्ट स्क्रीन देखें।
नोट
RecyclerView का उपयोग करने पर, फुटर के लिए अलग व्यू टाइप और एडैप्टर में लॉजिक की आवश्यकता होती है। ListView में addFooterView() बिल्ट-इन सपोर्ट प्रदान करता है।