1

I would like to get name from values resource file. for example

values.xml

<string name="ind_ginger">Ginger</string>
<string name="ind_garlic">Garlic</string>

I am using them for the check boxes like

<CheckBox
    android:id="@+id/c01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="21dp"
    android:text="@string/ind_garlic"
    android:layout_alignTop="@+id/c02"
    android:layout_alignRight="@+id/saveChanges"
    android:layout_alignEnd="@+id/saveChanges" />
 <CheckBox
    android:id="@+id/c02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="81dp"
    android:layout_marginStart="81dp"
    android:checked="false"
    android:text="@string/ind_ginger"
    android:visibility="visible"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="40dp" />

In application I need access String Name ( Please note value)

for (CheckBox item : checkBoxList){
     if(item.isChecked())
     {
          //String text=item.getText().toString();String viewID = getResources().getResourceName(item.getId()); // gets me the name 
          String name =  getResources().getResourceEntryName(item.getId());
           String tName  =  
          //item.getText().toString();
          // String id = item.getTag().toString();
          Toast.makeText(getApplicationContext(), tName,Toast.LENGTH_SHORT).show();
          Log.d(viewID, TAG);

      }

  }

Is parsing the XML only way?

4
  • I need the string name like ind_ginger Commented Nov 9, 2017 at 12:02
  • 1
    you can get string from strings.xml by using getResources().getString(R.String.ind_ginger); Commented Nov 9, 2017 at 12:03
  • 1
    As per your explaination you are requesting for Id from String.xml using its value data. But that is not possible. Commented Nov 9, 2017 at 12:28
  • Sorry .. Yes the file name is String.xml Commented Nov 9, 2017 at 12:42

5 Answers 5

1

Try

String ginger = getResources().getString(R.string.ind_ginger)
Sign up to request clarification or add additional context in comments.

10 Comments

what I need is ind_ginger . What I am getting is "Ginger"
May I know what is the rational behind the requirement.?
Not the text string. I need the attribute name like ind_ginger not Ginger (value) <string name="ind_ginger">Ginger</string>
Why don't you create a HashMap with value as the key and name as the value. so that you can get it directly right.?
They are name value pairs. needed for mapping and consistency purpose. And they are too many.
|
1

Easiest way to get the "KEY" name is as following:

Log.e("KEY_NAME", getResources().getResourceEntryName(R.string.app_name));

Here you will get "app_name" as result.

This can be also help in support multi-language support feature.

Comments

0

You can use this to fetch all the String Keys in string.xml

Field[] fields = R.string.class.getFields();
        String[] allStringNames = new String[fields.length];
        for (int  i =0; i < fields.length; i++) {
            allStringNames[i] = fields[i].getName();
            Log.e("String Key Name",""+allStringNames[i]);
        }

Hope this will help

Comments

0

Just Store you string items in (And its better if you store your string file in in strings.xml and not value.xml )

strings.xml

 <string name="ind_ginger">Ginger</string>
    <string name="ind_garlic">Garlic</string>

Comments

0

Firstly if you need ind_ginger you need to change your code from this

<string name="ind_ginger">Ginger</string>

to

<string name="ind_ginger">ind_ginger</string>

and

String ginger = getResources().getString(R.string.ind_ginger)

to get the ind_ginger. But what I can see from your code is you are using .getResourceEntryName(item.getId()) for that you need to create a String arraylist which would have all the itemId from strings.xml and then you can use them with item.get(position).

Here position is the position of the item in your array list.

Comments

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.