Search code examples
javaandroidandroid-dialogfragmentshowdialog

How do I show a DIalog Fragment?


Hi I wanted to know how I could show a dialog on screen when the user clicks on a button;

java file;

public class Example extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.example_layout);

        findViewById(R.id.exampleButton).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.i("Jomin","sdsd");
                ExampleDialogFragment CallUs = new ExampleDialogFragment();
                CallUs.getDialog();
            }
        });
    }
}

this the ExampleDialogFragment java file;

public class ExampleDialogFragment extends android.support.v4.app.DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setCancelable(false);      builder.setTitle(R.string.LogOutTitle).setMessage(R.string.LogOutMessage).setPositiveButton(R.string.LogOutPositive,new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                Uri number = Uri.parse("tel:0845 3000 116");
                Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
                startActivity(callIntent);
            }
        });

        AlertDialog dialog = builder.create();
        return dialog;
    }
}

So can someone tell me how I would be able to show this dialog when the user clicks on button with the ID - exampleButton.


Solution

  • callUs.show(getFragmentManager(), "SOME_TAG");
    

    And I think you have to switch your activity to FragmentActivity and repalce getFragmentManager() with getSupportFragmentManager() as your DialogFragment extends version from Support Library