How to access a specific line from a resource file using Resource.getString (id) in Android

I want to know that I have a file with a string.xml file that contains status messages. Now I want to process them by their identifier. I wrote the following code, but it does not work on Message = res.getString(Msgid);and logs Resource not found Exception.

Can anyone 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();
            }
+3
source share
2 answers

Suppose you have a value

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

inside strings.xml.

To access this value, indicate in your activity:

this.getText(R.string.msg).toString()
+3
source

you can access it using this method:

YourActivity.this.getResources().getString(R.string.myString);
+1
source

All Articles