Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using strings.xml w/ Android

Tags:

java

android

I've looked and tried many things but it seems my App isn't getting the values for strings in strings.xml. It actually seems like it's passing blank. I think there's an issue initializing.

strings.xml

    <string name="itb_cityID">2</string>
        <string name="itb_city">New York</string>

constants.java excerpt:

public class ConstantData {

public static String cityID="2";
public static String city="New York";

How do I set cityID = R.strings.itb_cityID and city=itb_city the correct way?

like image 274
basemansix8 Avatar asked Nov 30 '22 04:11

basemansix8


1 Answers

String yourString = Context.getResources().getString(R.string.your_string);

Note: You can't use Context statically. If you're inside an Activity, simply use this or call getResources() directly. Otherwise you'll need to obtain a handle to your application's context via getApplicationContext().

like image 182
LeffelMania Avatar answered Dec 09 '22 22:12

LeffelMania