avishag Cs
avishag Cs

Reputation: 19

Diaplay a popup window on parent fragment

I have a tab activity. 1 of its tab it contains a listView which contains fragment items. I have a button on these list items (which are fragments), and I want whenerver I click it to show a popup window. Unfortunatly I can see the popup, and I assume it's because the display ot the list item is too small. I would actually like to see it on the activity which contains the list.

Any ideas?

edit: The dialog:

public class CancelRunPupUp extends DialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(getResources().getString(R.string.cancelRunTitle))
            .setMessage(getResources().getString(R.string.AreYouSure))
            .setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    AlertDialog dialog = builder.create();
    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    return dialog;
}

}

The button which calls this dialog:

In public class RowInHomeTab extends Fragment:

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

    // Inflate the layout for this fragment

    view = inflater.inflate(R.layout.fragment_row_in_home_tab, container, false);

    ImageButton cancelRun = (ImageButton) view.findViewById(R.id.cancelRunButton);

    cancelRun.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            CancelRunPupUp dialog = new CancelRunPupUp();
            dialog.show(getFragmentManager(), "cancelRunDialog");
        }
    });
    return view;
}

RowInHomeTab is an item in a listView which is placed in a tabActivity.

Upvotes: 0

Views: 1241

Answers (2)

2red13
2red13

Reputation: 11227

Might be al little bit "oversized" but you can use System Alert:

alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

But this requires

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

edit: ok, seems that you already set the type to SYSTEM_ALERT, then I have no other idea but store the Context of the activity you want the Dialog to be shown and use this global var instead of getActivity() in your Dialog Class. But this is very sticky... The activity containing the List view needs:

public static Context main_ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
    main_ctx = this;
    [...]

And your Dialog Fragment:

public Dialog onCreateDialog(Bundle savedInstanceState) {

     AlertDialog.Builder builder = new AlertDialog.Builder([YOUR CLASS NAME].main_ctx);

edit:

Last try

getActivity().runOnUiThread(new Runnable() {
            public void run() {
               CancelRunPupUp dialog = new CancelRunPupUp();
               dialog.show(getFragmentManager(), "cancelRunDialog");  
            }
        });

Upvotes: 0

Muhammad Jamal
Muhammad Jamal

Reputation: 442

Use this Dialog Fragment to popup the window ....

                DFragment dFragment = new DFragment();
            // Show DialogFragment
            dFragment.show(fm, "Dialog Fragment");

Upvotes: 0

Related Questions