Show text according to Random Textview - android

I am showing Random text in the two textview's on button click. Now I have to show the explaination of the text showed in textview1 in textview2.But I am not able to get it. Help will be appreciated.
I put the text in string.xml
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="ten">10</string>
<string name="one_explaination">This is number one</string>
<string name="two_explaination">This is number two</string>
<string name="three_explaination">This is number three</string>
<string name="four_explaination">This is number four</string>
<string name="five_explaination">This is number five</string>
<string name="six_explaination">This is number six</string>
<string name="seven_explaination">This is number seven</string>
<string name="eight_explaination">This is number eight</string>
<string name="nine_explaination">This is number nine</string>
<string name="ten_explaination">This is number ten</string>
My code in MainActivity:
Random number,number_explaination;
int [] array_number,array_number_explaination;
int textview_number,textview_number_explaination;
TextView textView1,textView2;
number = new Random();
array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
R.string.eight,R.string.nine,R.string.ten};
textview_number = number.nextInt(array_number.length - 1);
textView1.setText(array_number[textview_number]);
number_explaination = new Random();
array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
textview_number_explaination = number_explaination.nextInt(array_number_explaination.length - 1);
textView2.setText(array_number_explaination[textview_number_explaination]);
What I want here if I get Random R.string.two in my textview1 and then I will get R.string.two_explaination and so on.How can I achive this. Sorry for my bad english.

try this
Random number,number_explaination;
int [] array_number,array_number_explaination;
int textview_number,textview_number_explaination;
TextView textView1,textView2;
number = new Random();
array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
R.string.eight,R.string.nine,R.string.ten};
array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
textview_number = number.nextInt(array_number.length - 1);
textView1.setText(getResources().getString(array_number[textview_number]));
textView2.setText(getResources().getString(array_number_explaination[textview_number]));
You do not need to generate another random number for explanation text

Try this:
textView2.setText(getString(array_number_explaination[textview_number_explaination]));
getString(int) should be used to get a string in your string resource file.

you need to use string array in the strings file :
<string-array
name="string_array_name">
<item
>text_string</item>
</string-array>
and then make an array in your code with reference of :
R.array.string_array_name
see http://developer.android.com/guide/topics/resources/string-resource.html for reference.
after you got the two arrays in your code you can run loops and do your desired work

Related

Android show plurals in both integer and float

My target is to show text like "1 star" "2.5 stars" "3 stars".
which is remove .0 for float value like 1.0, 2.0 etc. But for the value with .5 show the float value 2.5 3.5 etc.
Is this possible to use android plurals ?
I used this way but doesn't work.
plurals.xml
<plurals name="hotel_card_rating_text">
<item quantity="zero">#string/text_zero</item>
<item quantity="one">#string/text_one</item>
<item quantity="two">#string/text_two</item>
<item quantity="few">#string/text_few</item>
<item quantity="many">#string/text_many</item>
<item quantity="other">#string/text_other</item>
</plurals>
strings.xml
<resources>
<string name="text_zero">%.1f stars</string>
<string name="text_one">1 star</string>
<string name="text_two">%.1f stars</string>
<string name="text_few">%.1f stars</string>
<string name="text_many">%.1f stars</string>
<string name="text_other">%.1f stars</string>
</resources>
test code
DecimalFormat format = new DecimalFormat();
float rating = getRating();
getStarText().setText(getContext().getResources().getQuantityString(R.plurals.hotel_card_rating_text, (int) rating, format.format(rating)));
}
strings.xml use %s instead of %.1f
<resources>
<string name="text_zero">%s stars</string>
<string name="text_one">1 star</string>
<string name="text_two">%s stars</string>
<string name="text_few">%s stars</string>
<string name="text_many">%s stars</string>
<string name="text_other">%s stars</string>
</resources>
test code
DecimalFormat format = new DecimalFormat("#.#"); // format the number
float rating = getRating();
getContext().getResources()
.getQuantityString(R.plurals.hotel_card_rating_text, (int) rating, format.format(rating)));
Be careful when you use plurals. It has its own issues, see below:
Plural definition is ignored for zero quantity
Issue 8287: PluralRules does not handle quantity "zero"
you can give it a try to below snippet which is very typical
String somePostFixText = "star";
String output;
double starCount;
if (starCount > (double)1)
somePostFixText = somePostFixText+"s";
if(starCount == (long) starCount){
output = String.format("%d",(long)starCount)+" "+somePostFixText;
} else {
output = String.format("%s",starCount)+" "+somePostFixText;
}
//do whatever you want to do with output variable
Happy Coding!

Text not used again if it used once in Random Method

I am using random text from the string.xml to my textview through Random method. Now I don't want to use the text again if it has been used once. Help will be appreciated.
I put the text in string.xml
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="ten">10</string>
<string name="one_explaination">This is number one</string>
<string name="two_explaination">This is number two</string>
<string name="three_explaination">This is number three</string>
<string name="four_explaination">This is number four</string>
<string name="five_explaination">This is number five</string>
<string name="six_explaination">This is number six</string>
<string name="seven_explaination">This is number seven</string>
<string name="eight_explaination">This is number eight</string>
<string name="nine_explaination">This is number nine</string>
<string name="ten_explaination">This is number ten</string>
My code in MainActivity:
Random number,number_explaination;
int [] array_number,array_number_explaination;
int textview_number,textview_number_explaination;
TextView textView1,textView2;
number = new Random();
array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
R.string.eight,R.string.nine,R.string.ten};
array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
textview_number = number.nextInt(array_number.length - 1);
textView1.setText(getResources().getString(array_number[textview_number]));
textView2.setText(getResources().getString(array_number_explaination[textview_number]));
LinkedList<Integer> number=new LinkedList<Integer>();
LinkedList<Integer> number_exp=new LinkedList<Integer>();
//add data
int randomIndex=(int) (Math.random()*(strs.size()-1));
textview1.setText(number.get(randomIndex));
textview2.setText(number_exp.get(randomIndex));
number.remove(randomIndex);
number_exp.remove(randomIndex);

Android get random string by the xml string

Who i can get a random string from the xml string folder?
My xml loks like:
<string name="pr1">Question 1</string>
<string name="rs1.1">Aswer 1</string>
<string name="rs1.2">Aswer 2</string>
<string name="rs1.3">Aswer 3</string>
<string name="rs1.4">Aswer 4</string>
<string name="pr2">Question 2</string>
<string name="rs2.1">Aswer 1</string>
<string name="rs2.2">Aswer 2</string>
<string name="rs2.3">Aswer 3</string>
<string name="rs2.4">Aswer 4</string>
And i want do something like this:
Random r = new Random();
int num=r.nextInt(2);
TextView aswer= (TextView) findViewById(R.id.textView);
Button botao1 = (Button) findViewById(R.id.button3);
botao1.setText("#string/rs"+num+".1");
aswer.setText("#string/pr"+num);
But the input of the TextView is "#string/pr1", but i want the string of the xml who have the name "pr1". Please help. Thanks.
What you want is to get the id of the resource by name and luckily there is method for this:
getIdentifier
So, you should modify your code like this:
botao1.setText(getResources().getIdentifier("rs" + num + ".1", "string", getPackageName());

Format an array of strings with arguments

I'd like to format an array of strings just like android used to format strings:
Usually we do:
strings.xml
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In some java code:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
I'm looking for something like:
in some arbitrary xml:
<string-array name="employee">
<item>name: %1$s</item>
<item>post: %2$s</item>
</string-array>
in some java code:
Resources res = getResources();
String[] employee = ArrayString.format(res.getStringArray(R.string.employee), name, post);
Is there an elegant way to do that?
EDIT:
The next pieces of code is a workaround and I'm posting it just to help #Sufian, who asked for it in a comment. It's not a real answer once my question is about format the string array's content and the bellow code is formatting each string separately.
In some misc.xml:
<string-array
name="string_array">
<item>1st position: %1$d</item>
<item>2nd position: %1$d</item>
</string-array>
Then, in java code:
res = getResources();
String[] sa = res.getStringArray(R.array.string_array);
for (int i = 0; i < sa.length; i++ ) {
text += String.format(sa[i], i);
}
Just use:
String text = String.format(res.getStringArray(R.array.myStringArray)[index], param1, param2);
getQuantityString may solve your problem.
Look at quantity strings in http://developer.android.com/guide/topics/resources/string-resource.html
Here's the specific API doc:
http://developer.android.com/reference/android/content/res/Resources.html#getQuantityString(int,%20int,%20java.lang.Object...)

NullpointerException on string-array android xml

Good day, Any idea why i could be getting a NullPointerException in the following code. I am trying to retrieve elements from a string-array in XML and test against a given value but i keep getting a NullpointerException on the first line of the code below.
String [] position = context.getResources().getStringArray(R.array.google_weather_conditions); // NullpointerException here
for(String check: position){
String test_condition = check;
if(this.condition.equals(test_condition)){
weather_conditions = test_condition;
}
}
here is the strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="Clear">Clear</string>
<string name="Sunny">Sunny</string>
<string name="Mostly_Sunny">Mostly Sunny</string>
<string name="Partly_Sunny">Partly Sunny</string>
<string name="Cloudy">Cloudy</string>
<string name="Mostly_Cloudy">Mostly Cloudy</string>
<string name="Partly_Cloudy">Partly Cloudy</string>
<string name="Rain">Rain</string>
<string-array name="google_weather_conditions">
<item>#string/Clear</item>
<item>#string/Sunny</item>
<item>#string/Mostly_Sunny</item>
<item>#string/Partly_Sunny</item>
<item>#string/Cloudy</item>
<item>#string/Mostly_Cloudy</item>
<item>#string/Partly_Cloudy</item>
<item>#string/Rain</item>
</string-array>
</resources>
Most likely context is null at the point of the NPE.
context.getResources().getStringArray(R.array.google_weather_conditions); // NullpointerException here
The following could be null:
context
return value of context.getResources()
R
R.array
R.array.google_weather_conditons
Time to either step through with a debugger, or break it down, line by line and see where it occurs.
What is R?

Categories

Resources