Storing R.drawable IDs in XML array - android

I would like to store drawable resources' ID in the form of R.drawable.* inside an array using an XML values file, and then retrieve the array in my activity.
Any ideas of how to achieve this?

You use a typed array in arrays.xml file within your /res/values folder that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="random_imgs">
<item>#drawable/car_01</item>
<item>#drawable/balloon_random_02</item>
<item>#drawable/dog_03</item>
</integer-array>
</resources>
Then in your activity, access them like so:
TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);
// get resource ID by index, use 0 as default to set null resource
imgs.getResourceId(i, 0)
// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, 0));
// recycle the array
imgs.recycle();

In the value folder create xml file name it arrays.xml
add the data to it in this way
<integer-array name="your_array_name">
<item>#drawable/1</item>
<item>#drawable/2</item>
<item>#drawable/3</item>
<item>#drawable/4</item>
</integer-array>
Then obtain it to your code this way
private TypedArray img;
img = getResources().obtainTypedArray(R.array.your_array_name);
Then to use a Drawable of these in the img TypedArray for example as an ImageView background use the following code
ImageView.setBackgroundResource(img.getResourceId(index, defaultValue));
where index is the Drawable index.
defaultValue is a value you give if there is no item at this index
For more information about TypedArray visit this link
http://developer.android.com/reference/android/content/res/TypedArray.html

You can use this to create an array of other resources, such as drawables. Note that the array is not required to be homogeneous, so you can create an array of mixed resource types, but you must be aware of what and where the data types are in the array.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="icons">
<item>#drawable/home</item>
<item>#drawable/settings</item>
<item>#drawable/logout</item>
</array>
<array name="colors">
<item>#FFFF0000</item>
<item>#FF00FF00</item>
<item>#FF0000FF</item>
</array>
</resources>
And obtain the resources in your activity like this
Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);
TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);
Enjoy!!!!!

In Kotlin, you can do as:-
<integer-array name="drawer_icons">
<item>#drawable/drawer_home</item>
</integer-array>
You will get array of Image from the resource as TypedArray
val imageArray = resources.obtainTypedArray(R.array.drawer_icons)
get resource ID by the index
imageArray.getResourceId(imageArray.getIndex(0),-1)
OR you can set imageView's resource to the id
imageView.setImageResource(imageArray.getResourceId(imageArray.getIndex(0),-1))
and in last recycle the array
imageArray.recycle()

kotlin way could be this:
fun Int.resDrawableArray(context: Context, index: Int, block: (drawableResId: Int) -> Unit) {
val array = context.resources.obtainTypedArray(this)
block(array.getResourceId(index, -1))
array.recycle()
}
R.array.random_imgs.resDrawableArray(context, 0) {
mImgView1.setImageResource(it)
}

Related

Android - How to store array of classes in res xml

I want to store list of classes inside an xml on my res, so I can use it globally just using the index.
<resources>
<array name="array_class">
<class>Dashboard.class</class>
</array>
</resources>
in my activity
int index = x; // i get the the index from database
TypedArray arrayClass = getActivity().getResources()
.(R.array.array_class);
Class myClass = (Class) arrayClass.getString(index);
It throw me an error, cannot cast String to Class.
Yeah I know, I cannot do that. So how the correct way to store array of classes on res? or any alternative way to call Class[] value globally??
Inside your resource file store full name of all classes including package name
<resources>
<array name="array_class">
<class>com.example.Dashboard</class>
<class>com.example.YourOtherClass</class>
</array>
</resources>
Then in Java code, you can retrieve by the following method
try {
int index = x; // i get the the index from database
TypedArray arrayClass = getActivity().getResources().(R.array.array_class);
Class<?> act = Class.forName(arrayClass.getString(index));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Hope it helps

How to get dynamically images from strings.xml

Hello I have added some images in drawable folder and after that I have refer that in strings.xml
Now I want to get dynamically the images so how I can do that, I found this code but for this code I need to give the id of image manually
this is strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="images">
<item>#drawable/one</item>
<item>#drawable/two</item>
<item>#drawable/three</item>
<item>#drawable/four</item>
<item>#drawable/five</item>
</string-array>
</resources>
and this is the java code
TypedArray images = getResources().obtainTypedArray(R.array.images);
images .getResourceId(i, -1)
mImgView1.setImageResource(imgs.getResourceId(i, -1));
imgs.recycle();
String names[] = getResources().getStringArray(R.array.fi);
for (String string : names) {
}
or
for(int i=0; i<names.length ;i++){
}
Like this you can access all the elements in the array

getResourceEntryName() :can i manage to get "R.id.+getresourceentryname(R.string.myString)"?

im trying to resolve the following problem : i named my string resource like that in my xml string file :
<string name="array_nature">Nature</string>
<string name="array_architecture">Architecture</string>
<string name="array_arts">Art/Literature</string>
<string name="array_cinema">Cinema/TV/Radio</string>
<string name="array_geography">Geographie</string>
<string name="array_leisure">Loisirs</string>
<string name="array_music">Musique</string>
<string name="array_people">People</string>
<string name="array_sciences">Sciences</string>
<string name="array_technology">Technologie</string>
<string-array name="array_category">
<item>#string/array_nature</item>
<item>#string/array_architecture</item>
<item>#string/array_arts</item>
<item>#string/array_cinema</item>
<item>#string/array_geography</item>
<item>#string/array_leisure</item>
<item>#string/array_music</item>
<item>#string/array_people</item>
<item>#string/array_sciences</item>
<item>#string/array_technology</item>
</string-array>
i also have string-arrays corresponding to those two strings :
<string-array name="array_nature">
<item>Species</item>
<item>Plant</item>
<item>Animal</item>
<item>CelestialBody</item>
<item>Asteroid</item>
<item>Galaxy</item>
<item>Planet</item>
<item>Satellite</item>
<item>Star</item>
</string-array>
<string-array name="array_architecture">
<item>AmusementParkAttraction</item>
<item>Building</item>
<item>MilitaryStructure</item>
<item>Infrastructure</item>
<item>Airport</item>
<item>RouteOfTransportation</item>
<item>Bridge</item>
<item>HistoricPlace</item>
<item>Monument</item>
<item>Park</item>
<item>Zoo</item>
</string-array>
as you can see, the attribute name of my string (name = "array_nature") is exactly the same as the string-array name.
My goal is to avoid an annoying if, else if, else if ... code in my activity... So is that possible to get Something like that in my java code?
int myStringArray = R.id.+getResourceEntryName(R.string.array_nature);
and i stuck in this method
public void getchoicearray() {
Spinner sp = ((Spinner)findViewById(R.id.spinner_category));
int id = getResources().getIdentifier(getResources().getResourceEntryName(sp.getSelectedItem()./*how to get the entry
name of selected spinner item*/), "array", getPackageName());
choiceArray = id;
ArrayAdapter a = ArrayAdapter.createFromResource(this, choiceArray, android.R.layout.simple_spinner_item);
((Spinner) findViewById(R.id.spinner_choice)).setAdapter(a);
}
or any other value corresponding to a spinner choice...
EDIT : added xml spinner content
you can use public int getIdentifier (String name, String defType, String defPackage) to retrive the id at runtime.
name is the name of the resource. The one you specified in the the name attribute in the xml
defType is the type of the resource. E.g. drawable. In your case you have to use "array", because the resource you want is an array.
defPackage, is the package name of your app. You can retrieve it using getPackageName().
If you want to retrieve the id of array_nature, you will have:
int myStringArray = getResources().getIdentifier("array_nature", "array", getPackageName())
getIdentifier returns 0 it the resource can't be found. So you should always check if the return value is grater than 0
Resources res = getResources();
String[] planets = res.getStringArray(R.array.array_nature);
Do the above one in strings.xml.

getResourceEntryName for an array item

I have a string array in strings.xml as detailed below:
<string-array name="spinerArray">
<item name="one">First</item>
<item name="two">Second</item>
<item name="three">Third</item>
<item name="four">Fourth</item>
</string-array>
And I need to retrieve one of the items name not the value (one, two, three, four). So far, I know it's possible to get the name of the array using getResourceEntryName like:
getResources().getResourceEntryName(R.array.spinerArray);
Is there a way to get an item name?
Thanks.
As this is an xml, you will need a xml parser. Then you can get the values of name using
attributes.getValue("name");
Or
name = ((Element) your_node).getAttribute("name"));
using DOM parser (org.w3c.dom)
Another solution:
Resources res = getResources();
TypedArray entriesTypedArray = res.obtainTypedArray(R.array.spinerArray);
ArrayList entriesName = new ArrayList();
TypedValue v1 = new TypedValue();
for (int i1=0; i1<entriesTypedArray.length(); i1++) {
entriesTypedArray.getValue(i1, v1);
String name1 = res.getResourceEntryName(v1.resourceId);
entriesName.add(name1);
}

How to add items to a stringArray in an arrayList programmatically?

This is my array.xml file in the res/values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="updateInterval">
<item name="1000">Pico TTS</item>
<item name="5000">Invox TTs</item>
</string-array>
</resources>
I need to add some more items to the updateInterval array list. How can I add the items that are dynamically coming from server programmatically?
You can't add item directly to that string array.
But you can use that array and dynamically add elements to that string array.
Do in this way.
String[] array = getResources().getStringArray(R.array.updateInterval);
System.out.println("--array.length--"+array.length);
List<String> list = new ArrayList<String>();
list = Arrays.asList(array);
ArrayList<String> arrayList = new ArrayList<String>(list);
arrayList.add("TTS");
array = arrayList.toArray(new String[list.size()]);
System.out.println("--array.length--"+array.length);
It is not possible to edit an xml resource dynamically, you just can have an static arraylist in case you need some data at runtime plus xml data.

Categories

Resources