1. Are you ready for the Galaxy S20? Here is everything we know so far!

How do you show a dialog when in a fragment?

Discussion in 'Android Development' started by nleigh, Aug 1, 2013.

  1. nleigh

    nleigh Lurker
    Thread Starter

    1st post so not sure if im in the right section, been looking online but cant find a solution. The app is a menu for a restaurant want to navigat between mains/starters...

    Im using a view pager to be able to slide to different tabs.
    Each of the pages are a fragment class. Ive put a button in one of them and when I press it I just want to show a simple yes/no dialog box.

    Im just using this for the dialog fragment

    package com.example.pie;

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

    public class PieChoiceDialogFragment extends DialogFragment {

    //public static PieChoiceDialogFragment newInstance() {
    // PieChoiceDialogFragment frag = new PieChoiceDialogFragment();
    // return frag;
    // }
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Pie Choice")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    // User cancelled the dialog
    // Create the AlertDialog object and return it
    return builder.create();


    this is my fragment that im trying to show the dialog when the button is pressed

    package com.example.pie;

    import android.app.FragmentTransaction;
    import android.os.Bundle;
    import android.support.v4.app.DialogFragment;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class StartersFragment extends Fragment{

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.starters_fragment,
    container, false);

    return rootView;
    public void onClick_buttonDialog(){
    PieChoiceDialogFragment df = new PieChoiceDialogFragment();

    df.show(getFragmentManager(), "dialog");


    But I get the error in the df.show bit
    The method show(FragmentManager, String) in the type DialogFragment is not applicable for the arguments (FragmentManager, String)

    I dont know how to fix this, any help appreciated thanks :)


Share This Page