What I want to do is to get a specific text from strings.xml dynamically. I think it will involve to access an object variable dynamically.
There will be a function like:
public void getDynamicString(int level) {
text.setText(R.string.levelText_+level);
}
And in strings.xml there will be <string name="levelText_5">Text level 5</string>
I would rather not create a list with all the text resources. Can one do this in Java/Android.
Use the method getIdentifier(name, defType, defPackage) of the Resources class to get the id of a resource by name. Then you can do a normal getString(id) from the same class.
EDIT: a bit of Googling revealed this: this. You can find sample usage there.
Try: getResources().getString(R.id.stringId).
You should look at using getIdentifier(String, String, String) of the Resources class.
All you have to do is call
this.getString(R.string.levelText_5)
If your in an area of the program in which you have access to a Context or Application, such as a ListAdapter call:
context.getString(R.string.levelText_5)
or
application.getString(R.string.levelText_5)
if you have no access to the context or application then call:
getResources().getString(R.String.levelText_5);
To do it dynamically call:
String name = "levelText_"+level;
int id = getIdentifier(name, "string", "com.test.mypackage");
getResources().getString(id);
I had the same problem and I fixed it using this
okey , whenever you want to access a string from strings.xml dynamically and what i mean by that is to avoid using getResources().getString(R.id.stringId) ,you create a string in which you can manipulate dynamically however you want in our case uriq ("stupid variable name") and then you create resource object which is in my example level_res and initialize it then you use this method called getIdentifier() which accepts your dynamic string as a parameter ,now u simply pass your ressource to the method getstring(mysttring)
String uriq="level"+level_num;
level_res=getResources();
int mystring=getResources().getIdentifier(uriq,"string",getPackageName());
String level=level_res.getString(mystring);
Related
I have a URL and I want to print in my graphical user interface the ID value after the hashtag.
For example, we have www.site.com/index.php#hello and I want to print hello value on a label in my GUI.
How can I do this using Java in Netbeans?
Simple solution is getRef() in URL class:
URL url = new URL("http://www.anyhost.com/index.php#hello");
jLabel.setText(url.getRef());
EDIT: According to #Henry comment:
I would recommend to use the java.net.URI as it also deals with encoding. The Javadocs say: "Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL()."
and this comment:
Why not just doing uri.getFragment()
URI uri = new URI("http://www.anyhost.com/index.php#hello");
jLabel.setText(uri.getFragment());
Use the String.split() Method.
public static String getId(string url) {
return url.split("#")[1];
}
String.split() returns an array of Strings that are delimited, or "Split," by the value you pass to it, or in this case #.
Because you want only the string after the #, you can just use the second item in the array that it returns by adding [1] to the end of it.
For more on String.split() go to Tutorials Point.
By the way, the part of the URL you are referencing is the Element ID. It is used to jump to an Element on a webpage.
I'm trying to create a PreferenceActicity.
I need that on of my preferences will be of type MultiSelectedListPreference.
I found this code on the internet:
<MultiSelectListPreference
android:dialogTitle="#string/mode_repeat"
android:key="mode_repeat"
android:summary=""
android:title="#string/mode_repeat"
android:entries="#array/weekdays"
android:entryValues="#array/weekdays_values"
android:defaultValue="#array/empty_array"
/>
The problem is I'm getting the entries and entryValues in runtime.
I'm building the ArrayList while my app is running, the question is how can I set my ArrayList as the entries and as the entryValues?
Do I need to create an empty xml file, which I will re-write during the building of my list?
You wouldn't be able to change the xml in runtime. The solution for your problem is to use the methods setEntries()and setEntryValues()from the MultiSelectListPreference class.
Here's a basic code snippet:
MultiSelectListPreference repeatModePreference = (MultiSelectListPreference) findPreference(Constants. mode_repeat);
repeatModePreference.setEntries(yourEntries); // This is human-readable strings
repeatModePreference.setEntryValues(yourEntryvalues) // The value corresponding to the human-readable string
Hope this helps.
What's the analogous of
Resources.getMessage(request,"key");
(it's in org.apache.struts.validator package)?
I need to take the right string according to the language in use and put that string in an array.
These strings have a variable part. For example:
The field %s is required
I want to take the The field and is required from the MessageResources
EDIT
I've used
ResourceBundle myResources = ResourceBundle.getBundle("MessageResources");
String msg = MessageFormat.format(myResources.getString("errors.required"),
new Object[] { title });
It works, but only with the default language
You can use a resource bundle for this in idiomatic Java. The bundles are properties files, and they are named according to their locale's.
Here is a link to ResourceBundle in the Java API documentation.
http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html
Let me know if you need any more info.
I want to read/extract the value from HSSFComment.
I can access the HSSFComment by the following code:
HSSFComment comment = workSheet.getCellComment(1, 0);
But, how can I get the text/value from that "comment" instance?
there are tow methods in HSSFComment:
getTextObjectRecord()
getNoteRecord()
But both are protected methods...that's why I can't access those from my class. in other word, these methods are not visible from my class. Following line of code doesn't compile.
TextObjectRecord txo = comment.getTextObjectRecord();
Any comments?
Use getString() inherited from HSSFTextBox. This returns an HSSFRichTextString, which itself has a getString() method to get the plain text. In otherwords
String comment = cell.getComment().getString().getString();
Which you can't do like that due to the possibility of null returns, but that's the idea.
I've been racking my head with this...
I've got a localized strings.xml file in a values-en folder with this example string:
#string/my_string
The string has the following text stored in English: "My String"
When accessing the localized string via a layout, it works fine.
When I try to change it in code, that's where I get problems.
I store the string into an array of strings for later use. The 'context' is passed from my activity to a data class and used with this line of code:
dataStrings = new String[] { (String) context.getResources().getString(R.string.my_string) };
Later, I try to display this string, like so:
buttons[0].setText(dataStrings[0]);
It displays:
#string/my_string
How do I get it to display the string without '#string/', the proper localized string?
You can run getString() directly on the Context object; you don't need to run getResources(). However, this should do the same thing as you're currently doing so I don't think that's the source of your problem.
The first thing to confirm is that what you think is happening is happening. Either use the debugger to check that buttons[0] contains "#string/my_string" or try calling setText() with a hard-coded value to make sure the text is actually being updated on the correct button - e.g. buttons[0].setText("StackOverflow!");