Created
June 1, 2020 09:23
-
-
Save yoesuv/3ad86d4ce2615f6a83fc2d3a68ba21cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.os.Build | |
import android.os.LocaleList | |
import java.util.* | |
class ContextWrapper(base: Context) : android.content.ContextWrapper(base) { | |
companion object { | |
fun wrap(context: Context, newLocale: Locale): ContextWrapper { | |
var mContext = context | |
val res = mContext.resources | |
val configuration = res.configuration | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
configuration.setLocale(newLocale) | |
val localeList = LocaleList(newLocale) | |
LocaleList.setDefault(localeList) | |
mContext = mContext.createConfigurationContext(configuration) | |
} else { | |
configuration.locale = newLocale | |
res.updateConfiguration(configuration, res.displayMetrics) | |
} | |
return ContextWrapper(mContext) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment