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.
Related
I have a class called GamesData that has strings and getters and setters for this strings.
I download strings from a json. One of these strings is an URL with an image. I download the images but on a small size, because of their URL. I need to download it on a bigger size. For this, I need to remove this "small" string from the URL:
"home_team_logo": "https:\/\/URL\/images\/teams\/small\/olympique-marseille-890.png"
I have more than one URL coming from a big json object, all inside a json array, formated like the one above.
This is what I do to get the json.
arrayList.add(new GamesData(
gamesDataObject.getString(TAG_DATE),
gamesDataObject.getString(TAG_COMPETITION),
gamesDataObject.getString(TAG_HOME_TEAM),
gamesDataObject.getString(TAG_AWAY_TEAM),
gamesDataObject.getString(TAG_ID)
));
I need to remove from TAG_HOME_TEAM
public static final String TAG_HOME_TEAM= "home_team_logo";
, which is that URL above, only the "small" part, so the image downloaded will be the full size one.
In fact I will need this for all 3 tags: TAG_COMPETITION, TAG_HOME_TEAM, TAG_AWAY_TEAM.
How on earth do I do this? xD
You can use replace , use /small/ with / as replacement to avoid matching something like /othersmallteamname/
String s = "https:/URL/images/teams/small/olympique-marseille-890.png";
System.out.println(s.replace("/small/", "/"));
Output :
https:/URL/images/teams/olympique-marseille-890.png
I'm thinking something like this might work:
gamesDataObject.getString(TAG_HOME_TEAM).replaceAll("\/small\/","/");
This uses a regular expression to match all occurrences of the patern. A simplepr less error prone approach may be to use
gamesDataObject.getString(TAG_HOME_TEAM).replace("\/small\/","/");
Which should only replace the first occurrence.
I am trying to take a person's name from a text input field, send it to another Activity, and display that person's name in a sentence within a TextView.
My code is as follows:
This is in one of my Activities.
String meaningText = mCurrentColor.getMeaning();
meaningText = String.format(meaningText, mYourName);
mColorText.setText(meaningText);
I'm pulling the Meaning string from another class that would contain something along the lines of:
"Hey %1$s, you chose this which means blah blah....."
Then I want to insert that mYourName variable which is pulled from an EditText in a previous Activity and put that in the above string.
I found that formatting from a Team Treehouse tutorial and it worked in that project's code as I followed along.
I'm using the String.format which takes in a Locale.
I am not sure exactly what is wrong as I mirrored the code to a "T" and just changed some variable names.
Seeing as the String class' length() method returns an int value, the maximum length that would be returned by the method would be Integer.MAX_VALUE, which is 2^31 - 1 (or approximately 2 billion.)
So you can have a String of 2,147,483,647 characters, theoretically. I don't think you should need much more than that.
However, Android system limits your heap space, going as low as 16 MB. Therefore, you'll never practically be able to reach the theoretical limit, and will max out with a String somewhere in the 4-64 million character range.
so, why are you thinking of a longer string variable, when you can directly use your name variable along with a string variable of long length.
for Ex:-
String meaning_text = "the very long message you wanted to show..."+ mYourName + "again a long message if you want to show";
so my suggestion is to use your name variable directly with message you wanted to show.
EditText edt=(EditText)findViewbyId(R.id.edt);
edt.setText("Dipali");
now,getting value from edittext and Replace in TextView String resource.
String strEdtTextValue=edt.getText().toString();
TextView tV=(TextView)findViewById(R.id.txtView)
Set text in textview as xml Resource like as
dummy="Hey %1$s, you chose this which means blah blah....."
add string in resource file
<string name="dummy">Hey %1$s, you chose this which means blah blah.....</string>
Resources res = getResources();
String dumyText= (res.getString(R.string.dummy),strEdtTextValue);
tv.setText(dumyText);
Its helpful to you.
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 :)
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);
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.