How to change values in strings.xml through Java (Android) - java

I'm trying to change the values in my strings.xml using Java. It's to change certain labels in an app dynamically on certain events. Any help would be appreciated

You can't change values in strings.xml as Strings are immutable (unless you do some dynamic compilation). You can have enums of Strings that can change the answer depending on passed-in variables

I think the best solution for your need would be to define all possible strings within the XML File. Based on the event in your application you can then choose the correct string. If the event is something like a user input then the changing the XML file is not possible. But in this case it should be fine to change the String directly without using XML references.

You might want to take a look at Java String Formatter it simply let you format a string in your string.xml but not changing the whole String , as other answers said, you can't change the value of a String in strings.xml from your java code.

Related

get text using JAXB

In my app, I'm reading a xml file (with JAXB) but there is times that I have empty segments.
I put some examples
<tuv>
<seg>Unknown: the action taken should always be known</seg>
</tuv>
Here I read well the tag "seg", I can get all text "Unknown: the action taken should always be known" but if this text is like this:
<tuv>
<seg><bpt i="1" x-wb-tag="b1" />Unknown<ept i="1" x-wb-tag="/b1" />: the action taken should always be known</seg>
</tuv>
I don't get anything, my variable is empty and I want to get all text "Unknown: the action taken should always be known"
Could I get this text "Unknown: the action taken should always be known"? (I want this like a string)
Note: my variable's value is a "string"
Thanks!
I answer to my question if anybody needs it.
I have used #XmlMixed that return a object list.
Here there are more information: mixed element

Changing parameters in compiled program - Java

I made a program, and for "protection" set some parameters. First parameter is date to wich program can work (something Trial but with fixed date), and the second one is HDD serial number (had some problems with other hardware serials) on which program works.
Now, I need to make it possible to me to change these values after compiling program.
I tried adding Log in which accepts anything and executes program with default values. Only if I log in with my user/pass values, it somehow allows me to change default values. After that, by every start of program, he checks with new values I've entered earlier.
If someone understands what I want and what I tried, tell me is this possible, or is there some other and easier/better solution?
put your "constant" part in a String which will must have a fixed size
that string should have only ASCII code; for max flexibility I'd use a base64 encoded string, so you can put even binary encripyted data
compile the class
if you open the .class with an hexadecimal editor, you are able to see and change that string
edit the .class file by that hexadecimal editor, putting the new ASCII values. Keep attention not to change the size of the String
Note also that this approach can be hackable by some guys :)

Use dynamic R strings in Android

I'm having a problem using strings stored in my strings.xml, I have a wide list of strings stored there. They are very useful for me because I'm using them to translate my program. However, now I want to choose between those strings dynamically and I don't know how to do it. It will be easier to understand with an example. Let's assume that I have the following strings:
<string name="red">Red</string>
<string name="blue">Blue</string>
<string name="green">Green</string>
<string name="yellow">Yellow</string>
And now let's assume that I have a function that passes me a string with a color, for example "yellow". Now I only have a solution for this, to make a very huge switch (very very huge, because I have lots of strings), I think that there must be an option to transform the output of my function into the right parameter.
I mean, if I have a function that returns me "yellow", and I want to use R.strings.yellow, there must be a link between them. I don't know if maybe you could use any kind of reflection to achieve this.
Can you help me?
There's a way 10 times faster than regular android method "getIdentifier" to get the value from not only string but also drawable or any other resource existing in the R file in a very simple manner using reflection as follows:
try {
//Get the ID
Field resourceField = R.string.class.getDeclaredField("yourResourceName");
//Here we are getting the String id in R file...But you can change to R.drawable or any other resource you want...
int resourceId = resourceField.getInt(resourceField);
//Here you can use it as usual
String yourString = context.getString(resourceId);
} catch (Exception e) {
e.printStackTrace();
}
Hope this helps.
Regards!
Use a two-step process to find the id to load. First use Resources.getIdentifier(), for example:
int id = getResources().getIdentifier("yellow", "string", getPackageName());
Then, after checking the id is not zero (which indicates it could not find the resource), use the id to get a string like normal:
String colour = getString(id);
String mystring = getResources().getString(R.string.yellow);

Invoking java methods in XML in android

I am making an app that generates random numbers for android (you can find it hat https://github.com/JXPheonix/RNumGen ) and I need some way for an xml string to be dynamic (of sorts); it needs to change every time it is viewed.
I want the string to invoke a method that generates a random number. The string in my xml file goes something like this:
<string name="number">Your number is</string>
and I want after the is for a method in java to be called upon, which would subsequently change the ending of the string. Any way to do this (whether or not it involves actually editing the strings.xml file?)
You're going about it a round-about way. Instead of somehow making your string in your XML file dynamic, just use your string and append to it in code. Something like:
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(getString(R.string.number) + yourRNGMethod());
Don't change the string, concatenate the constant string with your generated number in your code.
Alternatively, use the string as an input to String.format and use a placeholder for the number.
You don't want to use the strings.xml for strings which have changing values.
Its not clear what the scope for consumption of your random number is but you can always use the Math classes to generate a random number and then Integer.toString() the value to get the value as a string.

Difference between String and AttributedString

I am quite confused here. Today I came across this concept of AttributedString. But I am not able to find the exact difference for String and AttributedString. And more ever I am not able to convert this attributed String to String using the toString(). Can anyone help me with this?
EDIT 1
Actually I want to provide Kerning attrribute for my text. But I don't know how to do it. Can anyone help me?
A String is a holder for some consecutive list of characters, usually known as "text". "Hello World" is a String. It's a general-purpose data type that's used a lot all throughout Java.
An AttributedString holds text and some attributes. Attributes can be pretty much anything. Usually it's used for taggin some parts of the text with some language or even providing information about layout arguments (bold, italics, ...). It's a very specific data type that's used for some specific areas only.
Unless you have a specific reason to use AttributedString, you can pretty much forget that it exists.

Categories