Reputation: 793
I have a question regarding mutiple languages for Android Apps. I know it is common pratice to use resource files for your strings (Android Studio always reminds you of this if you have forgotten). However, I am wondering how to do this for the following two elements:
How would you handle these issues? I'd appreciate every comment.
Update: Here is an example of a String variable that I use in the code (but not in a XML-Layout file). It is the string 'Comment' in an AlertDialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Comment");
// Set up the input
final EditText input = new EditText(this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
input.setSingleLine(false);
input.setLines(3);
input.setText(comment_Text);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
comment_Text = input.getText().toString();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
Upvotes: 0
Views: 109
Reputation: 818
You can localize your drawables as well.
You can do this by creating a folder for a specific language, the same way as you would localize the string resources.
Example of a drawable for the default locale:
res/drawable-hdpi/country_flag.png
Example of a drawable for a different locale (es_ES)
res/drawable-es-rES-hdpi/country_flag.png
For more information please check out the official documentation: https://developer.android.com/training/basics/supporting-devices/languages
Concerning you second question; your classes should also make use of the string resources, don't use hardcoded strings.
An example of how to do this:
In this case the string resource file would contain:
<string name="title_comment">Comment</string>
<string name="button_ok">OK</string>
<string name="button_cancel">Cancel</string>
The code for your AlertDialog would then look like this:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.title_comment);
final EditText input = new EditText(this);
input.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
input.setSingleLine(false);
input.setLines(3);
input.setText(comment_Text);
builder.setView(input);
builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
comment_Text = input.getText().toString();
}
});
builder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
Upvotes: 2