Android simple alert dialog

LS_ picture LS_ · Sep 29, 2014 · Viewed 277.8k times · Source

I need to show a little text message to the users that clicks a button on my Android app, on IOS I just had to create an AlertView that it's simple to use but with Android i'm struggling because the solution seems x10 times harder. I saw that I need to use a DialogFragment but I can't understand how to make it work, can someone explain? Also, is my solution right or there is something easier to show a simple text message to users?

Answer

MysticMagicϡ picture MysticMagicϡ · Sep 29, 2014

You would simply need to do this in your onClick:

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
alertDialog.show();

I don't know from where you saw that you need DialogFragment for simply showing an alert.

Hope this helps.