Comparing resources in API 21 x API 23 - android

My app is running well in API 23 and above, but there seems to be a bug in it when I run it in API 21. I've narrowed it down to this piece of code:
if (myButton.getBackground().getConstantState() == ResourcesCompat.getDrawable(getResources(), R.drawable.myDrawable, null).getConstantState())
{...}
In API 21 it always returns false (even when it shouldn't), so I guess one of those methods doesn't work in API 21.
Any ideas? Is there any other way (compatible with API 21 and above) to check if a button's background has a specific drawable assigned to it?
ps: my minsdk is 21, but Lint is not giving me any warnings.

You can try using ContextCompat:
ContextCompat.getDrawable(yourContext, R.drawable.myDrawable).getConstantState();

Related

Show PopupWindow above all window for target android 26

I want to show PopupWindow above all window (example: another popupwindow, dialog, another activity screen) without request SYSTEM_ALERT_WINDOW so I use WindowManager.LayoutParams.TYPE_TOAST
public void showSimplePopupWindow() {
final View popupView = layoutInflater.inflate(R.layout.popup_layout_2, null);
final PopupWindow popupWindow = new PopupWindow(popupView);
...config popup window...
PopupWindowCompat.setWindowLayoutType(popupWindow, WindowManager.LayoutParams.TYPE_TOAST);
popupWindow.showAsDropDown(findViewById(R.id.button_show_popup_window));
}
It working well in all android version if I set targetSdkVersion < 26 .
Currently, If I keep the code above and update the target targetSdkVersion to 26 then it will crash with device api 25-26 with exception android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W#859d91f is not valid; is your activity running?
I see that TYPE_TOAST is deprecated in sdk 26 and they suggest to use TYPE_APPLICATION_OVERLAY. However, when I use TYPE_APPLICATION_OVERLAY, AndroidStudio show TYPE_APPLICATION_OVERLAY required api 26. Therefore, TYPE_APPLICATION_OVERLAY only work well with device api 26, for device api < 26, it will crash ( even I have enabled Display/Draw over other app permission)
Is there any alternative way to make TYPE_TOAST work with target api 26? Any help or suggestion would be great appreciated.
There appears to be a bug with API 25. The bug is that if you change the target API to be 26, API 25 devices will no longer be able to use the TYPE_TOAST parameter, even though it is allowable in API 25.
The only way to fix this behavior is to have the user enable overlay permissions in Settings for your app. API 25 and 26 overlays will then function as expected.
Since:
TYPE_APPLICATION_OVERLAY (introduced in Api level 26) is a constant value 2038 (0x000007f6)
TYPE_TOAST (since Api level 1!) is a constant value 2005 (0x000007d5)
one option would be to check the System Version at Runtime and, depending on its value, using the right constant.

Android - setLayoutDirection in API 15 and API 16

Does anyone know if ViewCompat.setLayoutDirection works in API level below 17?
I search for a solution all over the internet but I can't find a solid one.
setLayoutDirection was added to in API Level 17 (read here) which is 4.2 so the older versions do not support that. So keep two layouts for higher and below and you need to work around for the below api level XML view.
If you're using the support library, you can do the following:
if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL) {
// view -RTL layout
} else {
//view - set made layout for below apis
}
BidiFormatter might help you depending on your requirement.Check that as well.
Every one knows about android:layout_gravity="end" right? can be helpfull in this case

How to use txtV1.setBackground(getResources().getDrawable(R.drawable.bcgrtxt));`in Older Devices?

How to use
txtV1.setBackground(getResources().getDrawable(R.drawable.bcgrtxt));
in Older Devices ?
View.setBackground replaced View.setBackgroundDrawable as of API level 16.
.setBackground was added in API Level 16
and .setBackgroundResource was added in API Level 1
so you can use .setBackgroundResource instead of .setBackground

WebView findall doesen't work with android api 15

I'm using the following code to find and highlight text in a webView:
int API = android.os.Build.VERSION.SDK_INT;
if(API < 16) {
wv.findAll(findBox.getText().toString());
} else {
wv.findAllAsync(findBox.getText().toString());
}
However, it fails on api 15 only. It finds the word but doesen't highlight it. Do you have a solution?
This is a known problem without a solution that I know of. This occurs on the following:
Platform Version API Level VERSION_CODE
Android 4.0.3, 4.0.4 15 ICE_CREAM_SANDWICH_MR1
This problem is discussed: Here and Here.

API Level error

In an Activity I have a code that shows this error, but only if you press save.
Call requires API level 11 (current min is 8): android.widget.SearchView#setSearchableInfo
If I change the android:minSdkVersion to 7, it works, but when I save the code again, the same error is thrown. The minSdk must then be changed back to 8,...
What is wrong?
SearchView is available since API lvl 11.
Since your minimum sdk is 8 (lower than 11), Lint will give an error when using SearchView.
You can remove that error by using #TargetApi annotation before your method or class.
But you have to make sure you use a conditional statement before using SearchView to check if it is available, and provide an alternative for earlier versions.
Here's what your code should look like:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
void yourMethod(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
// use SearchView
} else {
// use some other backward compatible custom view
}
}
The SearchView exists in Android from the version 11 ant more.
So, if you would like to use the SearchView in your code, you have to put the minSdkVersion in your manifest to be 11. In the case you put a number smaller than 11, you will get an error, which is normal beause you are giving access to your app to some android versions which will not support your app.
This can be seen (thanks to #JesseJ) here: http://developer.android.com/reference/android/widget/SearchView.html
Added in API level 11

Categories

Resources