Reputation: 51
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?
Upvotes: 1
Views: 1442
Reputation: 388
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.
Upvotes: 0
Reputation: 4292
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.
Upvotes: 1
Reputation: 9225
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>
Upvotes: 0
Reputation: 223
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
Upvotes: 0