user1946633
user1946633

Reputation: 89

How to make a dialog with content from a string?

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

Answers (6)

Vladimir Salguero
Vladimir Salguero

Reputation: 5947

Use getString method

ProgressDialog pDialog;

pDialog.setMessage(getString(R.string.your_xml_string));

Upvotes: 2

user1868632
user1868632

Reputation: 253

Get the resources, such as:

builder.setMessage(getRessources.getString(R.string.dialog_fire_missiles))

Upvotes: 0

deadfish
deadfish

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

Matt Clark
Matt Clark

Reputation: 28599

getApplicationContext().getString(R.string.yourstring);

Your activity context give you everything you need to load resources from 'R'.

Upvotes: 0

Raghav Sood
Raghav Sood

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

ρяσѕρєя K
ρяσѕρєя K

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

Related Questions