PopupWindow z ordering - android

I play with menus using PopupWindow, which overlap EditText.
It works fine, except that my PopupWindow is overlapped by some items from EditText IME system (selection marks, Paste button).
My question is: how do I force z-ordering of my PopupWindow so that it appears above those decorations?
Here's image of what's happening. I need my PopupWindow (the menu) be drawn on top of everything, thus somehow tell WindowManager how to order windows.
Thanks.

Found anwer myself.
Those decorations are normal PopupWindow-s, managed by EditText.
Z-ordering of any Window is defined by WindowManager.LayoutParams.type, actually it defines purpose of Window. Valid ranges are FIRST_SUB_WINDOW - LAST_SUB_WINDOW for a popup window.
App typically can't change "type" of PopupWindow, except of calling hidden function PopupWindow.setWindowLayoutType(int) using Java reflection, and setting desired window type.
Result:
EDIT:
Code that does that:
Method[] methods = PopupWindow.class.getMethods();
for(Method m: methods){
if(m.getName().equals("setWindowLayoutType")) {
try{
m.invoke(getPopupWindow(), WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
}catch(Exception e){
e.printStackTrace();
}
break;
}
}

import android.support.v4.widget.PopupWindowCompat;
PopupWindowCompat.setWindowLayoutType(popupWindow, WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);

public void compatibleSetWindowLayoutType(int layoutType) {
if (Build.VERSION.SDK_INT >= 23) {
setWindowLayoutType(layoutType);
} else {
try {
Class c = this.getClass();
Method m = c.getMethod("setWindowLayoutType", Integer.TYPE);
if(m != null) {
m.invoke(this, layoutType);
}
} catch (Exception e) {
}
}
}

Make are complement.
The PopupWindowCompat.setWindowLayoutType API must be called before show popWindow.

Related

How to clear animation of adding/removing actionbar?

At my application I am using container where I change several fragments. At some fragments I have to hide actionbar at parent activity. I managed to do it, but the process of hiding and viewing toolbar is supported with weird and uncomfortable animation. I hide and show toolbar via:
Objects.requireNonNull(getSupportActionBar()).hide();
Maybe I can clear animation?
I managed to find a solution for my problem with this method:
public static void disableShowHideAnimation(ActionBar actionBar) {
try
{
actionBar.getClass().getDeclaredMethod("setShowHideAnimationEnabled", boolean.class).invoke(actionBar, false);
}
catch (Exception exception)
{
try {
Field mActionBarField = actionBar.getClass().getSuperclass().getDeclaredField("mActionBar");
mActionBarField.setAccessible(true);
Object icsActionBar = mActionBarField.get(actionBar);
Field mShowHideAnimationEnabledField = icsActionBar.getClass().getDeclaredField("mShowHideAnimationEnabled");
mShowHideAnimationEnabledField.setAccessible(true);
mShowHideAnimationEnabledField.set(icsActionBar,false);
Field mCurrentShowAnimField = icsActionBar.getClass().getDeclaredField("mCurrentShowAnim");
mCurrentShowAnimField.setAccessible(true);
mCurrentShowAnimField.set(icsActionBar,null);
}catch (Exception e){
//....
}
}
}
link.

Android SDK Crash at android.widget.PopupWindow$1.onScrollChanged(PopupWindow.java:132)

Can someone with google dev team explain how to avoid this crash on pre-ics devices? In my case an ImageButton on the ListView item is the anchor of the PopupWindow to create a dropdown. I have tried everything popup.dismiss() , popup= null, etc but nothing seems to prevent this being an issue when the adapter is reset.
I am getting the following exception:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.PopupWindow$1.onScrollChanged(PopupWindow.java:132)
05-21 17:02:27.736: E/AndroidRuntime(25836): at
android.view.ViewTreeObserver.dispatchOnScrollChanged(ViewTreeObserver.java:607)
This Popup is on a list item. Once the last list item is removed from the ListView I resetAdapter to set a footer. Then when I move away from the screen this error occurs.
Not in Ice Cream Sandwich: Icecream Sandwich. See https://android.googlesource.com/platform/frameworks/base/+/749b0eb2c9a52bb188fd8900859b3725889e0ec0%5E!/
This suggests a fix related to null anchor of PopupWindow. What can be done?
In my case a button in ListView item is the anchor of the popup window.
and same issue here:
https://github.com/JakeWharton/ActionBarSherlock/issues/487
Not sure if OP still needs this since it is half year later that I just saw this..
This is fixed in ICS. However, you may assign a fixed scroll listener via reflection!
if(Build.VERSION.SDK_INT<Build.VERSION_CODES.ICE_CREAM_SANDWICH){
try {
final Field fAnchor = PopupWindow.class.getDeclaredField("mAnchor");
fAnchor.setAccessible(true);
Field listener = PopupWindow.class.getDeclaredField("mOnScrollChangedListener");
listener.setAccessible(true);
final ViewTreeObserver.OnScrollChangedListener originalListener = (ViewTreeObserver.OnScrollChangedListener) listener.get(window);
ViewTreeObserver.OnScrollChangedListener newListener=
new ViewTreeObserver.OnScrollChangedListener() {
public void onScrollChanged() {
try {
// PopupWindow implementation has WeakReference<View>
WeakReference<View> mAnchor = (WeakReference<View>) fAnchor.get(window);
if (mAnchor == null || mAnchor.get() == null) {
return;
} else {
originalListener.onScrollChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
listener.set(window,newListener);
} catch (Exception e) {
e.printStackTrace();
}
}

How to check if the current activity has a dialog in front?

I am using a third-party library and sometimes it pops up a dialog. Before I finish the current activity, I want to check whether there is a dialog popped up in the current context.
Is there any API for this?
You can check it running over the active fragments of that activity and checking if one of them is DialogFragment, meaning that there's a active dialog on the screen:
public static boolean hasOpenedDialogs(FragmentActivity activity) {
List<Fragment> fragments = activity.getSupportFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
if (fragment instanceof DialogFragment) {
return true;
}
}
}
return false;
}
I faced a similar problem, and did not want to modify all locations where dialogs were being created and shown. My solution was to look at whether the view I was showing had window focus via the hasWindowFocus() method. This will not work in all situations, but worked in my particular case (this was for an internal recording app used under fairly restricted circumstances).
This solution was not thoroughly tested for robustness but I figured I would post in in case it helped somebody.
This uses reflection and hidden APIs to get the currently active view roots. If an alert dialog shows this will return an additional view root. But careful as even a toast popup will return an additional view root.
I've confirmed compatibility from Android 4.1 to Android 6.0 but of course this may not work in earlier or later Android versions.
I've not checked the behavior for multi-window modes.
#SuppressWarnings("unchecked")
public static List<ViewParent> getViewRoots() {
List<ViewParent> viewRoots = new ArrayList<>();
try {
Object windowManager;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
windowManager = Class.forName("android.view.WindowManagerGlobal")
.getMethod("getInstance").invoke(null);
} else {
Field f = Class.forName("android.view.WindowManagerImpl")
.getDeclaredField("sWindowManager");
f.setAccessible(true);
windowManager = f.get(null);
}
Field rootsField = windowManager.getClass().getDeclaredField("mRoots");
rootsField.setAccessible(true);
Field stoppedField = Class.forName("android.view.ViewRootImpl")
.getDeclaredField("mStopped");
stoppedField.setAccessible(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
List<ViewParent> viewParents = (List<ViewParent>) rootsField.get(windowManager);
// Filter out inactive view roots
for (ViewParent viewParent : viewParents) {
boolean stopped = (boolean) stoppedField.get(viewParent);
if (!stopped) {
viewRoots.add(viewParent);
}
}
} else {
ViewParent[] viewParents = (ViewParent[]) rootsField.get(windowManager);
// Filter out inactive view roots
for (ViewParent viewParent : viewParents) {
boolean stopped = (boolean) stoppedField.get(viewParent);
if (!stopped) {
viewRoots.add(viewParent);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return viewRoots;
}
AFAIK - there is no public API for this.
Recommended way is to have a reference to the dialog, and check for isShowing() and call dismiss() if necessary, but since you're using a third party library, this may not be an options for you.
Your best bet is to check the documentation for the library you use. If that doesn't help, you're out of luck.
Hint: Activity switches to 'paused' state if a dialog pops up. You may be able to 'abuse' this behavior ;)
You can override activity method onWindowFocusChanged(boolean hasFocus) and track the state of your activity.
Normally, if some alert dialog is shown above your activity, the activity does not get onPause() and onResume() events. But it loses focus on alert dialog shown and gains it when it dismisses.
For anyone reading this and wondering how to detect a Dialog above fragment or activity, my problem was that inside my base fragment I wanted to detect if I'm displaying a Dialog on top of my fragment. The dialog itself was displayed from my activity and I didn't want to reach it there, so the solution I came up with (Thanks to all answers related to this kind of question) was to get the view (or you can get the view.rootView) of my fragment and check whether any of its children have the focus or not. If none of its children have no focus it means that there is something (hopefully a Dialog) being displayed above my fragment.
// Code inside my base fragment:
val dialogIsDisplayed = (view as ViewGroup).children.any { it.hasWindowFocus() }
Solution in kotlin
Inside Fragment
val hasWindowFocus = activity?.hasWindowFocus()
In Activity
val hasWindowFocus = hasWindowFocus()
If true, there is no Dialog in the foreground
if FALSE , there is a view/dialog in the foreground and has focus.
I am assuming, you are dealing with third party library and you don't have access to dialog object.
You can get the root view from the activity,
Then you can use tree traversal algorithm to see if you can reach any of the child view. You should not reach any of your child view if alert box is displayed.
When alert view is displayed ( check with Ui Automator ), the only element present in UI tree are from DialogBox / DialogActivity. You can use this trick to see if dialog is displayed on the screen. Though it sounds expensive, it could be optimized.
If you are using Kotlin just:
supportFragmentManager.fragments.any { it is DialogFragment }

Change HorizontalScrollView's light color - OverScrollMode

At the end of the HorizontalScrollView, a light appears to show that the scroll content has ended. Is there a way to change this color? It is appearing in my phone as a yellow one. I have already set the HorizontalScrollView's background color to the one that I desire, but this "end-of-scroll" light isn't the one I want.
EDIT:
I just noticed that this light appears due to the onOverScrollMode (since API level 9 - see this link). Is there a way to set OVER_SCROLL_NEVER and also keep the compatibility with the Eclair versions? Or even better: to set the color of this light (if it appears)?
Unfortunately there is no simple way to set the color for the OverScroll EdgeEffect.
To safely set OVER_SCROLL_NEVER and remain compatible with early SDK revs, you can introspect for the setOverScrollMode method and if found call it. (tested on 3.1 and 2.2)
setContentView(R.layout.main);
// find scroll view
HorizontalScrollView hscroll = (HorizontalScrollView)findViewById(R.id.hscroll);
try {
// look for setOverScrollMode method
Method setOverScroll = hscroll.getClass().getMethod("setOverScrollMode", new Class[] { Integer.TYPE } );
if (setOverScroll != null) {
try {
// if found call it (OVER_SCROLL_NEVER == 2)
setOverScroll.invoke(hscroll, 2);
} catch (InvocationTargetException ite) {
} catch (IllegalAccessException ie) {
}
}
} catch (NoSuchMethodException nsme) {
}
Although this question has already been answered, I wanted to add a couple of more ways that you can go about setting the overScrollMode attribute.
1) Create a "layout-v10" folder and put your alternate xml layout with the overScrollMode attribute set as desired.
2) If #1 would mean copying a ton of files and duplication, you can alternatively create a style for the HorizontalScrollView.
You can set the EdgeEffect color using reflection. The following will work from API 14+:
public static void setEdgeGlowColor(final HorizontalScrollView hsv, final int color) {
try {
final Class<?> clazz = HorizontalScrollView.class;
for (final String name : new String[] {
"mEdgeGlowLeft", "mEdgeGlowRight"
}) {
final Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
setEdgeEffectColor((EdgeEffect) field.get(hsv), color);
}
} catch (final Exception ignored) {
}
}
public static void setEdgeEffectColor(final EdgeEffect edgeEffect, final int color) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
edgeEffect.setColor(color);
return;
}
final Field edgeField = EdgeEffect.class.getDeclaredField("mEdge");
edgeField.setAccessible(true);
final Field glowField = EdgeEffect.class.getDeclaredField("mGlow");
glowField.setAccessible(true);
final Drawable mEdge = (Drawable) edgeField.get(edgeEffect);
final Drawable mGlow = (Drawable) glowField.get(edgeEffect);
mEdge.setColorFilter(color, PorterDuff.Mode.SRC_IN);
mGlow.setColorFilter(color, PorterDuff.Mode.SRC_IN);
mEdge.setCallback(null); // free up any references
mGlow.setCallback(null); // free up any references
} catch (final Exception ignored) {
}
}

ListView top highlight on scrolling

The border displays a default color (that's orange on my Nexus S) while scrolling a ListView to the limit. How to change that color?
I really don't know how to explain it. Just look at this picture:
So, how to change the highlight color when the ListView scrolling to the border? using themes or styles
The solution is to use setOverscrollFooter(null) and setOverscrollHeader(null).
The documentation is here !
You can also set it directly in the XML :
<ListView android:overScrollMode="never" />
Or specify the footer and the header :
<ListView
android:overscrollHeader="#null"
android:overscrollFooter="#null" />
N.B. : There is also a property fadingEdge that may interest you.
"Overscroll" methodes are supported starting API level 9
Finally I found the solution.
setOverscrollFooter(null) and setOverscrollHeader(null) does not work. At least on 2.3.*. Setting attributes from *.xml doesn't help too.
setOverScrollMode(View.OVER_SCROLL_NEVER) causes glitchy scrolling. At least on 2.3.*.
The only solution that really works involves the use of Java Reflection.
It works even with ugly custom Samsung listviews with bounce overscroll effect.
Here is a snippet:
#Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
//onOverScrolled method must be overrided, or we will see the background of the listview when overscroll fast.
}
private void removeOverscrollEffect() {
try {
Class<?> superClass = getClass().getSuperclass().getSuperclass();
Field field = superClass.getDeclaredField("mEdgeGlowTop");
field.setAccessible(true);
Object edgeGlowTop = field.get(this);
if (edgeGlowTop != null) {
Class<? extends Object> edgeClass = edgeGlowTop.getClass();
Field edgeDrawable = edgeClass.getDeclaredField("mEdge");
edgeDrawable.setAccessible(true);
edgeDrawable.set(edgeGlowTop, new ColorDrawable(Color.TRANSPARENT));
Field glowDrawable = edgeClass.getDeclaredField("mGlow");
glowDrawable.setAccessible(true);
glowDrawable.set(edgeGlowTop, new ColorDrawable(Color.TRANSPARENT));
field.set(this, edgeGlowTop);
}
Field fieldBottom = superClass.getDeclaredField("mEdgeGlowBottom");
fieldBottom.setAccessible(true);
Object edgeGlowBottom = fieldBottom.get(this);
if (edgeGlowBottom != null) {
Class<? extends Object> edgeClassBottom = edgeGlowBottom.getClass();
Field edgeDrawableBottom = edgeClassBottom.getDeclaredField("mEdge");
edgeDrawableBottom.setAccessible(true);
edgeDrawableBottom.set(edgeGlowBottom, new ColorDrawable(Color.TRANSPARENT));
Field glowDrawableBottom = edgeClassBottom.getDeclaredField("mGlow");
glowDrawableBottom.setAccessible(true);
glowDrawableBottom.set(edgeGlowBottom, new ColorDrawable(Color.TRANSPARENT));
fieldBottom.set(this, edgeGlowBottom);
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
I hope this helps.
Here is a nice article on ListView Backgrounds Optimization.
To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. You can do this from code (see setCacheColorHint(int)) or preferably from XML, by using the android:cacheColorHint attribute. To disable the optimization, simply use the transparent color #00000000. The following screenshot shows a list with android:cacheColorHint="#00000000"
Use it in XML file--
<ListView ---
android:fadingEdge="none"
---</ListView>
EDITED:
Using fading edges may introduce noticeable performance degradations and should be used only when required by the application's visual design. To request fading edges with API level 14 and above, use the android:requiresFadingEdge attribute instead.
Check this API link
I used kord's answer until it stopped working in Lollipop, so I changed into this:
try {
Class<?> superClass = getClass().getSuperclass().getSuperclass();
Field field = superClass.getDeclaredField("mEdgeGlowTop");
field.setAccessible(true);
field.set(this, new NoEdgeEffect(getContext()));
Field fieldBottom = superClass.getDeclaredField("mEdgeGlowBottom");
fieldBottom.setAccessible(true);
fieldBottom.set(this, new NoEdgeEffect(getContext()));
} catch (Exception e) {
}
class NoEdgeEffect extends EdgeEffect
{
public NoEdgeEffect(Context context) {
super(context);
}
public boolean draw(Canvas canvas) {
// Do nothing
return false;
}
}
you can use android:listSelector="#002234".
In above value can be any color code that you can find on internet easily.

Categories

Resources