Get string from xml (other than strings.xml) - android

I want to get a string array from other file than strings.xml, because I want my project to be organised.
For example, I have this code:
String[] column1 = getResources().getStringArray(R.array.column1);
But my string array isn't located in strings.xml, but in \res\values\tables\10.xml
How can I get the string array from that file?
Thanks in advance.

Your XML files can have arbitrary names, but they need to be located in the values folder. I suggest just calling your file tables10.xml.

Related

ListView and R string directory

I have a project (meaning an android app) created and used a custom ListView with three TextView UI for each column. But the app is also designed to support multi-language (at least three) using strings.xml file to fetch the selected language when user selects. Thus, the case here is, I have to write for the ListView String [] example {"a", "b", "c"} in java. I tried to do something like this String [] example {R.string.a, R.string. b, R.string. c} but the compiler says Error " illegal array ".
Now my question: Is it possible to reference the String [] { } of the ListView to strings.XML files.
Thank in advance and any example code or link I'm appreciated.
If your elements of the array are constant, you can make string array in strings.xml and access that array in code.
Put the following in strings.xml
<string-array name="example">
<item>a</item>
<item>b</item>
<item>c</item>
</string-array>
Access this array in code as:
String examples[] = getResources().getStringArray(R.array.example);
you can not use the direct reference to the String array, you have to get the value of String resource via getString()
like this
String[] example = {
getString(R.string.a), getString(R.string.b), getString(R.string.c)
};

Can you select a string resource with a name like

I am thinking of creating a small dynamic layout using strings.xml as a base.
I know how to select single resources but is there a way of selecting ALL resources with a name like "Intro_"
A bit like a Select where like query.
So if i had some strings like;
Intro_Welcome
Intro_FirstParagraph
SecondScreen_Welcome
List of strings mList = select all string resources with a name like "Intro_"
mList would now contain the string of Intro_Welcome and Intro_FirstParagraph
Any help is usefull
Thank you
You can't do this like you described. The string resources name that you define in your xml file are mapped into int values. So, when you use they in your code, you use by the int value created.
You may consider using a modified version of this class, that can get the string resource id within the string text.

How many java files are using string value from String xml

I am reading one android project in Eclipse. There is one String value in String xml file.
<string name="emoji_keyboard">Emoji</string>
I want to know how many files are using this value. Any idea?
You could search for - R.string.emoji_keyboard.

How to access values from "strings.xml" dynamically in android?

App version 2.3.3
Here is what i am looking for...
I have few strings in "values/strings.xml". I wish to retrieve the values depending on the string's "name" attribute and use it my application. The input to which string in the xml should be used comes dynamically. Here is an example...
My input from other file :: "A" => This value changes. It can be any value in A,B,C,D.
I have different strings in strings.xml, like...
<string name="A">AforApple</string>
<string name="B">BforBall</string>
<string name="C">CforCat</string>
<string name="D">DforDog</string>
Now, how do i programmatically get the value(AforApple) of the String with name="A".
String str = context.getResources().getString(R.string.A)
or u can use
textBox.setText(R.string.A);
do not forget to import the package com.yourpackackage.R.
You need to use getResources() method:
String a = getResources().getString(R.string.A);
Just for the record, you can also dynamically generate it using reflection.
int stringRes=R.string.class.getField("Here you put the dynamically generated input, such as A").getInt(null);
textView.setText(stringRes);
This will return the resource int value from string XML based on the input, as long as the input value "A" matches string name in the XML, this will retrieve them dynamically.
To access a String resource/value that you’ve defined in an XML file from your Android/Java code, use the Android getString method, like this:
String A = getString(R.string.a_string);
If your code gets a string like "A" and you are trying to dynamically find the string in your resources that matches that name, I don't think you can do that.
Instead of using the strings.xml, you might want to use arrays.xml and build a HashMap from that before you need to access those strings
Try this:
String word = getResources().getString(R.string.A);
Check out the link here.
You can use this code:
getText(R.string.A);
Basically, you need to pass the resource id as a parameter to the getText() method.

Getting string array from String values folder?

In my Values folder for my project, I have a string array named currency_avatar. I want to be able to use this array of strings in my activity.
I know that "R.array.currency_array" this line of code is legal, however I do not know how to actually get this to refer to a local String array in my own activity.
So, how do I refer this currency_array located in my values folder of res to a local String array variable in my activity?
Sorry for such a simple question. :)
String[] currencies = getResources().getStringArray(R.array.currency_array)
This article will help you in following problems you can face.

Categories

Resources