Mak
Mak

Reputation: 1063

How can access particular String from Resource file with using Resource.getString(id) in Android

I want to know that I have the string.xml Resource file which contains status messages. Now I want to process them according their id. I have written the following code but it fails on Message = res.getString(Msgid); and logs Resource not found Exception.

Can any one help me?

public void FileStatusMsg(int Msgid)


        {
                String Message = null;
                Resources res = this.getResources();


                Message = res.getString(Msgid);

                //Display Status Messages..
                AlertDialog.Builder builder = new AlertDialog.Builder(OfficeLinQ.this);
                builder.setMessage(Message).setCancelable(false).setPositiveButton(
                        "OK", new DialogInterface.OnClickListener() 
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }

Upvotes: 3

Views: 1466

Answers (2)

Houcine
Houcine

Reputation: 24181

you can access it by using this method :

YourActivity.this.getResources().getString(R.string.myString);

Upvotes: 1

Anju
Anju

Reputation: 9479

Suppose you have a value

<string name="msg">Message</string>

inside strings.xml.

To access this value, in your activity give:

this.getText(R.string.msg).toString()

Upvotes: 3

Related Questions