skip to Main Content

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.

2

Answers


  1. 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

    Login or Signup to reply.
  2. You can do this by adding each picture to a different locale version within resdrawable. Both versions of pictures must have the same name in order to allow Android system to pick the appropriate picture when checking the device language.

    In your example your locales are English & Arabic, so you need to have two folders:

    • appsrcmainresdrawable-armyPicture.png << Arabic version
    • appsrcmainresdrawable-enmyPicture.png <<< English version

    Both pictures have the same name but they are different in content as you need.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search