how to extract the name attribute from string array? - android

Hi I build a quiz application.
I have the following (values/)question.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="question">
<item name="correct">A</item>
<item name="wrong">B</item>
<item name="wrong">C</item>
<item name="wrong">D</item>
</string-array>
</resources>
I would like to have a question with four possible answers but when i retrieve my answers in Java.. I don't know which answer is correct. So I decided to use name attribute in the item tags to pass a value of 'correct' or 'wrong' answer.
Is there anyway to get the name along with the tag value?
because when i use String[] test = res.getStringArray(R.array.question); I can only get the value of each item in my array.
or because this is my 1st time in Android. is there other suitable approach to do this?
thanks

You need to use Handler to parse the xml.
to get attribute value, code is :
attributes.getValue("name")
Try these links for reference:
first and
second

I have made many quizzes, and one nice way is to put correct option always on top, right after question. You can use random function to shuffle options while displaying in an activity.

in Activity:
String[] questionArray = getResources().getStringArray(R.id.question);
then you have
questionArray[0] -> A
questionArray[1] -> B
questionArray[2] -> C
questionArray[3] -> D

Related

Get a specific string from a stringarray in Android Studio

I have an array of strings (in strings.xml), in which I collect all my games
<string-array name="games">
<item name="select" translatable="false">...</item>
<item name="game_fortnite" translatable="false">Fortnite</item>
<item name="game_csgo" translatable="false">CounterStrike: Global Offensive</item>
<item name="game_minecraft_minigames" translatable="false">Minecraft Minigames</item>
</string-array>
I am now trying to get a specific item (In this case the first one, but I will need others later) from this array. Since i can give the items names, without Android Studio underlining it for me, I thought maybe i can refer to the strings by names, which does not work in any way I tried it.
In this case I am trying to find out wheter my string variable "game" has the same value as the array item I have named "select". I have tried all of the following:
if(game.equals(R.array.games.select)){
}
if(game.equals(R.string.games.select)){
}
if(game.equals(R.array.games[0])){
}
if(game.equals(R.array.select)){
}
I heve tried, as you can see, using an index (didn't work), but I'd like a possibility to refer to them by their name property.
This will reach your xml string and then you can use java string,
String gamesArr[] = getResources().getStringArray(R.array.games);
After doing this, create a list view and fill the list view items with that array.
And below code will give you selected item on the list view.
myList.getSelectedItem();
As per android documentation, the item tag for string-array does not have any attribute available.
https://developer.android.com/guide/topics/resources/string-resource#StringArray

What's the simplest and most efficient way to associate different data in XML?

Basically what I'm trying to achieve is to access from code two related resources.
Consider this example, the best solution I can think of to my problem:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="black">⬛</string><color name="black_c">#000000</color>
<string name="white">⬜</string><color name="white_c">#ffffff</color>
</resources>
Given a string N in my code I can access both the second string associated to it (⬛ or ⬜) or the color by adding "_c" to the end of the N string.
So, if N="black" I can use N to retrieve both ⬛ and #000000 (with N + "_c")
Is there a better way to do this? My solution feels a bit hacky. Hope I managed to explain what I'm trying to achieve, thanks!
I have another proposal. I hope it will help you.
If you have a colors.xml and a strings.xml (within the values directory)
<!-- colors.xml -->
<resources>
<color name="black">#000000</color >
</resources>
<!-- strings.xml -->
<resources>
<string name="black">Some black string</string>
</resources>
Using the same name you can access both of them if you are able to get the different id (ie R.string.black or R.color.black). The getIdentifier()` method can do it. So you can try (not tested)
String name = "black;
String choice = "color"; //or "string" dependending on if you want the color or the string
int resID = getResources().getIdentifier(name, choice, getPackageName());
Resources resources = getResources();
//Then access using
//If choice=="color"
int color = resources.getColor(resId);
//If choice=="string"
String text = resources.getString(resId);
Ya it sounds a bit hacky. You could use styles to group all your resources (as attributes to that style) and then read those styles. More info here: How to retrieve style attributes programmatically from styles.xml
But this again sounds hacky. What's your actual requirement? The way you suggested sounds OK. You can go ahead with that implementation since from a performance standpoint, it doesn't take any extra over head. Just make sure you write a neat API to fetch data from your xml.

getQuantityString returns wrong string with 0 value

In an android app, I have the following string resources:
<plurals name="test">
<item quantity="zero">"I have 0 item"</item>
<item quantity="one">"I have 1 item"</item>
<item quantity="other">"I have several items"</item>
</plurals>
And the following line of code:
String text = getResources().getQuantityString(R.plurals.test, 0)
which I would expect to return
I have 0 item
But it actually returns
I have 1 item
Why ?
Quantity Strings are broken on some Plattforms and phones as the issue Tracker and this discussion "Should Plurals and Quantity Strings be used" points out. It depends on many factors which you cannot control (i.e. localization on the phone).
One solution can be to take an external library like this one, which mimes the same functionallity.
Another solution is stated in the documentation of plurals in android. Avoid using it and use "quantity-neutral" formulations like "Books: 1"
Change the code like this
String text = getResources().getQuantityString(R.plurals.test, 0,0);

Dynamically referencing a String in Strings.xml in Android

Say I have the following my strings.xml file:
<string name="string0">Lorem</string>
<string name="string1">ipsum</string>
<string name="string2">dolor</string>
In my activity an ID is set based on the clicking of a button. If the top button is clicked the id is 0, middle is 1 and bottom button is 2.
What would the syntax look like for referencing one of the three strings?
I know R.string.string0 works but I want to do something equivalent to:
R.string["string"+currentID]
where I derive the string to use based on the ID.
Just not sure what the syntax would look like in Java/Android.
Thanks in advance,
Tony
Could you not just use a string array in your resources instead of separate string entries?
That's a bad approach. It's slow. It'd be better to have an internal integer array with all the R.string IDs.
If you really insist on using a string-based approach, use Resources.getIdentifier().

Array defining in Android Application

I want to use the concept of array in my Android Application, I don't know how to do that actually.
So could anybody please help me how to do that on demand.
I guess you are talking about arrays in Android through the res folder.
Create an array.xml inside the /res/values folder with something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="names_list">
<item>John</item>
<item>Peter</item>
<item>Charles</item>
</string-array>
</resources>
You can get that array on your Activity by doing:
getResources().getStringArray(R.array.names_list);
There are alot of different "array" types in java... there are actual arrays like Thorsten showed you and then there are lists, collections and hashes. Take you pick. :) A great place to start learning more about Java is the docs.
http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/
This defines an array of 5 strings:
String[] stringArray = new String[5];
However, I can not imagine that this is really what you're talking about...
CLARIFICATION
If you actually don't know what an array is, then my reply will give you a hint. In case you're talking about something else, this reply should indicate that you're not giving enough detail. You might as well be talking about this...

Categories

Resources