কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েড এলার্ট ডায়ালগে ListView প্রদর্শনের পদ্ধতি

AlertDialog-এ ListView দেখানো

এই উদাহরণটি দেখায় কীভাবে একটি কাস্টম লেআউটে ListView রাখে AlertDialog-এ প্রদর্শন করা যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project থেকে প্রজেক্ট তৈরি করুন।

ধাপ ২: মূল লেআউট (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:textSize="12sp"
        android:textStyle="bold"
        android:onClick="openDialog"
        android:text="Click here to get ListView in Alert Dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>

ধাপ ৩: 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 {
    String[] names = {"India", "Brazil", "Argentina", "Portugal", "France", "England", "Italy"};
    ArrayAdapter adapter;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void openDialog(View v) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        
        // কাস্টম লেআউট ইনফ্লেট করুন
        View rowList = getLayoutInflater().inflate(R.layout.row, null);
        listView = rowList.findViewById(R.id.listView);
        
        // অ্যাডাপ্টার সেট করুন
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names);
        listView.setAdapter(adapter);
        
        alertDialog.setView(rowList);
        AlertDialog dialog = alertDialog.create();
        dialog.show();
    }
}

ধাপ ৪: ডায়ালগের লেআউট (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"
    android:padding="16dp">
    
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/button"
        android:layout_alignParentTop="true"/>
    
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="ALL COUNTRIES" />
</RelativeLayout>

উন্নত সংস্করণ: DialogFragment ব্যবহার (প্রস্তাবিত)

আধুনিক অ্যান্ড্রয়েড ডেভেলপমেন্টে AlertDialog.Builder-এর বদলে DialogFragment ব্যবহার করা ভালো:

public class ListDialogFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = getLayoutInflater().inflate(R.layout.row, null);
        ListView listView = view.findViewById(R.id.listView);
        String[] names = getResources().getStringArray(R.array.countries);
        ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), 
            android.R.layout.simple_list_item_1, names);
        listView.setAdapter(adapter);
        builder.setView(view);
        return builder.create();
    }
}

ধাপ ৫: 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>

অ্যাপ্লিকেশন টেস্ট করুন

মেনু বাটনে ক্লিক করলে একটি ডায়ালগ খুলবে যেখানে দেশের নামের তালিকা দেখাবে। লিস্ট ভিউ স্ক্রল করা যাবে এবং বাটনে ক্লিক করলে ডায়ালগ বন্ধ হবে।

  1. অ্যান্ড্রয়েড ListView-এ ফুটার যোগ করার পদ্ধতি

  2. How to show alert dialog in Android?

  3. অ্যান্ড্রয়েডে Toast মেসেজ দেখানোর পদ্ধতি

  4. অ্যান্ড্রয়েডে একটি অ্যাক্টিভিটি শুরু করার আগে প্রোগ্রেস ডায়ালগ কীভাবে দেখাবেন?