How to add message box with 'OK' button?

AndroidMessagebox

Android Problem Overview


I want to display a message box with an OK button. I used the following code but it results in a compile error with argument:

AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
dlgAlert.setMessage("This is an alert with no consequence");
dlgAlert.setTitle("App Title");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();

How should I go about displaying a message box in Android?

Android Solutions


Solution 1 - Android

I think there may be problem that you haven't added click listener for ok positive button.

dlgAlert.setPositiveButton("Ok",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          //dismiss the dialog  
        }
    });

Solution 2 - Android

Since in your situation you only want to notify the user with a short and simple message, a Toast would make for a better user experience.

Toast.makeText(getApplicationContext(), "Data saved", Toast.LENGTH_LONG).show();

> Update: A Snackbar is recommended now instead of a Toast for Material Design apps.

If you have a more lengthy message that you want to give the reader time to read and understand, then you should use a DialogFragment. (The documentation currently recommends wrapping your AlertDialog in a fragment rather than calling it directly.)

Make a class that extends DialogFragment:

public class MyDialogFragment extends DialogFragment {
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		// Use the Builder class for convenient dialog construction
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("App Title");
		builder.setMessage("This is an alert with no consequence");
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				// You don't have to do anything here if you just 
                // want it dismissed when clicked
			}
		});
		
		// Create the AlertDialog object and return it
		return builder.create();
	}
}

Then call it when you need it in your activity:

DialogFragment dialog = new MyDialogFragment();
dialog.show(getSupportFragmentManager(), "MyDialogFragmentTag");

###See also

enter image description here

Solution 3 - Android

The code compiles ok for me. May be you have forgotten to add the import:

import android.app.AlertDialog;

Anyway, you have a good tutorial here.

Solution 4 - Android

@Override
protected Dialog onCreateDialog(int id)
{
	switch(id)
	{
	case 0:
	{				
		return new AlertDialog.Builder(this)
		.setMessage("text here")
		.setPositiveButton("OK", new DialogInterface.OnClickListener() 
		{					
			@Override
			public void onClick(DialogInterface arg0, int arg1) 
			{
				try
				{
					
				}//end try
				catch(Exception e)
				{
					Toast.makeText(getBaseContext(),  "", Toast.LENGTH_LONG).show();
				}//end catch
			}//end onClick()
		}).create();				
	}//end case
  }//end switch
	return null;
}//end onCreateDialog

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajkumar ReddyView Question on Stackoverflow
Solution 1 - AndroidParesh MayaniView Answer on Stackoverflow
Solution 2 - AndroidSuragchView Answer on Stackoverflow
Solution 3 - AndroidFerranBView Answer on Stackoverflow
Solution 4 - AndroidNdupzaView Answer on Stackoverflow