Is there a way to dynamically add values to an Android resource string-array?
E.g.:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="titles">
</string-array>
</resources>
Is there a way to dynamically add values to an Android resource string-array?
No, because resources are read-only at runtime.
Yes! you can create a static array of strings in Android but dynamically adding values to an Android resource is not yet possible.
It turns out that it’s easy to create and use a static array of strings in Android. Of course, you can do this in Java code, as I describe in my Java string array tutorial, but for Android, I’m talking about doing this in XML.
In short, this is how you define a static string array in an Android XML file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="my_books">
<item>Scala Cookbook</item>
<item>Play Framework Recipes</item>
<item>How I Sold My Business: A Personal Diary</item>
<item>A Survival Guide for New Consultants</item>
</string-array>
</resources>
Then inside an Activity, Fragment, or other Java class, you can create a string array in Java from that XML like this:
Resources res = getResources();
String[] myBooks = res.getStringArray(R.array.my_books);
Related
I want to use a different string resource on small screens but Android always uses the default string. Am I doing it wrong?
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue</string>
</resources>
/res/values-large/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue to Next Page</string>
</resources>
#string/continue always resolves to "Continue", even on large screens. It does, however use the dimensions in /res/values-large/dimens.xml so I'm sure I'm using the right resource qualifier. I also tried /res/values-w550dp/strings.xml and it didn't work either.
Am I doing something wrong or is this a limitation of Android?
values-large is only available for dimens and styles, but not for string resources.
one could possibly add custom string-arrays and then look them up accordingly.
see Android Resource Types.
I want to support multiple languages in my app using sqlite database and xml resources.
For example, the user can insert transaction objects. Each transaction has a category.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="categories">
<item>#string/category1</item>
<item>#string/category2</item>
<item>#string/category3</item>
<item>#string/category4</item>
<item>#string/category5</item>
</array>
</resources>
What is the correct approach to use the above values in sqlite? Should a table be created or I can use them directly from xml?
If I want the user to be able to also add his own categories, how the multi-language thing should work?
Please read the two following google guides about supporting different languages and localizing:
http://developer.android.com/training/basics/supporting-devices/languages.html
http://developer.android.com/guide/topics/resources/localization.html
the normal way is to create a strings.xml for every language:
For Storing your categories in your SQLite Database you should enumerate your Categories and store only the numbers, so you are independent of the spelling. Adding a user defined Catergory should also work this way.
MyProject/
res/
values/
strings.xml
values-es/
strings.xml
/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>
/values-es/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi Aplicación</string>
<string name="hello_world">Hola Mundo!</string>
</resources>
I am programming an application contains layouts with some views. I load these colors from values/colors.xml. Now, I want to define multiple themes for my application, e.g. Blue and Green (some sort of blue and green colors). My question is how can I define two colors.xml file and load it based on some conditions or choosing by user. What I want is some thing like strings.xml that we can load strings based on locale defined.
Thanks in advance.
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="white">#FFFFFF</string>
</resources>
values-fr/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="white">#EEEEEE</string>
</resources>
button.setBackgroundColor(Integer.parseInt(getString(R.string.white)));
I have a settings.xml file that I use to set various global variables within my app. I want to be able to create sub xml files that I include in the settings.xml file because depending on the client, I change settings in this file so I want to make it easier on myself.
I know that there is a way to include layout files this way but I can't find any documentation that shows how to do this with plain XML on Android.
Any help would be much appreciated.
I think all you have to do is make an additional file which includes the settings you want.
Ie, I have strings.xml in values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="main_title">Hello there</string>
</resources>
And I can create additionalstrings.xml in the values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="additional">How are you?</string>
</resources>
I can refer to either R.string.main_title or R.string.additional, without having to say the file it came from.
I'm writing an application were it requires to change the language of the application as and when the user requires. The data in different language is stored in the data base, from which the data is fetched and the UI is updated. I'm want to what should be done if device does not support the particular language font. Any help will be much appreciated.Thanks in advance. _/|_
I don't know more about this but ...
for example if you want your application to support both English
and French strings (in addition to the default strings),
you can simply create two additional
resource directories called /res/values-en (for the English strings.xml) and
/res/values-fr (for the French strings.xml).
Within the strings.xml files, the
resource names are the same.
For example, the /res/values-en/strings.xml file could
look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello in English!</string>
</resources>
Whereas, the /res/values-fr/strings.xml file would look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Bonjour en Français!</string>
</resources>
A default layout file in the /res/layout directory that displays the string refers to the
string by the variable name #string/hello, without regard to which language or directory
the string resource is in.
The Android operating system determines which version of
the string (French, English, or default) to load at runtime.A layout with a TextView control
to display the string might look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/hello" >
</LinearLayout>
The string is accessed programmatically in the normal way:
String str = getString(R.string.hello);
It’s as easy as that.
More you will found Here