How to get dynamically images from strings.xml - android

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

Related

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.

Android random non repeating string

I'm making a bingo type game. I have a 5x5 grid of imagebuttons, each with their own textview. When the app starts or is reset, I want each textview to display a random string without any one string being displayed twice during a game. I currently have the strings in a resource array, with 127 items:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="tile_text">
<item>String 1</item>
<item>String 2</item>
<item>String 3</item>
...all the way to String 127
</string-array>
</resources>
And to display a random string on each textview:
public String[] myString;
Resources res = getResources();
myString = res.getStringArray(R.array.tile_text);
Random random = new Random(System.currentTimeMillis());
int[] textViews = {
//I have all my textviews added to this array
};
for(int v : textViews) {
TextView tv = (TextView)findViewById(v);
tv.setText(myString[random.nextInt(myString.length)]);
}
The above works well, but even with 200 strings in the array to choose from, some items still show up twice. Is there a way I can get the array to shuffle and not pick the same string twice for per game? I have searched and I find info on random strings, but nothing about non repeating random strings, so apologies if this is a duplicate question.
I'd keep a list of the strings you'e already added and then keep picking new random strings until you find one that's not already in your list.
Something like this:
Vector<String> alreadyUsed = new Vector<String>();
for(int v : textViews) {
TextView tv = (TextView)findViewById(v);
String nextString;
do {
nextString = myString[random.nextInt(myString.length)];
} while (alreadyUsed.contains(nextString));
alreadyUsed.add(nextString);
tv.setText(nextString);
}

Storing R.drawable IDs in XML array

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)
}

Android - Getting a list of drawable resource

In android you can access a resource with syntax like R..
what I want to do is to reach a set of images has naming convention.
for example I have 4 files in drawable with the names :
draw_1_.jpg
how can I get the list of drawable images to List ..
This is because, I want to make a slide show.
thx
I don't believe you can trust the resource compiler to give your images sequential integer values. In the past I've always created a static array to store these.
private static int[] imgs = { R.id.draw_1, R.id.draw_2, R.id.draw_3, R.id.draw_4 };
With this you can then have a section of code like:
int curSlide = 0;
view.setBackgroundResource(imgs[curSlide]);
Although it is an old question, I just had a similar problem recently. I liked solution of jeffd, but storing a list of images ids in code is not the best idea. So, I created an xml file with the list, which I put in res/xml directory.
<?xml version="1.0" encoding="utf-8"?>
<list>
<image id="#drawable/draw_1"/>
<image id="#drawable/draw_2"/>
<image id="#drawable/draw_3"/>
<image id="#drawable/draw_4"/>
</list>
Images are stored in drawable directory and are named draw_1.jpg, draw_2.jpg, etc.
Using this sort of xml has an advantage that availability of resources is checked at compile time, so if you have a typo, it won't compile.
Retrieving the list in code is simply parsing the xml, yet it's a bit verbose. XmlPullParser has method getAttributeResourceValue that allows getting resource id without dealing with any strings.
List<Integer> list = new ArrayList<>();
try {
while (parser.next() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() == XmlPullParser.START_TAG &&
parser.getName().equals("image")) {
int imageId = -1;
for (int i = 0; i < parser.getAttributeCount(); ++i) {
if (parser.getAttributeName(i).equals("id")) {
list.add(parser.getAttributeResourceValue(i, -1));
}
}
}
}
}
catch (XmlPullParserException | IOException e) {
e.printStackTrace();
}
Another simpler solution would be using arrays as resources. I haven't tried this out, but you can see an example here: http://www.geeks.gallery/how-to-list-images-from-array-xml-in-android/ This is how to structure your xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="list">
<item>#drawable/draw_1</item>
<item>#drawable/draw_2</item>
<item>#drawable/draw_3</item>
</array>
</resources>
To access the array you can use this code:
TypedArray list = getResources().obtainTypedArray(R.array.list);
for (int i = 0; i < list.length(); ++i) {
int id = list.getResourceId(i, -1);
}
well, if you know the suffix of the images, you can request the identifier for a drawable by getResources().getIdentifier(...) and then using the identifier get the drawable. So if you know how many images you have, then you can create a loop and store each of the drawables in a list. Just take into account that such a lookup is relatively expensive.
You Should identify all the Image as a Drawable .. and then you can use them as you can !

Categories

Resources