Adel Nizamuddin
Adel Nizamuddin

Reputation: 821

ListFragment as DialogFragment

Is it possible to show ListFragment as Dialog? Or there's no way and I should implement my own ListView, empty TextView and indeterminate ProgressBar inside DialogFragment myself?

Upvotes: 1

Views: 3100

Answers (2)

Hpsaturn
Hpsaturn

Reputation: 2742

Another Option:

Build Dialog Fragment:

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;    

public class ListDialogFragment extends DialogFragment {

    private OnListDialogItemSelect listener;
    private String title;
    private String[] list;

    public ListDialogFragment(OnListDialogItemSelect listener, String[] list, String title) {
        this.listener=listener;
        this.list=list;
        this.title=title;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return new AlertDialog.Builder(getActivity())
                .setTitle(title)
                .setCancelable(false)
                .setItems(list, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {

                        listener.onListItemSelected(list[item]);
                        getDialog().dismiss(); 
                        ListDialogFragment.this.dismiss();

                    }
                }).create();

    }

    public interface OnListDialogItemSelect{
        public void onListItemSelected(String selection);
    }

}

On Your Fragment Activity like this:

public class YourActivity extends FragmentActivity implements OnListDialogItemSelect{

    private void showCountryFragment(){
         FragmentManager fm = getSupportFragmentManager();
         ListDialogFragment newFragment = new ListDialogFragment(this,getCountries(),"Country:");
         newFragment.show(fm, "country_picker");
    }

    @Override
    public void onListItemSelected(String selection) {
        _bt_country.setText(selection);
    }

}

Upvotes: 7

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

I am not very sure whether it works with ListFragment or not but we can show an activity as Dialog by applying a theme to activity in manifest file as:

<activity android:theme="@android:style/Theme.Dialog" />

Please try with your ListFragment, and let me know if it works.

Thank you.

Upvotes: 1

Related Questions