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

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>

Related

in string array to use multi language

I want to use multi language in my app but cant solve problem with array; I couldn't define text from XML here. So what do you advise me to do here so that i can use multi language ? thanks a lot
public static final String[] columns = { "Bluetooth", "WiFi",
"Mobile Networks", "Auto Sync", "Gps", "Auto-rotate screen",
"Vibrate on touch", "Airplane mode", "Brightness", "Sleep",
"Volume Settings", "Phone Ringtone", "Uninstall",
"Backup & Restore", "Battery Usage", "Cache Clear", "System Clear",
"System Info" };
}
You can define string array in strings.xml, as the documentation says like:
<resources>
...
<string-array name="numbers">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
</string-array>
...
</resources>
Than you can get it from code like :
String [] fiilliste = getResources().getStringArray(R.array.numbers);
Don't forget to define your array in every strigns.xml in each of your values-XX folder which you want to support.
I think the best approach is to use R.string.whateveryouwant as Integer array, something like this:
Integer[] columns = { R.string.bluetooth, R.string.wifi, .... }
And when you display the text get the value from the language xml file, given the Integer key.

Android integer within listpreference

I have a preference xml file and a listpreference.
The listpreference entryValues and entries are in a array.xml file.
Here's the problem, the entries/entryValues contain "10 mb/s" and i would like to get the int value from that entry/whatever is selected.
This gives me an error however, here is the code:
TextView t = (TextView)findViewById(R.id.textView1);
String result = sp.getString("BITRATE", "8");
int i = Integer.parseInt(result.substring(result.lastIndexOf(" mb/s")));
t.setText("Int value: " + i);
As i said this gives me an error and i cannot find the issue.
Thanks for any help!
FINAL FIXED CODE
TextView t = (TextView)findViewById(R.id.textView1);
String result = sp.getString("BITRATE", "3");
int intRes = Integer.parseInt(result);
t.setText("Int value: " + intRes);
Also i changed the preferences entryValues items to only be integers instead of ex 10mb/s to just 10.
Make your entryValues array containing only the integers. The entries are used when displaying to the user, the entryValues are what is stored in your preferences (and are not displayed). The two arrays do not have to have the same contents, but they should have the same number of items.
<string-array name="bitrate_entries">
<item>10 mb/s</item>
<item>25 mb/s</item>
<item>50 mb/s</item>
</string-array>
<string-array name="bitrate_entry_values">
<item>10</item>
<item>25</item>
<item>50</item>
</string-array>
Now you only need to use Integer.parseInt()

eclipse android setText to random String

Hello I have 2000 Questions each with 3 possible answers in my Strings.xml. I want that a random question is displayed in the textview with is answers.
<string name="Frage1">Was versteht man unter defensivem Fahren?</string>
<string name="ersteAntwort1">Nicht auf dem eigenen Recht bestehen</string>
<string name="zweiteAntwort1">Mit Fehlern anderer rechnen</string>
<string name="dritteAntwort1">Vorsorglich an jeder Kreuzung anhalten</string>
<string name="Frage2">Was kann zu Auffahrunfällen führen?</string>
<string name="ersteAntwort2">Unerwartet starkes Bremsen</string>
<string name="zweiteAntwort2">Unaufmerksamkeit</string>
<string name="dritteAntwort2">Zu dichtes Auffahren</string>
<string name="Frage3">Sie fahren innerorts hinter einem Fahrzeug mit ortsfremdem Kennzeichen. Was könnte geschehen?</string>
<string name="ersteAntwort3">- bremst unerwartet</string>
<string name="zweiteAntwort3">- betätigt den Blinker vor dem Abbiegen zu spät</string>
<string name="dritteAntwort3">- hält unerwartet an, um nach dem Weg zu fragen</string>
The names of the Strings are always the same only the number of the question changes so i want to add a random number to the first part of the string name
public void neueFrage (View view) {
Button buttonTipp = (Button)findViewById(R.id.button1);
final TextView tw = (TextView)findViewById(R.id.textView1);
final CheckBox Chk1 = (CheckBox)findViewById(R.id.checkBox1);
final CheckBox Chk2 = (CheckBox)findViewById(R.id.checkBox2);
final CheckBox Chk3 = (CheckBox)findViewById(R.id.checkBox3);
buttonTipp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int random = (int) (Math.random() *3 );
String zahl = String.valueOf(random);
String question = "Frage"+zahl;
String firstAnswer ="ersteAntwort"+zahl;
String secondAnswer = "zweiteAntwort"+zahl;
String thirdAnswer = "dritteAntwort"+zahl;
tw.setText(R.string.question);
Chk1.setText(R.string.firstAnswer);
Chk1.setText(R.string.secondAnswer);
Chk1.setText(R.string.thirdAnswer);
}});
}
First of all, if you can, write the data in an appropriate format. XML would be a good choice, you could create your own XML format and read it. An example XML format could be as follows, Android does include some excellent parsers as well.
<questions_list>
<question name="Question 1?">
<answer name="Answer 1"/>
<answer name="Answer 2">
<correct/>
</answer>
<answer name="Answer 3"/>
</question>
</question_list>
If that doesn't work, maybe a String Array could work, like this:
<string-array name="question1">
<item>Question 1</item>
<item>Answer 1</item>
...
</string-array>
A third option would be to put the data into a SQLite database, probably with 5 columns, question, 3 answers, and the correct answer. There's a lot out there on how to do this, the easiest way is to use a language like Python to populate the database, then use something like SQLiteAssetHelper to put the database in to your program.
I think re-formatting will save you considerable work. XML, a database, or something else, but I'm sure a better format would help you considerably.
As it stands, you would have to reference not only the questions directly in the code, but also the answers. The tool simply isn't meant to be used the way you're using it.

How to underline the string in a string-array

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

List Preferences is returning Entries and not EntryValues

I created a List Preference in my Preferences Activity and the value that the code is returning for the selected item is the value of the Entry and not the value of the EntryValues.
Example:
Entry: David New
Value: DAVIDNEW.TTF
The codes returns "David New" instead of "DAVIDNEW.TTF".
Here's the code:
File preferences.xml:
<ListPreference
android:key="fontSelect"
android:title="#string/textPrefs"
android:summary="#string/textPrefs"
android:defaultValue="DAVIDNEW.TTF"
android:entries="#array/fonts"
android:entryValues="#array/fontsValues" />
File arrays.xml:
<string-array name="fonts">
<item>Alex</item>
<item>Cardo</item>
<item>Chaya</item>
<item>David New</item>
<item>Droid Sans</item>
<item>Frank</item>
<item>Mike Hebrew</item>
</string-array>
<string-array name="fontsValues">
<item>ALEX.TTF</item>
<item>Cardo99s.ttf</item>
<item>CHAYA.TTF</item>
<item>DAVIDNEW.TTF</item>
<item>DroidSansHebrew.ttf</item>
<item>frank.ttf</item>
<item>mike_hebrew_regular_v33.ttf</item>
</string-array>
File reader.java:
String f1 = new String(myprefs.getString("fontSelect", "DAVIDNEW.TTF"));
font = Typeface.createFromAsset(getAssets(), f1);
tv.setTypeface(font);
Value of f1 is "David New" instead of "DAVIDNEW.TTF".

Categories

Resources