Change Locale not work after migrate to Androidx
Finally, I find the problem in my app. When migrating the project to Androidx
dependencies of my project changed like this:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0-alpha04'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'
}
As it is seen, version of androidx.appcompat:appcompat
is 1.1.0-alpha03
when I changed it to the latest stable version, 1.0.2
, my problem is resolved and the change language working properly.
I find the latest stable version of appcompat
library in Maven Repository. I also change other libraries to the latest stable version.
Now my app dependencies section is like bellow:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
Changing Localization programatically during runtime no longer working on pre-Oreo sdk 26 after migrating to androidX
Changing implementation of 'androidx.appcompat:appcompat:1.1.0'
to version 1.0.0
and keeping 'androidx.constraintlayout:constraintlayout:1.1.3'
to version 1.1.3
solves the problem.
Also, if you use implementation 'com.google.android.material:material:1.1.0'
, change it to version 1.0.0
Language change issue after updating to androidx.appcompat:appcompat:1.1.0
Edit:
To continue using version 1.1.0 add this below your attachBaseContext
:
Kotlin solution:
override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
if (overrideConfiguration != null) {
val uiMode = overrideConfiguration.uiMode
overrideConfiguration.setTo(baseContext.resources.configuration)
overrideConfiguration.uiMode = uiMode
}
super.applyOverrideConfiguration(overrideConfiguration)
}
Java solution:
@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (overrideConfiguration != null) {
int uiMode = overrideConfiguration.uiMode;
overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
overrideConfiguration.uiMode = uiMode;
}
super.applyOverrideConfiguration(overrideConfiguration);
}
If you don't need to upgrade to the latest
appCompat
then check the
old answer. Else use the solution provided by @0101100101
here.
Old Answer:
After spending hours trying, got to know that it might be a bug.
Downgrade to the last stable version and it works flawlessly.
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2' //************ DO NOT UPGRADE LANGUAGE ISSUE on API 23 and below *******************//
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
....
}
Meanwhile, I have filed an issue with Google https://issuetracker.google.com/issues/140880275
changes in locale does work when add many modules
Explanation
The problem is related to Appcompat
. There are two different fixes depending on the version of AppCompat
. Since you are using 1.2.0
, you will have to implement that one.
AppCompatDelegateImpl
ends up removing the locale, because essentially a ContextThemeWrapper wraps your ContextWrapper. See the implementation @ AppCompatDelegateImpl.java line 368 . Also lines 388, and 480.
try {
ContextThemeWrapperCompatApi17Impl.applyOverrideConfiguration(
(android.view.ContextThemeWrapper) baseContext, config);
return baseContext;
} catch (IllegalStateException e) {
if (DEBUG) {
Log.d(TAG, "Failed to apply configuration to base context", e);
}
}
The work-around for this is to over-ride getDelegate
inside your Base Activity like this:
private var baseContextWrappingDelegate: AppCompatDelegate? = null
override fun getDelegate() = baseContextWrappingDelegate ?: BaseContextWrappingDelegate(super.getDelegate()).apply {
baseContextWrappingDelegate = this
}
And you also need the following class ( see : Change Locale not work after migrate to Androidx ).
package androidx.appcompat.app
import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import android.util.AttributeSet
import android.view.MenuInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.view.ActionMode
import androidx.appcompat.widget.Toolbar
class BaseContextWrappingDelegate(private val superDelegate: AppCompatDelegate) : AppCompatDelegate() {
override fun getSupportActionBar() = superDelegate.supportActionBar
override fun setSupportActionBar(toolbar: Toolbar?) = superDelegate.setSupportActionBar(toolbar)
override fun getMenuInflater(): MenuInflater? = superDelegate.menuInflater
override fun onCreate(savedInstanceState: Bundle?) {
superDelegate.onCreate(savedInstanceState)
removeActivityDelegate(superDelegate)
addActiveDelegate(this)
}
override fun onPostCreate(savedInstanceState: Bundle?) = superDelegate.onPostCreate(savedInstanceState)
override fun onConfigurationChanged(newConfig: Configuration?) = superDelegate.onConfigurationChanged(newConfig)
override fun onStart() = superDelegate.onStart()
override fun onStop() = superDelegate.onStop()
override fun onPostResume() = superDelegate.onPostResume()
override fun setTheme(themeResId: Int) = superDelegate.setTheme(themeResId)
override fun <T : View?> findViewById(id: Int) = superDelegate.findViewById<T>(id)
override fun setContentView(v: View?) = superDelegate.setContentView(v)
override fun setContentView(resId: Int) = superDelegate.setContentView(resId)
override fun setContentView(v: View?, lp: ViewGroup.LayoutParams?) = superDelegate.setContentView(v, lp)
override fun addContentView(v: View?, lp: ViewGroup.LayoutParams?) = superDelegate.addContentView(v, lp)
override fun attachBaseContext2(context: Context) = wrap(superDelegate.attachBaseContext2(super.attachBaseContext2(context)))
override fun setTitle(title: CharSequence?) = superDelegate.setTitle(title)
override fun invalidateOptionsMenu() = superDelegate.invalidateOptionsMenu()
override fun onDestroy() {
superDelegate.onDestroy()
removeActivityDelegate(this)
}
override fun getDrawerToggleDelegate() = superDelegate.drawerToggleDelegate
override fun requestWindowFeature(featureId: Int) = superDelegate.requestWindowFeature(featureId)
override fun hasWindowFeature(featureId: Int) = superDelegate.hasWindowFeature(featureId)
override fun startSupportActionMode(callback: ActionMode.Callback) = superDelegate.startSupportActionMode(callback)
override fun installViewFactory() = superDelegate.installViewFactory()
override fun createView(parent: View?, name: String?, context: Context, attrs: AttributeSet): View? = superDelegate.createView(parent, name, context, attrs)
override fun setHandleNativeActionModesEnabled(enabled: Boolean) {
superDelegate.isHandleNativeActionModesEnabled = enabled
}
override fun isHandleNativeActionModesEnabled() = superDelegate.isHandleNativeActionModesEnabled
override fun onSaveInstanceState(outState: Bundle?) = superDelegate.onSaveInstanceState(outState)
override fun applyDayNight() = superDelegate.applyDayNight()
override fun setLocalNightMode(mode: Int) {
superDelegate.localNightMode = mode
}
override fun getLocalNightMode() = superDelegate.localNightMode
private fun wrap(context: Context): Context {
//Put wrapping implementation here
}
}
References
https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-master-dev/appcompat/appcompat/src/main/java/androidx/appcompat/app/AppCompatDelegateImpl.java#368
Change Locale not work after migrate to Androidx
https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-master-dev/appcompat/appcompat/src/main/java/androidx/appcompat/app/AppCompatDelegateImpl.java#368
setLocale for change language has no effect in Android
This answer worked for me :
bundle {
language {
// Specifies that the app bundle should not support
// configuration APKs for language resources. These
// resources are instead packaged with each base and
// dynamic feature APK.
enableSplit = false
}
}
I have got the answer from this
popup dialog not working after Migrate to Androidx
in the XML layout, i just changed the hieght and width to custom size, its just working fine
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="@dimen/_260sdp"
android:layout_height="@dimen/_220sdp"
Related Topics
Android Sdk Manager Won't Open
How to Provide Shadow to Button
Don't Reload Application When Orientation Changes
Changing Screen Brightness Programmatically (As with the Power Widget)
Android Activity Over Default Lock Screen
How to Deploy and Execute an Application on a Device Connected to a Remote System
Image, Saved to Sdcard, Doesn't Appear in Android's Gallery App
Capturing Mobile Phone Traffic on Wireshark
Listadapter Not Updating Item in Recyclerview
Overlay Two Images in Android to Set an Imageview
Capture Screen Shot of Googlemap Android API V2
How to Hide Action Bar Before Activity Is Created, and Then Show It Again
How to Configure a Static Ip Address, Netmask, Gateway Programmatically on Android 3.X or 4.X
Calculate Compass Bearing/Heading to Location in Android
How to Change Shape Color Dynamically
Android: Out of Memory Exception in Gallery