Can you select a string resource with a name like - android

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.

Related

String resource ID keeps changing when I edit my string.xml file, android kotlin

So I have string.xml files for two languages, english and swahili. I let my users choose their default languages. And everything goes well until when I save the string resource ID for later use when they decide to change the language from the application's settings,but whenever I edit or add another string resource,and update my application, the original string ID's change and the string resource ID's that I saved in the database end up fetching values from different string resource from what my users had selected earlier. How do I overcome this? or stop my string resource ID's from changing every time I add new strings.
And everything goes well until when I save the string resource ID for later use
That is not a good plan.
but whenever I edit or add another string resource,and update my application, the original string ID's change and the string resource ID's that I saved in the database end up fetching values from different string resource from what my users had selected earlier
That is why this is not a good plan. Resource IDs are not designed to be constant from build to build.
How do I overcome this?
Do not store string resource IDs in a file, database, Web service, or similar container. Instead, store some identifier that you control that allows you to determine what string resource ID(s) that you should use.

Android-Studio: How to make a resource string value the same as its name

I want to use the string resources to save some key values. So, I have something like this:
<string name="key_name">key_name</string>
The resource name and the resource value are the same. There is no need for me to make a difference between them, as it is just to cleanly store one value.
To reduce redundant information,
is there a way to just tell android-studio that the name equals the value? Something like this?
<string name="key_name"/>
I think you can't do it. It looks like you create a variable, you must name it and when you need you can set its value.
You can't create a variable with same values with name at the same time.
I think you have the wrong resource type. A string resource is used for mapping a name to a text. From what you're describing here you only need the name (and then can extrapolate the text from it).
Using strings with matching name and value shouldn't be necessary:
If you're going to be referencing them from Java or Kotlin code, you still need to know the name (i.e. R.string.key_name), so you might as well just use the string value there.
If it's for using inside XML code (where you want to pass the value of a string, e.g. text="#string/key_name") most of the time you can just use the raw string there (i.e. text="key_name").

How to add R.drawable resources to an array for android?

I would like to have sequence of set R.drawable been called in integer array so that it can be accessed later with choice.But I'll like to load to the R.drawable dynamically which corresponds to different names.It names needs to be had as per the external value input not hardcoded.I tried this in making many but like use list,set,array conversion etc.Kindly guide me with a snippet or example on this regard.Thank you.
I suppose, that best way to achieve this funcionality is by using ArrayAdapter of Integer.
You will simply add drawble resources by id.
If you need to get resource id, but you will get string input from user, you can use this code:
Android, getting resource ID from string?

Storing strings in android XML resource file and referencing them

I'm wondering if I should use a resource XML file for to store all the possible recipes for me rather than using an ArrayList that is hardcoded, the problem is that I don't know how to call from a resource file using the method i have...
Here is a cut down version of what I want:
int recipeNumber = b.getInt("RECIPE"); //This is taken from another activity
final TextView rowTextView = new TextView(this); //Create a textview
rowTextView.setText(R.string.recipeNumber); //This is what i am struggling with
howToLinearLayout.addView(rowTextView); //Add textview to linearlayout
I don't know what to put in the part that references my resource file. I know I need:
rowTextView.setText(R. but I'm not sure what would come after that.
Im storing strings that would be for example:
<string name="1">One part Vodka, One part Coke</string>
<string name="2">One part Vodka, One part Lemonade</string>
This list will be quite long so id appreciate any other suggestions on storage, bare in mind I'm pretty new to this.
The recipeNumber int is what string will be called in to the textview.
Thanks for any help
I recommend storing the values outside your code, for example in XML or JSON (quicker and easier to work with). I would go with JSON. Really easy to load a list into an array, or other collection, and work with it anyway you want.
http://www.vogella.com/articles/AndroidJSON/article.html
You can then maintain your recipes, add new ones, maintain it etc without having to touch the code. Simply replace the XML or JSON (or whatever you choose) and rebuild your app.
Strings.xml is intended for pieces of text in your UI and code which you might want to localise and to avoid hard coding strings. It's not really intended for storing data.
To get string resources in code, use getResources().
rowTextView.setText(getResources().getString(R.string.recipeNumber));
To get an unknown resource identifier, but a known resource name, you can use the following method.
int identifier = getResources().getIdentifier("" + recipeNumber, "string", "com.your.package.name");
rowTextView.setText(getResources().getString(identifier));

Set values to text identifier by var name. An alternative to getIdentifier

I am trying to set String values for ids defined in xmls. I have defined the set of text which can be assigned to different ids in the layout xml files. Thereby I can also see the int values that are associated with different texts in R.java.
I have stored the variable names that I have given to the texts in my database along with the R.id prefix as they appear in R.java file.
For setting text,
TextView messageone = (TextView)findViewById(R.id.textfield1);
Normal Usage:
String message = "Hi, hello";
messageone.setText(Status);
What i want to implement:
public static final int messagestring is present in R.java
R.id.messagestring is stored in sqlite database in text format
messageone.setText(what_here);
what_here = a way to get the value from "R.id.messagestring" string as obtained from database.
I know public int getIdentifier (String name, String defType, String defPackage)
can be used here. The only change would be stored text in database will change from R.id.messagestring to messagestring. But there is a note discouraging this type of implementation.
It says: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Android Docs getIdentifier
I think although this method seems like a longer implementation, can be efficient when the objects dealt with are not text.
There's a getString(int Id) method you can use inside an Activity. It will return a String from a given R.string Id.
String something = getString(R.string.something);
I hope I helped out a bit here, because I'm not entirely sure if I understand your question.

Categories

Resources