menu_on_top
menu_on_top

Reputation: 2613

get data from the strings.xml

How can I get text from the strings.xml file into my .setmessage?

show = new AlertDialog.Builder(mContext).setTitle("moria")
        .setMessage("R.string.erroroik")
        .setPositiveButton("OK", null).show();

Upvotes: 5

Views: 14877

Answers (6)

Abeer Sul
Abeer Sul

Reputation: 984

Try this

String x= getResources().getString(R.string.xxxxx);

Upvotes: 1

thhVictor
thhVictor

Reputation: 338

Solution 1

Context context;
show = new AlertDialog.Builder(mContext).setTitle("moria")
    .setMessage(context.getString(R.string.erroroik))
    .setPositiveButton("OK", null).show();

Solution 2

show = new AlertDialog.Builder(mContext).setTitle("moria")
    .setMessage(getString(R.string.erroroik))
    .setPositiveButton("OK", null).show();

Upvotes: 3

Robby Pond
Robby Pond

Reputation: 73484

The ids in the xml resource files are actually integer values not strings.

show = new AlertDialog.Builder(mContext).setTitle("moria")
                    .setMessage(R.string.erroroik)
                            .setPositiveButton("OK", null).show();

Upvotes: 3

David Kuridža
David Kuridža

Reputation: 7197

You can access it through context, depending where exactly this DialogBuilder is, it can be

context.getString(R.string.erroroik);

or

this.getString(R.string.erroroik);

Take a look at String Resources for more information.

Upvotes: 9

Wroclai
Wroclai

Reputation: 26925

Use R.string.yourText without "" since R.string.yourText is referring to an int declared as static in your R.java.

Upvotes: 6

Blundell
Blundell

Reputation: 76458

show = new AlertDialog.Builder(mContext).setTitle("moria")
                    .setMessage(R.string.erroroik)
                            .setPositiveButton("OK", null).show();

Done

Upvotes: 3

Related Questions