Issue
Hello there.
I want to help me . I am a beginner in programming android Studio. I want to pass a "specific picture" in Android Studio, According to the default language of the phone.
Example: I have two pictures, picture 1: it has Arabic writing. Picture 2: It has English writting .
Q . I want to use the conditionals "if" and "else if" in the statement?
EX : if(the language is Arabic){pass the picture 1 } else if(the language is English){pass the picture 2 }.....and so on to multiple languages.
- Please, I want an answer to this algorithm.
Solution
There are 2 ways to do this:
First way, using switch
and case
( more efficient ):
yourImageViewID.setImageResource(R.drawable.image_for_other_languages);
switch(Locale.getDefault().getLanguage()) {
case "ar": {
//"ar" is the code of the Arabic language
yourImageViewID.setImageResource(R.drawable.arabic_image);
break;
}
case "en": {
//"en" is the code of the English language
yourImageViewID.setImageResource(R.drawable.english_image);
break;
}
}
Second way, using if else
( less efficient ):
yourImageViewID.setImageResource(R.drawable.image_for_other_languages);
if (Locale.getDefault().getLanguage().equals("ar")) {
yourImageViewID.setImageResource(R.drawable.arabic_image);
}
else if (Locale.getDefault().getLanguage().equals("en")) {
yourImageViewID.setImageResource(R.drawable.english_image);
}
In both ways, yourImageViewID
is an ImageView
, Don't forget to add 3 images to your assets, one for the Arabic language, another one for the English language, and a third one for other languages ( will be used if device language isn't Arabic or English ), also add more images if you want to add more languages to your application
Answered By - Omar Hemaia