Change Image Resource As Per Culture in Android

Many times we have to add different language support for the customers. Based on that locale we have to load the resources. This example will show you how to change the image resources as per the locale.


Note: You have to give the same name to the resources that is change as per the locale.

For Example, If you have two flags one for Australia and one for India then both of these flags have the same name for example ic_flag or any you want. (Shown below in this example avoid for now)       

First, you have to add your string in the string.xml file.

string.xml ( For English)
<resources>    
<string name="app_name">MultiLanguageSupportDemo</string>   
<string name="str_welcome">Welcome</string>   
<string name="str_username">Username</string>
<string name="str_password">Password</string>    
<string name="str_login">Login</string>   
<string name="str_sign_up">Don\'t have an account ? Sign Up Here</string>    <string name="change_language">Change Language</string>
</resources>

To create string.xml for Hindi follow the below step:-

- Create a new directory in res and name it as values-hi.
- Copy the string.xml file in it change the text as below.

resource folder

string.xml (For Hindi)
<resources>
<string name="str_welcome">स्वागत</string>
   
<string name="str_username">उपयोगकर्ता नाम</string>
    
<string name="str_password">पासवर्ड</string>
   
<string name="str_login">लॉग इन</string>
   
<string name="str_sign_up">एक खाता नहीं है? पंजी यहॉ करे</string>

<string name="change_language">भाषा बदलो</string>

</resources>


To create mipmap for Indian follow below step:-

- Create a new directory in res and name it as mipmap-hi.
- Copy the drawable (Indian Flag Image) file in it give the ic_flag.


To create mipmap for Australia follow below step:-

 - Create a new directory if not exists in res and name it as mipmap-hdpi.

 - Copy the drawable ( Australia Flag Image ) file in it give the name ic_flag.

                     



Code :

 activity_Main.xml

<layout>

<RelativeLayout>        

<TextView          
 android:id="@+id/act_login_tv_change_language"            
android:layout_width="wrap_content"            
android:layout_height="wrap_content"           
android:layout_alignParentEnd="true"            
android:text="@string/change_language"            
android:textSize="17dp" />        

<LinearLayout           
android:layout_width="match_parent"            
android:layout_height="wrap_content"            
android:layout_centerInParent="true"            
android:gravity="center"           
android:orientation="vertical">           

<TextView               
android:layout_width="wrap_content"              
android:layout_height="wrap_content"                
android:text="@string/str_welcome"               
android:textSize="32dp" />            

<EditText                
android:id="@+id/act_login_et_username"                
android:layout_width="match_parent"                
android:layout_height="wrap_content"                
android:hint="@string/str_username" />            

<EditText    
android:id="@+id/act_login_et_password"                
android:layout_width="match_parent"                
android:layout_height="wrap_content"                
android:hint="@string/str_password" />           

<Button
android:id="@+id/act_login_bt_login"                
android:layout_width="wrap_content"                
android:layout_height="wrap_content"               
 android:text="@string/str_login" />        

</LinearLayout>        


<TextView           
android:layout_width="wrap_content"            
android:layout_height="wrap_content"            
android:layout_alignParentBottom="true"           
android:layout_centerHorizontal="true"            
android:layout_marginBottom="12dp"           
android:text="@string/str_sign_up"           
android:textSize="18dp" />   

</RelativeLayout>
</layout>


Activity_main.java
public class MainActivity extends AppCompatActivity{
   
private ActivityMainBinding mBinding;    

@Override    
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this,                                                                     R.layout.activity_main);
          setonClickListner();    
}    

private void setonClickListner() {
        mBinding.actLoginTvChangeLanguage.setOnClickListener(new View.OnClickListener() {            
           @Override            
            public void onClick(View view) {
               Locale locale = new Locale("hi");
               Locale.setDefault(locale);
                Configuration config = new Configuration();
                config.locale = locale;                                                                                            getBaseContext().getResources().updateConfiguration(config,                               getBaseContext().getResources().getDisplayMetrics());                

                 //Restart the activity to apply changes               
                 Intent refresh = new Intent(MainActivity.this, MainActivity.class);
                 startActivity(refresh);               
                 finish();           
}        
});    
}
}

Demo:-

  

Comments