Storing array into xml file - android

I want to store an array into a xml file.
The array will be called with the following function.
String getTextSpeech = TextSpeech[Index];
This is using the textToSpeechAPI.
How can I store the array TextSpeech[] in an xml file in the values folder and then call it within the class.
Thanks
Edit:
<resources>
<string-array name="myArray">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
</resources>

In activity you can call it as
String[] resourceString =getResources().getStringArray(R.array.myArray);
if your class is not of an activity type then use a contructor of class like
YourClass(Context activityContext)
{
String[] resourceString =activityContext.getResources().getStringArray(R.array.myArray);
}

You cant store any thing in values at run times but what you want can be achieved through various data storage solutions provided by android http://developer.android.com/guide/topics/data/data-storage.html
For your scenario I would suggest SQLite Databases

Related

How can i create nested array in android studio Strings.xml

<resources>
<string name="app_name">UnConv</string>
<string-array name="mainunit">
<item>Area</item>
<item>Pressure</item>
<item>Speed</item>
<item>Volume</item>
</string-array>
</resources>
i want to add some other values to the above items like a subgroup. Example for area i want yard, acre etc how can i achieve that?
Unfortunately, There is no direct way to save two dimensional string-array in Android resource file. I provide 2 methods for replacement.
1. Save the data as Json string showing in the following code:
<string name="mainunit">{"Area":["yard","acre"],"Pressure":[],"Speed":[],"Volume":[]}</string>
Then parse Json String to Java/Kotlin Object.
2. Save subgroup value splitting with ",", and get value by position, but it has drawback because it ignored the key name.
<string-array name="mainunit">
<item>yard,acre</item>
<item>Pressure</item>
<item>Speed</item>
<item>Volume</item>
</string-array>

accessing string from string array in layout file of android studio

I am using Android studio and have an array in my string.xml file as:
<string-array name="my_array">
<item>text1</item>
<item>text2</item>
<item>text3</item>
</string-array>
I know how to access the array (and get the 1st item) in my MainActivity.java file:
myButton.setText(getResources().getStringArray(R.array.my_array)[0]);
My question: Is there anyway to set the text directly in the activity_main.xml file? I tried:
<Button
android:id="#+id/myButton"
android:text="#array/my_array[0]"
... />
but that causes an error. Without the "[0]" it displays the 1st value (text1), but maybe that is just because of the button's size and it's not showing the rest - I can't get it to display other items (e.g., text2).
Is it possible to access one value of the array directly in the layout file?
Thanks.
I found a good answer here: https://stackoverflow.com/a/4161645/933969
Basically you create named strings first (and use those where you would want mystrings[x]) and then create your array using references to those named strings:
<string name="earth">Earth</string>
<string name="moon">Moon</string>
<string-array name="system">
<item>#string/earth</item>
<item>#string/moon</item>
</string-array>

Get data from android string array from resource

I know how to get data from string-array. I have done like this.
<string-array name="values">
<item>value1</item>
<item>value2</item>
</string-array>
String[] values = this.getResources().getStringArray(R.array.values);
But I dont know whether the below type string-array is possible and confused to get values from that.
<string-array name="country_data">
<item>
<country>Afghanistan</country>
<countryCode>93</countryCode>
<iso2>AF</iso2>
<iso3>AFG</iso3>
</item>
</string-array>
If it is possible please help me to get the values.
I dont know whether the below type string-array is possible
It is not.
If you want to have arbitrary XML, use res/xml/ for whatever XML structure you like. You can then use getResources().getXml() to get an XmlPullParser that you can use to parse that XML.
Or, wrap the contents of each of your <item> elements in CDATA, which should prevent the resource parser from messing with them. Then, each string you retrieve from the array would be plain XML, that you would need to parse.
Or, go with JSON in res/raw/ or assets/, using your favorite JSON parser (e.g., Gson).
Or, use SQLiteAssetHelper to package a database in your app that has this data pre-loaded.
Or, use R. Zagórski's solution, of having one <string-array> per object, with <item> elements for each field of the object.
xml :
<string-array name="_name">
<item>n11</item>
<item>n12</item>
<item>n13</item>
<item>n14</item>
<item>n15</item>
<item>n16</item>
</string-array>
Retrieve at java class:
String[] test;
test = getResources().getStringArray(R.array._name);
Of course it is possible.
In summary:
Define POJO representing your model
Create a function to import string array of arrays from resources into this POJO
Remember that you cannot use custom xml entries, they have to be <item>. And you need to know it's type upfront.

How Do I call String-array value in xml layout

I have a string-array in my string.xml file in Android res/value folder like this
my string.xml
<string-array name="books_titles_en">
<item>Smoking Effeccts on Your Body</item>
<item>Smoking - effects on your body</item>
<item>How Tobacco Smoke Causes Disease</item>
<item>patterns of use, health effects, use in smoking cessation and regulatory issues</item>
</string-array>
I need to retrieve the first element of array like this
my home.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/frag_1_text_pad"
android:textSize="#dimen/frag_1_text_size"
android:paddingRight="#dimen/frg_1_text_right_pad"
android:id="#+id/book_link0"
android:text="#string-array/book_title_en[0]"/>
I know it seems like it will cause an error. How do I retrive these things on the string-array in string.xml in Android
Just to giva a full answer to your problem:
There is no way of accessing an array directly, but you could do something like this:
<string name="books_titles_en_1">Smoking Effeccts on Your Body</string>
<string name="books_titles_en_2">Smoking - effects on your body</string>
<string name="books_titles_en_3">How Tobacco Smoke Causes Disease</string>
<string name="books_titles_en_4">patterns of use, health effects, use in smoking cessation and regulatory issues</string>
<string-array name="books_titles_en">
<item>#string/books_titles_en_1</item>
<item>#string/books_titles_en_2</item>
<item>#string/books_titles_en_3</item>
<item>#string/books_titles_en_4</item>
</string-array>
and then:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/frag_1_text_pad"
android:textSize="#dimen/frag_1_text_size"
android:paddingRight="#dimen/frg_1_text_right_pad"
android:id="#+id/book_link0"
android:text="#string/book_title_en_1"/>
So you basically reference the neccessary strings directly, that are referenced in your string array. ;)
Refer to this link Android - retrieve string array from resources
And this link Help in getting String Array from arrays.xml file
Basically you will have to code for the array retrieval xml in the Java Class by making an adapter.
The first link I posted used
for(int i = 0; i < menuArray.length; i++)
{
Button b = new Button(this);
b.setText(menuArray[i]);
ll.addView(b);
}
which is a sample of how to obtain the whole array from the xml.
You can modify it for single array value retrieval, or whichever array value you wish to retrieve.
Happy coding :D

Best way to use a static array

In my little app, I have a long list of data. Before, I used an editor to let the user enter the data - but now I would like to put the data static in program code/string list value Folder.
What is the best way to achieve this?
If you already know the values inside array you can crate string array inside strings.xml file which is present under values.
example:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sports_array">
<item>Football</item>
<item>Cricket</item>
<item>Hockey</item>
<item>Tennis</item>
</string-array>
</resources>
This application code retrieves a string array:
Resources res = getResources();
String[] sportlist = res.getStringArray(R.array.sports_array);
so now your sportlist will contain all the items specified in sports_array which is declared inside strings.xml
for more information pls see
http://developer.android.com/guide/topics/resources/string-resource.html

Categories

Resources