Android DialogFragment Containing ListView

By | June 4, 2014

In this previous tutorial we have seen an example of how to create dialog fragment , In this post i am trying to extend and show how to display a list-view inside the dialog fragment window .

XML Layout

Create Two XML Layout , one for main activity and another for the which is shown as dialog .

  1.  activity_main.xml .
  2.  dialog_fragment.xml.

file :activity_main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout 
    xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    android:padding=”10dp” >

    <Button
        android:id=”@+id/button1″
        android:layout_width=”match_parent”
        android:layout_height=”wrap_content”
        android:text=”Show List Dialog”
        android:onClick=”showDialog” />

</LinearLayout>

file :dialog_fragment.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout 
    xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    android:layout_marginLeft=”5dp”
    android:layout_marginRight=”5dp” >

    <ListView
        android:id=”@+id/list”
        android:layout_width=”match_parent”
        android:layout_height=”match_parent” >
    </ListView>

</LinearLayout>

Dialog Fragment

  • To Create a dialog fragment your fragment should extend DialogFragment class .
  • Inside the onCreateView() method inflate the layout of this dialog with above defined dialog_fragment.xml layout .
  • Inside the onActivityCreated() method i am setting the array-adapter for the list view of dialog_fragment.xml layout .
  • Set the onItemClick() Listener for list item , when the user clicks items dismiss the dialog and show the toast message.

file : MyDialogFragment.java

package com.pavan.dialogfragmentdemo;

import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyDialogFragment extends DialogFragment implements
        OnItemClickListener {

    String[] listitems = { “item01”, “item02”, “item03”, “item04” };

    ListView mylist;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.dialog_fragment, null, false);
        mylist = (ListView) view.findViewById(R.id.list);

        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, listitems);

        mylist.setAdapter(adapter);

        mylist.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        dismiss();
        Toast.makeText(getActivity(), listitems[position], Toast.LENGTH_SHORT)
                .show();
    }

}

MainActivity

file : MainActivity.java

package com.pavan.dialogfragmentdemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

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

    public void showDialog(View view) {
      
        FragmentManager manager = getFragmentManager();

        MyDialogFragment dialog = new MyDialogFragment();
        dialog.show(manager, “dialog”);

    }

}