I have an ArrayAdapter<String> that I am using to show a single choice Dialog like this:
dialogBuilder.setAdapter(arrayAdapter ...
This is the ArrayAdapter:
arrayAdapter = new ArrayAdapter<String>(StartActivity.this, android.R.layout.select_dialog_singlechoice);
arrayAdapter.addAll("A Tropical Rainforest", "Backwater Chorus", "Big River", "Bird Song 1", "Bird Song 2", "Cave Ambience", "Cold Stormy Wind",
"Crickets", "Deep Woods", "Fireplace", "Jungle River", "Long Soothing Rain", "Ocean Waves 1", "Ocean Waves 2",
"Pacific And Songbirds", "Pig Frogs", "Small Green Froggies", "Small Rapid", "Sparkling Water", "Stormy Wind",
"Thunder and Rain", "Thunderstorm Inner Perspective", "Twilight", "Waterfall", "Wind");
Now the problem is that I want to make this a multi language app which means I need to get all of this strings from String Resources. But I can't do it since a String Resource returns int while I can only insert String in there.
Any idea how I can do this?
Create an array resource
<resources>
<string-array name="values">
<item>A Tropical Rainforest</item>
<item>Backwater Chorus</item>
<item>Big River</item>
<item>Bird Song 1</item>
<item>And so on</item>
</string-array>
</resources>
And now, you can retrieve the values from code like this:
String values[] = getResources().getStringArray(R.array.values);
arrayAdapter.addAll(values);
First, define a string array in resources:
<string-array name="nature_things_array">
<item>A Tropical Rainforest</item>
<item>Backwater Chorus</item>
<item>Big River</item>
<item>Bird Song 1</item>
</string-array>
Then set single choice items to AlertDialog.Builder:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color)
.setMultiChoiceItems(R.array.nature_things_array, null,
new DialogInterface.OnSingleChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// ...
}
})
});
return builder.create();
You can find a good android dialog manual here.
Related
On Android, how to get a word (for example Blue), compare the word with each item of a Spinner, and if the word is equal to a Spinner item (on this case, if there is blue in an Spinner item), the system automatically puts selected the item with the word on the screen, that is, on this case, the system automatically shows blue on the spinner?
Remembering what is Spinner in Android: https://dds861.medium.com/simple-spinner-874a65834422
The code to transform the selected item of a Spinner and transform into String is:
<string-array name = "color">
<item>Click here to choose your preferred color</item>
<item>Blue</item>
<item>Red</item>
<item>Yellow</item>
<item>Green</item>
</string-array>
Spinner colorField = findViewById(R.id.spinner_color);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.color,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
colorField.setAdapter(adapter);
colorField.setOnItemSelectedListener(this);
final String colorIntoString = colorField.getSelectedItem().toString();
But how to make the inverse?
It doesn't necessarily have to be in that code.
Does anyone know of any code to get a String, compare all items from a Spinner, and return the Spinner item that is equal to String?
You can get your string array like this:
String[] colors = getResources().getStringArray(R.array.color);
Then you can compare each element with your desired string.
String color = "blue";
for(int i = 0; i < colors.length; i++) {
if (colors[i].equals(color)) {
colorField.setSelection(i); // sets the selection
break;
}
}
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.
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()
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.
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...)