Show PopupWindow above all window for target android 26 - android

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.

Related

Comparing resources in API 21 x API 23

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();

If I set minSdkVersion as 8, but want to specify some methods for SdkVersion=15, how can I do that?

I want to develop an Android application even for android devices with a lower version, while I do not want to sacrifice some available functions in higher versions.
In the AndroidManifest.xml file:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
I get the android level of the current device:
deviceLevel = android.os.Build.VERSION.SDK_INT;
Then I will use if statements to specify different code for different versions.
if (deviceLevel >= 11)
{
List<Camera.Size> videoSizes = parameters.getSupportedVideoSizes();
......
}
else
{
......
}
However, Eclipse indicates there exists errors. getSupportedVideoSizes is not availabe for version = 8;
I think I need to suppress the error at this situation.
How to do that?
You will want to add #SupressLint("NewApi") to your activity class. Then you will want to use android.os.Build.VERSION.SDK_INT; to get the version int, which should always be the API number (ie. Jelly Bean 4.2 is API 17, use 17 for your comparison) or you can use Build.VERSION_CODES.DESSERT_NAME_HERE, and use this value for an if-else to perform your action that can only take place in higher level API's.
I think it will be better if you change "minSdkVersion" to 15. Because it will easily die when run at pervious device.

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

TextView.getAlpha doesnt exist in Android

I want to get current alpha of textview i am using the following code but i am getting an error that
java.lang.NoSuchMethodError: android.widget.TextView.getAlpha
Please guide me.
This method since API Level 11. Check your API version.
View.getAlpha() only exists since API level 11. You are trying to running your code on a too-old version of Android.
If you absolutely require this functionality, then update your app's minSdkVersion in AndroidManifest.xml to prevent it running on older Android versions. If you can live without it, do a runtime check to see if the API level is high enough.
you can use Alpha method like below
Textview tv_password;
tv_password =(TextView) findviewById(R.id.tv1);
tv_password.getBackground().setAlpha(50);
you can't use getAlpha() method with Textview
View.getAlpha() available from API level 11.But if you want to use this on older api then you may use NineOldAndroids library available for using the Honeycomb (Android 3.0) animation API on all versions of the platform back to 1.0!
So use need to change for e.g.
mAlpha = mView.getAlpha();
to
mAlpha = ViewHelper.getAlpha(mView);
where mView is your view.
Note : Don't forget to import com.nineoldandroids.view.ViewHelper;

Hide time in Android notification without using custom layout

In the bottom right corner of a standard Android notification is time (eg 12:00). Can I hide it without using custom notification layout?
For API level 17 and onward, the above accepted answer by Erwan is correct.
For historical purposes, the answer below remains
It is possible to do that.
Try setting when to 0.
For the few notifications that don't do that, it looks like thats what they do.
Here's the general API reference.
Here's a link to the ADB notification and search for private void updateAdbNotification()...
From API Level 17 onwards, you can use setShowWhen(false)
public Notification.Builder setShowWhen (boolean show)
Added in API level 17 Control whether the timestamp set with setWhen
is shown in the content view.
http://developer.android.com/reference/android/app/Notification.Builder.html#setShowWhen(boolean)
For API level 17 and onward,
mBuilder.setShowWhen(boolen);
1.Use Hide Notification Time
mBuilder.setShowWhen(false);
2.Use Show Notification Time
mBuilder.setShowWhen(true);

Categories

Resources