How to access strings contained in string.xml

I want to create an alertdialog with a string present in String.xml but R.string.string1 returns an int. Please tell me how to get a string in string.xml format, here is my code ..

   public static void showAlert(String alertMessage) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(null);
    builder.setMessage(alertMessage);              
    final AlertDialog alert = builder.create();
    alert.show();
}

 showAlert(R.string.string1);
+5
source share
3 answers

use getResources().getStringto get string value from strings.xml like:

String str=getResources().getString(R.string.internetNotAvailable);
showAlert(str);

and also you pass null in AlertDialog.Builderto create an AlertDialog. please pass the current activity context instead of null to create AlertDialogas:

AlertDialog.Builder builder = new AlertDialog.Builder(Your_Current_Activity.this);
+3
source
myContext.getResources().getString(R.string.string1);

Please note that myContext- this is anyone Contextyou are currently in or have access to - if you are in Activity, maybe justthis

0

getResources().getString(R.string.string1) sting string.xml

public static void showAlert(String alertMessage) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(null);
    builder.setMessage(alertMessage);              
    final AlertDialog alert = builder.create();
    alert.show();
}

 showAlert(getResources().getString(R.string.string1));
0

All Articles