Sirwan Rahimi
Sirwan Rahimi

Reputation: 121

multi language change in android studio app

i am working on a project that i need to have some settings that users can choose there language

when I use the old version of material design librarys , it work well but if i want to change the dependencys to androidX (android V30) it not work

it's mean that if i use

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;

it work but if i use

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

it not work

not work means (the app will be launch but not work well)

my main Activity is

package com.faranesh.saman.multilanguage2020;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {


    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LoadLocale();
        setContentView(R.layout.activity_main);

        ActionBar actionBar=getSupportActionBar();
        actionBar.setTitle(getResources().getString(R.string.app_name));


        btn=(Button)findViewById(R.id.btn_chang_language);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChangeLanguageState();
            }
        });

    }


    public void ChangeLanguageState(){


        final String[] listItem=new String[]{"English","فارسی"};

        AlertDialog.Builder dialog=new AlertDialog.Builder(this);

        dialog.setTitle("Chosse one Language....");
        dialog.setSingleChoiceItems(listItem, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {


                if (which==0){

                    setLocale("en");
                    recreate();
                }else if (which==1){
                    setLocale("fa");
                    recreate();
                }

                dialog.dismiss();
            }
        });

        AlertDialog builder=dialog.create();
        builder.show();

    }

    public  void setLocale(String lang){

        Locale locale=new Locale(lang);
        Locale.setDefault(locale);
        Configuration configuration=new Configuration();
        configuration.locale=locale;
        getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
        SharedPreferences.Editor editor=getSharedPreferences("settings",MODE_PRIVATE).edit();
        editor.putString("MY_LANG",lang);
        editor.apply();


    }

    public void LoadLocale(){

        SharedPreferences sharepref=getSharedPreferences("settings",Activity.MODE_PRIVATE);
                String language=sharepref.getString("MY_LANG","");
        setLocale(language);
    }
}

Upvotes: 0

Views: 259

Answers (1)

mahdi sadeghi
mahdi sadeghi

Reputation: 68

You should call setLocal and LoadLocal function together before setContentView. like below code:

private void changeLanguage(String language) {
    sharedPreferences.edit().putString(Constant.LANGUAGE_KEY, language).apply();
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Configuration configuration = new 
          Configuration(getResources().getConfiguration());
    configuration.setLayoutDirection(locale);

    configuration.locale = locale;
    configuration.setLocale(locale);

    getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
    getResources().getConfiguration().setLocale(locale);
}

and pass getResources().getConfiguration() to configuration

i hope work for you:)

Upvotes: 1

Related Questions