I have an alertdialog that I show but no matter what I do the alertdialog shows with a blank Title and Message. The Icon, the Positive button and negative buttons show ok with correct descriptions. Here is the snippets of code that I use: 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 context:
final Context context = this;
I place my alert 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();
}
I then call confirm from where I need it, like so:
confirm();
The alert shows up ok. The icon is set The setPositiveButton is good and contains correct description The setNegativeButton is good and contains correct description
The Title is blank The Message is blank
Any ideas?
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");
Try setting the message and title this way. This from the developer's guide on Dialogs
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
// 3. Get the AlertDialog
from create()
AlertDialog dialog = builder.create();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With