How to underline the string in a string-array - android

I have a string-array in strings.xml. How can I underline the string in item?
For string we do like this..
<string name="clickable_string">This is a <u>clickable string</u></string>
I tried something like this.
<string-array name="products">
<item>this is a <u> clickable</u> string </item>
<item> <u> clickable</u> string </item>
<item>this is a <u> clickable</u> string </item>
</string-array>
Its not working. How can I do that?
Thank you

Try this way it will work
<string-array name="description">
<item> <Data> <![CDATA[ Check this <u>Redirect to Next Activity</u> ]]></Data> </item>
</string-array>
In java Code use this :
ArrayList<String> title_list = new ArrayList<String>();
String[] description_Array = getResources().getStringArray(R.array.description);
String categoryAndDesc = null;
for(String cad : description_Array) {
categoryAndDesc = cad;
title_list.add(categoryAndDesc);
}
CharSequence sequence = Html.fromHtml(categoryAndDesc);
seperator_view.setText(strBuilder);
seperator_view.setMovementMethod(LinkMovementMethod.getInstance());

Try this:
<string name="clickable_string">This is a <u>clickable string</u></string>
(i.e) Replace the < and > with &lt&semi; and &gt&semi; in your strings.xml for HTML formatted content.
And in your code use:
String text = context.getString(R.string.clickable_string);
myTextView.setText(Html.fromHtml(text));

Related

Android getString issue

So I have many strings in strings.xml, they are recorded in a format of:
<string name="list_1">xxxxxxxxxxx</string>
<string name="list_2">xxxxxxxxxxx</string>
<string name="list_3">xxxxxxxxxxx</string>
......
Now I want to load them one by one without having to type all the string IDs. I want to load them in a fashion like:
for (int i = 1; i <= num; i++) {
// Just showing what I mean.
String xxx = getString(R.string.("list_" + i));
}
Is there a method to do so?
Try this:
int resourceID = getResources().getIdentifier("list_" + i, "string", getPackageName());
String xxx = getString(resourceID);
It would be worth to use resource arrays. For example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
and you can access, iterate as follows:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

Show text according to Random Textview

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

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());

Both \n and \r\n get the same result in PreferenceScreen

I have set android:defaultValue="\n", but when I open the PreferenceScreen, I find the third item is selected, I think the first item should be selected.
And more, I find more surprising thing. even if I select the third item, but the value String newLineValue = prefs.getString("SetNewLine", "\n") is "\n", it should be "\r\n".
Why? Thanks!
private String GetNewLine(){
SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String newLineValue = prefs.getString("SetNewLine", "\n");
if (newLineValue.equals("\n")){
Toast.makeText(getApplicationContext(),"OK" ,Toast.LENGTH_SHORT).show();
}
return newLineValue;
}
array.xml
<string-array name="NewLine">
<item>Make new line with \\n</item>
<item>Make new line with <br/></item>
<item>Make new line with \\r\\n</item>
</string-array>
<string-array name="NewLine_values">
<item>\n</item>
<item><br/></item>
<item>\r\n</item>
</string-array>
mypreference.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="AppPreference"
android:summary="#string/Preferencesummary"
android:title="#string/Preference" >
<ListPreference
android:defaultValue="\n"
android:dialogTitle="#string/NewLineFormat"
android:entries="#array/NewLine"
android:entryValues="#array/NewLine_values"
android:key="SetNewLine"
android:summary="#string/NewLineFormatsummary"
android:title="#string/NewLineFormat"
android:layout="#layout/mypreference_layout"
/>
</PreferenceScreen>

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...)

Categories

Resources