I need to get the device Locale on android studio, but apparently the code below only works on api level 24+; How can i achieve the same result as this line of code in lower level apis?
getResources().getConfiguration().getLocales().get(0);
well according to google docs the command public Locale locale has deprecated in API 24 and you are allowed to use getLocales() and setLocales(android.os.LocaleList) in API levels above 24. so you should yoconfiguration.locale instead in lower APIs.
#Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
// update overrideConfiguration with your locale
setLocale(overrideConfiguration) // you need to implement this
}
super.applyOverrideConfiguration(overrideConfiguration);
}
also check this out, it says it all:
Set Locale programmatically
For lower lavel api, you can use
Locale current = getResources().getConfiguration().locale;
Reference: https://stackoverflow.com/a/14389640/12676247
Related
I am trying to return the locale in my app with this line of code:
Locale current = getResources().getConfiguration().locale;
The minSdkVersion is 15 and compiling with 24. But locale is deprecated, does it affect my app efficiency?
Is there any other 'not deprecated' way to retrieve the locale?
If you are compiling with API 24 or above you should do this. Still it will show deprecated for lower one but you can ignore that.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = getResources().getConfiguration().getLocales().get(0);
} else {
locale = getResources().getConfiguration().locale;
}
I have this code:
public void run() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
// This action will have the side-effect of blurring the currently focused element
inAppWebView.loadUrl("javascript:" + finalScriptToInject);
}
But the id KITKAT gets the error: KITKAT cannot be resolved or is not a field
Build.VERSION_CODES.KITKAT
As in docs : October 2013: Android 4.4, KitKat, another tasty treat.
means KITKAT constant added in API level 19.
So change current project target version to API level 19(Android 4.4)
I'm not new to Android and I'm well used to the version handling and how to condition it, but when I see this it troubles me...
// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Call some material design APIs here
} else {
// Implement this feature without material design
}
On any device pre lollipop this line would crash the app because the Build.VERSION_CODES.LOLLIPOP field does not exists... so why is this in the recommended solution in the documentation?
I'm really wondering what am I missing?
Well in that case use this
// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Call some material design APIs here
} else {
// Implement this feature without material design
}
Build.VERSION_CODES.LOLLIPOP = 21
Well, you must compile your project with the latest SDK version. Your constants are replaced with corresponding integer values during compilation. No matter what version of Android you run the application on - integers are the same
Try this one
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
// Marshmallow+
}else{
//below Marshmallow
}
Note: Build.VERSION_CODES.LOLLIPOP_MR1==22
Build.VERSION_CODES.M==23
Bit a late to answer, but, today I encountered with the same issue on Android Studio 3.4.1
So the workaround is:
Upgrade to the latest Android SDK.
And,
After Marshmallow/Android 6 Build.VERSION_CODES.xxx full names are replaced with initials and some other variations.
So, now for Marshmallow, it will be:
Build.VERSION_CODES.M
And for Nougat:
Build.VERSION_CODES.N
And so on.
Read more about the build version codes here: Android Developer Reference
in my app minSdkVersion="16" and targetSdkVersion="19". And I wish to use MediaMuxer that introduced in API 18. So when I detetect the API on the phone how can I deside if API>=18 use
MediaMuxer and if API<18 use other function. When targeting minSdkVersion="16" the function MediaMuxer is now shown at all in eclipse.
Test the version number like this
if (Build.VERSION.SDK_INT >= 18) {
// do high api
else {
// do low api
}
You have to check the target version of the Android device using below code
if (android.os.Build.VERSION.SDK_INT >= 16) {
System.out.println("Android Version of device is getter or equal to 16 ::Version::"
+ android.os.Build.VERSION.SDK_INT);
}else{
System.out.println("Android Version of device is getter or equal to 16 ::Version::"
+ android.os.Build.VERSION.SDK_INT);
}
For instance, this code:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) {
myCalendarView.setOnDateChangeListener(
new OnDateChangeListener() {
#Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Toast.makeText
(
getApplicationContext(), ""+dayOfMonth, 0
).show();
}
}
);
}
Gives error:
Description Resource Path Location Type Call requires API level 11
(current min is 8):
android.widget.CalendarView#setOnDateChangeListener example.java /example/src/com/example/example line
20 Android Lint Problem
I understand why I get this error compile-time. But is there any way to mark a source Java class to only be used on certain API level-11? Or surround code blocks with a define/similar so the code is late-bound/jitted only on devices above API level-11? What is the best solution to achieve what I want? (Which is to provide an activity with CalendarView on devices capabile of it.)
I'm not sure if this is going to solve your issue,
but what you are using to check version is not working under API 9
(and you are supporting since API 8).
You should use:
if (Build.VERSION.SDK_INT > 9) {
Or as problematic function is API 11, check for "SDK_INT>10"
Then for lint errors on eclipse, do as people comment, disable lint errors or add the #SuppressLint("NewAPi") or the target to that function to 11.
For anyone stumbling upon this much later, conditionally executing code based on the API version at runtime is currently a valid way of supporting different platform versions.
See: https://developer.android.com/training/basics/supporting-devices/platforms#version-codes
Where it says:
Android provides a unique code for each platform version in the Build
constants class. Use these codes within your app to build conditions
that ensure the code that depends on higher API levels is executed
only when those APIs are available on the system.
And gives examples:-
Java:
private void setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Kotlin:
private fun setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
actionBar.setDisplayHomeAsUpEnabled(true)
}
}