Android AlertDialog में कस्टम ListView प्रदर्शित करना
यह उदाहरण दर्शाता है कि एक कस्टम लेआउट के साथ AlertDialog में ListView कैसे दिखाया जाए, जिसमें नीचे एक बटन भी शामिल है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: मेन लेआउट (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"
tools:context=".MainActivity">
<Button
android:id="@+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="क्लिक करें - अलर्ट डायलॉग में ListView देखें"
android:textSize="14sp"
android:textStyle="bold"
android:onClick="openDialog"/>
</RelativeLayout>
चरण 3: डायलॉग के लिए कस्टम रो लेआउट (res/layout/row.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="@id/button"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="सभी देश"/>
</RelativeLayout>
चरण 4: MainActivity (src/MainActivity.java)
import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private String[] countries = {"भारत", "ब्राज़ील", "अर्जेंटीना",
"पुर्तगाल", "फ्रांस", "इंग्लैंड", "इटली"};
private ArrayAdapter adapter;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openDialog(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// कस्टम लेआउट इन्फ्लेट करें
View dialogView = getLayoutInflater().inflate(R.layout.row, null);
listView = dialogView.findViewById(R.id.listView);
// सिंपल एडाप्टर बनाएं
adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, countries);
listView.setAdapter(adapter);
// डायलॉग में कस्टम व्यू सेट करें
builder.setView(dialogView);
AlertDialog dialog = builder.create();
dialog.show();
}
}
चरण 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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>
मुख्य बिंदु
- AlertDialog.Builder: डायलॉग बनाने के लिए मुख्य क्लास
- setView(View): कस्टम लेआउट सेट करने के लिए
- LayoutInflater.inflate(): XML लेआउट को View ऑब्जेक्ट में बदलने के लिए
- ArrayAdapter: सिंपल स्ट्रिंग ऐरे को ListView में बाइंड करने के लिए
ऐप चलाएं
ऐप लॉन्च करें, बटन दबाएं, और देशों की सूची वाला अलर्ट डायलॉग देखें।

