Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – multi language change in android studio app

Posted on November 14, 2022

Solution 1 :

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:)

Problem :

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);
    }
}
READ  [ANSWERED] java - Android Room - How can I check if an entity with the same name already exists before inserting?
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress