I have defined a string name and value in the string.json under the element folder. How to access those values in the java code?
Asked
Active
Viewed 48 times
1 Answers
0
Use the following Snippet to access String from strings.json
public static String getString(Context context, int id) {
String result = "";
if (context == null) {
LogUtil.error(TAG, "getString -> get null context");
return result;
}
ResourceManager manager = context.getResourceManager();
if (manager == null) {
LogUtil.error(TAG, "getString -> get null ResourceManager");
return result;
}
try {
result = manager.getElement(id).getString();
} catch (IOException e) {
LogUtil.error(TAG, "getString -> IOException");
} catch (NotExistException e) {
LogUtil.error(TAG, "getString -> NotExistException");
} catch (WrongTypeException e) {
LogUtil.error(TAG, "getString -> WrongTypeException");
}
return result;
}
and Usage would be,
String appName = getString(context, ResourceTable.String_appname);

Gowtham GS
- 478
- 2
- 5