0

Given the strings.xml posted belwo, how to get the identifier of the string given its value? in other words, let assume we have the View posted below, we can get the value set to the Button as follows:

mBtnDecisionAccepted = (Button) findViewById(R.id.actMain_btn_yes);
mBtnDecisionAccepted.getText().toString()

But is there any method which can return the identifier of the string which is

decision_accepted

layout:

<Button
                android:id="@+id/actMain_btn_yes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/decision_accepted" />

strings.xml:

<resources>
    <string name="app_name">Test-TraverseThroughAView-1</string>
    <string name="decision_accepted">eng: yes</string>
    <string name="decision_rejected">eng: no</string>
    <string name="decision_postponed">eng: change</string>
</resources>
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • 2
    Possible duplicate of [Android: How do I get string from resources using its name?](https://stackoverflow.com/questions/7493287/android-how-do-i-get-string-from-resources-using-its-name) – Elias Fazel Jul 02 '17 at 09:47
  • @LetsamrIt did you get this working...if solution was provided by answer below can you please accept it so others will know it's been answered. – John O'Reilly Jan 25 '18 at 17:20

2 Answers2

0
  int resId = getResources().getIdentifier(youString, "string", packageName);
John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
0

you should use context.getResources().getIdentifier();

  private String getStringResourceByName(String aString) {
    String packageName = getPackageName();
    int resId = getResources().getIdentifier(aString, "string", packageName);
    return getString(resId);
    }
Elias Fazel
  • 2,093
  • 16
  • 20