Android AlertDialog Builder

I have an alertdialog that I am showing, but no matter what I do, it shows alertdialog with an empty header and message. The icon, the “Positive” button and the “negative” buttons are displayed with the correct descriptions. Here are the snippets of the code I'm using: In the manifest file:

<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="16" /> 

In my code, I declare:

 import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; 

I also declare the context:

 final Context context = this; 

I place my warning in:

 public void confirm() { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); // set title alertDialogBuilder.setTitle("This is title"); alertDialogBuilder.setIcon(R.drawable.ic_delete); // set dialog message alertDialogBuilder .setMessage("This is the message") .setCancelable(false) .setPositiveButton(R.string.yes,new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, close // current activity MainActivity.this.finish(); } }) .setNegativeButton(R.string.no,new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); } 

Then I call confirmation where I need it, for example:

 confirm(); 

A warning will appear. The icon is set. SetPositiveButton is good and contains the correct description. Set of NegativeButton is good and contains the correct description.

Title is empty Message is empty

Any ideas?

+4
source share
4 answers

you can use

  AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this); builder.setTitle("Modify Customer Details"); 

OR

  Dialog dialog = new Dialog(YourActivity.this); dialog.setTitle("Payment Options"); 
+3
source

Try customizing the message and title in this way. This is from the dialog developer guide

//1. Create an instance of AlertDialog.Builder with your constructor AlertDialog.Builder builder = new AlertDialog.Builder (getActivity ());

// 2. The chain between the various configuration methods for setting the characteristics of the dialog builder.setMessage (R.string.dialog_message) .setTitle (R.string.dialog_title);

// 3. Get AlertDialog from create () AlertDialog dialog = builder.create ();

+2
source

This is related to your topic in context ("this"). You need to check this context. or you have to inflate your own view and call setView to place it.

0
source

Modify alertDialog create alertDialog to:

 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, android.R.style.Theme_Material_Light_Dialog_Alert); 
0
source

All Articles