Search code examples
androiddialogandroid-dialogfragment

Converting Dialog into DialogFragments


I have a very nice dialog that does what I need. But I need to update it to the new standards.

this is my current dialog:

public class MyProgressDialog extends Dialog {

    private MyProgressDialog dialog;

    public MyProgressDialog(Context context) {
        super(context);
    }

    public MyProgressDialog show(Context context) {
        dialog = new MyProgressDialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.progress);
        dialog.getWindow().setBackgroundDrawableResource(
                android.R.color.transparent);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        return dialog;
    }

    public void dismiss(Context context) {

        dialog.dismiss();

    }

}

I use this NON-Fragment Dialog already with Fragments and ListFragments (using support library 4) as ProgressDialogs. Unfortunately, on pages with Multiple fragments, they just stack on top of each other. So I want to associate each dialog with each fragment.

I call the dialog this way in the onPreExecute() of each of my Task's:

dialog = new MyProgressDialog(getActivity());

Is it possible to tweak my current setup to work as I need, or do I need to completely redo this from the ground up? I've looked at all the samples from Google and I can't find much that even look close to what I am doing. Can I have some guidance n this transition? Whether it is links, code samples, or ideas to research?

EDIT in response to comment below:

@Override
public Fragment getItem(int position) {
    Fragment f = null;
    switch (position) {
    case 0: {
        f = new RateFragment();
        Bundle args = new Bundle();
        f.setArguments(args);
        break;
    }
    case 1: {
        f = new ReviewFragment();
        Bundle args = new Bundle();

        f.setArguments(args);
        break;
    }
    default:
        throw new IllegalArgumentException("not this many fragments: "
                + position);
    }

    return f;

}

Tablet Layout for this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frags"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/cat_frag"
        android:layout_width="450dp"
        android:layout_height="match_parent"
        class="com.---.---.RateFragmentActivity$RateFragment" />

    <LinearLayout
        android:layout_width="4dp"
        android:layout_height="match_parent"
        android:background="#222" />

    <fragment
        android:id="@+id/featured_frag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.---.---.RateFragmentActivity$ReviewFragment" />

</LinearLayout>

Solution

  • So I want to associate each dialog with each fragment.

    If you want to show the dialogs above the corresponding fragments that triggered the work then you can't use a DialogFragment as this will place the dialog as a normal dialog.

    Instead you could use a normal Fragment shaped like your custom ProgressDialog and attach that to the container(which should be a RelativeLayout or FrameLayout) on top of the normal fragment/content.

    Is it possible to tweak my current setup to work as I need, or do I need to completely redo this from the ground up?

    If those pages with multiple fragments place those fragments in wrapper layouts then it's pretty easy. If not you'll need to make some changes that could be easy or very hard to make depending on how you built the current layouts.