Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Fix: getConfiguration().locale field was deprecated in API level 24 + example fix #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

janstol
Copy link

@janstol janstol commented Mar 17, 2019

Configuration.locale

This field was deprecated in API level 24.
Do not set or read this directly. Use getLocales() and setLocales(android.os.LocaleList). If only the primary locale is needed, getLocales().get(0) is now the preferred accessor.

Also updated (fixed) example...

Eimji added a commit to Eimji/speech_recognition that referenced this pull request Mar 25, 2019
@janstol janstol changed the title Fix: getConfiguration().locale field was deprecated in API level 24 Fix: getConfiguration().locale field was deprecated in API level 24 + example fix Mar 26, 2019
Copy link

@vintage vintage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good! Just one comment about reusing the code.

Locale locale = activity.getResources().getConfiguration().locale;
Locale locale;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
locale = activity.getResources().getConfiguration().getLocales().get(0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about moving the part activity.getResources().getConfiguration() outside of conditional statements to avoid code repetition? We would have something like:

Configuration config = activity.getResources().getConfiguration();
Locale locale;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    locale = config.getLocales().get(0);
} else {
    locale = config.locale;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, good point. Done.

@vintage
Copy link

vintage commented May 7, 2019

Just tested the changes on Android 9 (OnePlus 6t) - works as expected. As it's always good to be aligned with the deprecations of the platforms, @rxlabz can you give it a look?

@Piyushbisen
Copy link

package com.example.newgenrationdairy;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.view.View;
import android.widget.Button;
import androidx.fragment.app.FragmentManager;

import java.util.Locale;

public class test extends AppCompatActivity {

public static FragmentManager fragmentManager;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    fragmentManager = getSupportFragmentManager();
    loadLocale();
    Button changeLan = findViewById(R.id.changel);
    Button login = findViewById(R.id.btnlogin);
    changeLan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showChangeLanguageDialog();
        }
    });
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            test.fragmentManager.beginTransaction().replace(R.id.test_layout,new Homepage(),null).addToBackStack(null).commit();
        }
    });

}



private void showChangeLanguageDialog() {
    final String[] listItems = {"English", "हिन्दी"};
    AlertDialog.Builder mBuilder = new AlertDialog.Builder(test.this);
    mBuilder.setTitle("Choose Language..");
    mBuilder.setSingleChoiceItems(listItems, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            if (i == 0){
                setLocale("en");
                recreate();
            }

            else if (i == 1){
                setLocale("hi-rIN");
                recreate();
            }

            dialogInterface.dismiss();

        }
    });

    AlertDialog mDialog = mBuilder.create();
    mDialog.show();
}

private void setLocale(String lang) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
    editor.putString("My_Lang", lang);
    editor.apply();
}

public void loadLocale() {
    SharedPreferences prefs = getSharedPreferences("Settings", Activity.MODE_PRIVATE);
    String language = prefs.getString("My Lang", "");
    setLocale(language);
}

}

i have a problem with config.locale = locale; help me

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants