Is this the right way to set text in TextView programmatically?
points_txt.setText(R.string.you_have + current_points + R.string.points);`
It shows me a ResourcesNotFoundException error for the string while I can see the string in the strings.xml file.
points_txt.setText(getResources().getString(R.string.you_have) + current_points + getResources().getString(R.string.points));
You get a ResourcesNotFoundException because you're adding int values (resource identifiers are mapped to int values at compile time) instead of concatenating Strings.
The sum of the various resource identifiers might even be another valid resource identifier, but that would only happen accidentally. Nevertheless, if you pass an int value into setText(), the runtime tries to find a string resource by that number. In your case, it failed and so your app crashed.
So you have to get the Strings first and concatenate them afterwards:
points_txt.setText(getString(R.string.you_have) + current_points + getString(R.string.points));
points_txt.setText(R.string.you_have + current_points + R.string.points);
This is showing "ResourcesNotFoundException" because "R.string.you_have" is an integer value an "current_point" variable also is an int type
setText() requires the String type...
to get string value of "R.string.you_have" you can use
getResources().getString(R.string.you_have);
points_txt.setText(getResources().getString(R.string.you_have) + current_points + getResources().getString(R.string.points));
To get a string from strings.xml do this:
String you_have = getResources().getString(R.string.you_have);
You are almost there but I feel might be behind a few steps but not sure about this since you haven't shared all of your code.
You need to wire in the TextView first between your Java class and XML
TextView tv1 = (TextView)findViewById(R.i.d.textView1)
Next is setting the String for the textview
tv1.setText(getResources().getString(R.string.you_have) + "current_points" + getResources().getString(R.string.points));
You are basically missing the " " marks which are compulsory when you are assigning hardcoded string.
You must firstly parse that resource to string:
String string = getString(R.string.yourString);
More about that here:
how to read value from string.xml in android?
So answer for your question will be literally as following:
String you_have = getString(R.string.you_have);
String points = getString(R.string.points);
points_txt.setText(you_have + current_points + points);
Related
Im having trouble taking a number value from a string on a webpage im testing.
Im currently using:
String total_points = potential_points.getText();
System.out.println("Potential points are: " + total_points);
int pot_points = Integer.parseInt(total_points);
The problem is the selector is actually returning two strings aswell:
"Potential Points"
":"
"1476"
How can i extract the 1476 and use it as an int. Ive attached the html.
Potential Points: 1473
Entry:Free To Play
You need to get the result by using substring method as below
String total_points = potential_points.getText();
int startIndex=total_points.indexOf(":")+2;
int endIndex=total_points.length();
String result=total_points.substring(startIndex,endIndex);
int no=Integer.parseInt(result);
Simplified the above code as below
String result=total_points.substring(total_points.indexOf(":")+2,total_points.length());
int no=Integer.parseInt(result);
I want to show sentences for its number.
Getting number with EditText, and sentences are in string.xml
Name of strings are
sen_(number)
ex: sen_1, sen_25
I tried to make the code to String, so I did like this.
(sentence_num is getString of EditText) (sen_1 is "Hello, world!")
String getTxtString = "getText(R.string.sen_" + sentence_num + ")";
TextView scrambled_sen = (TextView)findViewById(R.id.scrambled_sentence);
scrambled_sen.setText(getTxtString);
But it shows getText(R.string.sen_1), not "Hello, world!".
How can I make it show string with its number?
I want to put getTxtString for a java code, not a String.
You could use getResources().getIdentifier() to get the string but a better and non-complicated way is through a switch case or the if else method.
Something like
If(editText value equals something){
return getString(R.string.sen_1);
}
Minimizes the errors that you could cause through the first getIdentifier() method too.
I have many string arrays in my resource files, and I want to access them programmatically depending on user input.
int c = Getter.getCurrentNumber();
String[] info = getResources().getStringArray(R.array.n_<c>);
So if c==12, info should be the string-array with name "n_12".
Is there a way to do this, and avoiding to do a switch statement with hundreds of cases?
Thanks
You can get the resource id like so
int c = Getter.getCurrentNumber();
String resource = "n_" + c;
int id = getResources().getIdentifier(resource, "array", "com.your.project");
Then just use that id
String[] info = getResources().getStringArray(id);
Have a look here for another example on getResources().getIdentifier().
If you want to get a resource by name (programmatically) and you are not sure how to address the resource by name (but you do know how to access it by R.), you can do this:
First print the exact resource name, like this:
Log.d("", context.getResources().getResourceName(R.id.whichYouAlreadyKnow) );
(Note: R.id.whichYouAlreadyKnow can be R.string.* R.drawable.* etc...)
Now you know the exact Resource address name
Take the printed name and use it as is, like this:
int id = getResources().getIdentifier(resource_name_that_printed_above, null, null);
Cheers
I am loading strings from a text file, eg;
Sunset Blvd 1950.ogg,Sunset Blvd,Paramount Pictures,1950,110,Billy Wilder,4,William Holden,Gloria Swanson,Erich von Stroheim,Nancy Olson
Now I have a class setup that extends from 2 parent classes (Media > Video > Avi/Ogg/etc). And that class holds the following variables;
public Avi(String title, String fileName, int releaseYear, String studio, String director, String castNames, double runtime, int cast) {
super(title, fileName, releaseYear, studio, director, castNames, runtime, cast);
}
Now I load the text file in using a buffer reader and a loop, but heres the problem, the cast names (Which come last in the text file, are also separated with commas but since I am using a splitter already I am not sure how to get every cast member into a simply string such as "Larry Davis,Eddy Murphy,Etc Etc" that can be returned later on. Also using a different splitter for cast names is not an option
if your cast starts at William Holden, you can do
line.split(",", 8);
I assume that by splitter you mean the String method "split".
If so, does your text file always have the same structure ?
Meaning is there always the same number of elements before the cast names ?
Because the String method "split" can take a second parameter specifying the number of elements to retrieve (cf. link to String API)
None of these solutions worked but heres what I came up with that worked:
String castNames = "";
int splitLength = split.length - 7;
for (int i = 0; i < splitLength; i++) {
castNames += split[7 + i] + ",";
}
Avi avi = new Avi(split[1]/*title*/
, split[0]/*filename*/
, Integer.parseInt(split[3])/*releaseyear*/
, split[2]/*studio*/
, split[5]/*director*/
, castNames/*castnames*/
, Double.parseDouble(split[4])/*runtime*/
, Integer.parseInt(split[6])/*cast*/);
return avi;
Having a symbol as the string separator as well as being valid data is not a good idea and results in code that is prone to errors. Of course you can work around that - some people before me have suggested ways to do it - but I strongly recommend that you change your input and remove the ambiguity.
I am newcomer to programming and I am attempting to create an Android app using Android Studio. I've tried searching but my findings do not appear to be what I am looking for, because they seem to be overly complex. What I've written below is just an example.
I want to be able to return a string from string.xml when user types "whale". The string in this case is information about the whale.
This is my java file, animal is already a string entered from a form.
TextView textview = new TextView(this);
String animalType = "water_" + animal; // This become water_whale if user typed whale
String animalInfo = getString(R.string.animalType); // This doesn't work
textView.setText(animalInfo);
This is my string.xml
<string name="water_fish">Fish is a small bla...</string>
<string name="water_whale">A whale is an enourmous blabla...</string>
<string name="land_giraffe">Africa.</string>
I have probably tunneled on this particular way and I have probably miss something obvious or is there another way to do this?
R.string.anyIdentifer represents an integer value. You can't add your own identifier with it, just the way you can't call any non existent property on any class. If you want to access any resource dynamically with it's name then there is a different approach for that.
Use this
TextView textview = new TextView(this);
String animalType = "water_" + animal;
int animalTypeId = getResources().getIdentifier(animalType, "string", getActivity().getPackageName())
String animalInfo = getResources().getString(animalTypeId);
textView.setText(animalInfo);
String string=getResources().getString(R.string.water_whale);
you can't use getString() method directly.