Reputation: 89
Is it possible to make a dialog with content from a string defined in your OnCreate? Because the only way to define the content of the dialog, which I could find, is with text or a string defined in values/strings.xml like this:
builder.setMessage(R.string.dialog_fire_missiles)
I hope there is someone who can help me, please tell me if my question is clear enough.
Upvotes: 5
Views: 9070
Reputation: 5947
Use getString method
ProgressDialog pDialog;
pDialog.setMessage(getString(R.string.your_xml_string));
Upvotes: 2
Reputation: 253
Get the resources, such as:
builder.setMessage(getRessources.getString(R.string.dialog_fire_missiles))
Upvotes: 0
Reputation: 12304
Yes, you can use strings o_O
Check documentation first: setMessage (CharSequence message)
Then you can check the tutorial test dialog here
And for lazy ones:
String text = "Hello";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(text).setPositiveButton("Wohoo", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
And at the end add this, because you have created dialog and it needs to be show:
builder.show();
Upvotes: 0
Reputation: 28599
getApplicationContext().getString(R.string.yourstring);
Your activity context give you everything you need to load resources from 'R'.
Upvotes: 0
Reputation: 82553
Just use:
builder.setMessage("Your Message")
There are two setMessage() methods, one for accepting a CharSequence, and one for accepting an R.string.*.
Upvotes: 2
Reputation: 132982
use getString
method of Context
to get String from strings.xml:
builder.setMessage(Your_Activity.this.getString(R.string.dialog_fire_missiles))
Upvotes: 10