I´ve got the problem, that Android Studio reminds me to use the string.xml for set the text. The following gives me the hint: Do not concatenate text displayed with setText. Use resource string with placeholders.
public int points = 0;
public TextView tv_points;
tv_points.setText("Points: " + points);
Okay if i use the string xml with this code it does not what i want:
<string name="points_string">Points: </string>
public int points = 0;
public TextView tv_points;
tv_points.setText((R.string.points_string) + points);
It brings no error and no hint, but is wrong. I do not get the wanted effect to set the points.
You need to format the string using strings.xml like this:
<string name="points_string">Points: %1$d</string>
Then you can use it like this:
tv_points.setText(getResources().getString(R.string.points_string, points));
Related
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);
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'm currently developing android quiz app and I implemented the "reset" button that resets the questions and randomize them, but I'm having trouble with calling the random Strings from .xml file in MainActivity.java.
I have all of my questions listed in the Strings.xml file like this, in order to make them easy to translate:
<string name="q1">The reactor at the site of the Chernobyl nuclear disaster is now in which country?</string>
<string name="a1">A. Slovakia</string>
<string name="b1">B. Ukraine</string>
<string name="c1">C. Hungary</string>
<string name="d1">D. Russia</string>
<string name="q1answer">B</string>
All of the questions are listed in that file like q1, q2, q3, and the same goes for ABCD answers: a1, b1, c1, d1; a2, b2, c2, d2, and so on.
There are many questions, but button chooses only 5 of them and displays them on the screen. The problem is that I just can't access the Strings if I want to use an integer generated by randomizer to find them in .xml:
for (int i = 1; i <= 5; i++) {
// I managed to get the identifier of the TextView with the for loop like that (TextViews for questios are listed q1q, q2q, q3q, q4q, q5q):
// I would like to do the same thing down there with Strings.
TextView question = (TextView) findViewById(getResources().getIdentifier("q" + i + "q", "id", this.getPackageName()));
// randomQuestion is the number of the question in random number generator from different method.
randomQuestion = randomizeNumbers();
// And here I'm stuck, this will show "q1-randomNumber" instead of the real question,
// because it does not see it as an ID. I tried various different solutions, but nothing works.
question.setText("q" + randomQuestion);
// I left the most silly approach to show what I mean.
}
What can I do to make the computer distinguish the name of the String? So it displays the real question instead of "q1" "q6" "q17"?
Thank you for help in advance!
You can access strings from the string.xml file by using the getIdentifier() utility.
private String getStringByName(String name) {
int resId = getResources().getIdentifier(name, "string", getPackageName());
return getString(resId);
}
It would be a lot easier if you also tried another approach entirely and create a Problem class which would look something like :
public class Problem(){
int ID;
String question;
String answer1; String answer2; //..and so on
String answerCorrect;
public class Problem(int ID, String question, ...){
this.question= question;
...
}
}
And than, create and ArrayList = new ArrayList<>(); and populate it with your questions. Than, you would have the ability to access them by their ID / position in array, search them by name and so on. In the future, you can use this model to request the list of problems from a server too.
question.setText("q" + randomQuestion);
In here, your parameter is inferred as a string since you pass "q", so it will display "q-randomNumber" as expected. I think what you want is to pass and actual ID to setText. To do this, you can do the same approach you did to find the question textview ID using "string" instead of "id".
In the MainActivity or in a function :
Resources res = getResources();
myString = res.getStringArray(R.array.YOURXMLFILE);
String q = myString[rgenerator.nextInt(myString.length)];
// WE GET THE TEXTVIEW
tv = (TextView) findViewById(R.id.textView15);
tv.setText(q);
// WE SET THE TEXTVIEW WITH A RANDOM QUESTION
And declare :
private String[] myString;
private static final Random rgenerator = new Random();
private TextView tv;
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.
I am looping through a list of checkboxes upon click of a button. What I am looking to do is grab the name of the checkbox at runtime to strip out an integer value specified within the name. I am not looking to get the value nor the id. So in the strings.xml file, <string name="checkList1">Pain assessment.</string>, I am trying to get checkList1 at run time. I can get the text without a problem. Currently I am looping through the view elements with the code below:
RelativeLayout root = (RelativeLayout)findViewById(R.id.Root);
for (int i = 0; i < root.getChildCount(); i++)
{
View v1 = root.getChildAt(i);
Class c = v1.getClass();
if (c == CheckBox.class)
{
CheckBox thisBox = (CheckBox)v1;
if (thisBox.isChecked())
{
String text = (String)thisBox.ge;
DoDailyCheckListUpdate(thisBox.isChecked(),checkBoxCount);
countItemsFinished++;
}
checkBoxCount++;
}
}
What I am looking for is to somehow get the name of Checkbox thisBox. So when it loops through and hits the Pain Assessment checkbox, I want to be able to pull out checkList1. Without going as far as ripping through the strings.xml file based on the text I find to get the name, I was hoping maybe there was a simpler solution that I maybe overlooking.
Thank You in advance.
CheckBox extends from TextView so to retrieve a text from it is quite simple :
String text = thisBox.getText().toString();
http://developer.android.com/reference/android/widget/CheckBox.html
If you want to retrieve the key name of the string. I suggest you put it into the tag of the object :
thisBox.setTag(getResources().getResourceEntryName(R.string. checkList1);
Retrieve it like that :
String text = (String)thisBox.getTag();
that should do the trick.