'Why the app doesn't change to the language I already saved after close
I have some issues with changing the language in the app. I do not know why it is not working, there is no error, checked the log all clear.
I used the following code in the splash screen. It worked well in the beginning, but after I added admob ads, it no longer works,. Only the first page is translated and then it's back to the language of the system after I reload the page.
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_LENGTH = 2500;
ImageView imagesplash;
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagesplash = findViewById(R.id.logosplash);
Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.animsplashscreen);
imagesplash.startAnimation(hyperspaceJump);
//lang
prefs= MainActivity.this.getSharedPreferences("Settings", Activity.MODE_PRIVATE);
String Language = prefs.getString("lang", Locale.getDefault().getLanguage().trim());
if (Language.equals(""))
{
LoadLanguage("en");
Intent o = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
o.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(o);
}
//splash
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
Intent intt = new Intent(MainActivity.this, Startpage.class);
ActivityOptions options =
ActivityOptions.makeCustomAnimation(MainActivity.this, R.anim.slid_in_left, R.anim.slid_out_right);
startActivity(intt, options.toBundle());
finish();
}
},SPLASH_DISPLAY_LENGTH);
}
public void onBackPressed() {
}
public void LoadLanguage(String lang){
Locale locale2 = new Locale(lang);
Locale.setDefault(locale2);
Configuration config2 = new Configuration();
config2.locale = locale2;
getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics());
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|