0

I have 2 activities, one with buttons which have ID's. Depending on which button you click, an ID is passed to a second activity through it's intent. I then in that second activity want to retrieve string values based on the id of that button. I had named my string values as followed:

<resources>
    <string name="app_name">DataApp</string>
    <string name="info_title_zalm">Zalm</string>
    <string name="info_weight_zalm">1kg</string>
    <string name="info_origin_zalm">Atlantische Oceaan</string>
    <string name="info_title_haai">Haai</string>
    <string name="info_weight_haai">200kg</string>
    <string name="info_origin_haai">Atlantische Oceaan, zoet water uitzonderingen</string>
</resources>

So the string have a shared start of the name, for example info_title_zalm and info_title_haai. When clicking the haai button a "haai" string is passed to the second activity, where I was planning to do something such as "info_title_" + string to get the corresponding string. But because of needing to retrieve it as R.string.info_title_haai this doesn't work.

Is such a thing possible in Android?

Using Android Studio, building for Jellybean and above.

3
  • You can pass the int id (R.string.info_title_haai) instead of the value. Thus the second activity can obtain the value by getString(extraID) Commented Feb 18, 2015 at 12:18
  • oh wait. do you want to know the string name? For what? Commented Feb 18, 2015 at 12:19
  • but how do I build R.string.info_title_haai when it needs to depend on what button is pressed? I want to make it so that pressing the button decides which suffix is added and which strings appear. Commented Feb 18, 2015 at 12:19

3 Answers 3

4

I use this:

int resId = getResources().getIdentifier("info_title_" + string, "string", getPackageName());
String s = getString(resId);

Here you have more info

Sign up to request clarification or add additional context in comments.

Comments

2

You can use a some kind of wrapper method like this (untested):

private String getStringResourceByName(String myResourceName) {
    String packageName = getPackageName();
    int resId = getResources()
            .getIdentifier(myResourceName, "string", packageName);
    if (resId == 0) {
        return myResourceName;
    } else {
        return getString(resId);
    }
}

Which will get values depending on matches to myResourceName - Then you can pass in whatever you like.

Comments

1

If you only have two options, you could pass the ID's of all the resources you need to via the intent.

If this table will grow larger, I think you're better off by packing a small database with your application.

1 Comment

You are right in that I would probably have to work with a small db if this would be larger, but for the time being I am just testing a concept with small amounts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.