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.
CodePudding user response:
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
CodePudding user response:
You can do this by adding each picture to a different local version within res\drawable
. Bot versions of pictures must have the same name in order to allow Android system to to the appropriate picture when checking the device language.
In your example your locals are English & Arabic, so you need to have two folders:
\app\src\main\res\drawable-ar\myPicture.png
<< Arabic version\app\src\main\res\drawable-en\myPicture.png
<<< English version
Both pictures have the same name but they are different in content as you need.