How to use listview in Dialog ?

1.First declare AlertDialog reference
AlertDialog dialog;

2.Second thing is you have to create an instance for AlertDialog.Builder
AlertDialog.Builder builder=new AlertDialog.Builder(this);

3.Set the title whatever you want and Here important thing is
AlertDialog.Builder should set the view .
4.The following code is in onCreate Method.Here CustomListAdapter is Adapter class which extends BaseAdapter for the custom view.

Button btn=(Button) findViewById(;
 btn.setText("Listview in Dialog");
   btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
   AlertDialog.Builder builder=new AlertDialog.Builder(DialogWithListviewActivity.this);
   ListView list=new ListView(DialogWithListviewActivity.this);
   list.setAdapter(new CustomListAdapter(DialogWithListviewActivity.this));
   list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

     public void onItemClick(AdapterView<?> arg0, View arg1,
        int position, long arg3) {
         // TODO Auto-generated method stub
      Toast.makeText(mContext, "Clicked at Position"+position, Toast.LENGTH_SHORT).show();

5.CustomAdapter should be like this.

<pre>public class CustomListAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflator;
    final int[] images = { R.drawable.magnolia,
            R.drawable.orchid, R.drawable.rose,
    public CustomListAdapter(Context context) {
        // TODO Auto-generated constructor stub
 this.inflator= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    public int getCount() {
        // TODO Auto-generated method stub
        return images.length;

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        MainListHolder mHolder;

        View v = convertView;
        if (convertView == null)
            mHolder = new MainListHolder();

            v = inflator.inflate(R.layout.inflate, null);
            mHolder.image=  (ImageView) v.findViewById(;

        } else {
            mHolder = (MainListHolder) v.getTag();

        mHolder.image.setPadding(20, 20, 20, 20);
        return v;
    class MainListHolder
        private ImageView image;


I hope that this is useful for someone and download example here


One thought on “How to use listview in Dialog ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s