0

I use a third party Android package, which defines a HeaderBar UI class. This class has a private member which is a popup window.

I want to reset the width of the popup window, but I cannot get a reference of the popup window from the instance of HeaderBar. Is there any way I can get a reference of it from the view hierarchy of Android?

HChen
  • 258
  • 1
  • 3
  • 12

1 Answers1

0

I finally use java reflection to get the reference of the popup window, even though it is private.

class MyHeaderBar extends HeaderBar {
private int defaultWidth = 0;
...
private PopupWindow getPopupWindow() {
  try{
    Field field = HeaderBar.class.getDeclaredField("mPopupWindow");
    field.setAccessible(true);
    Object value = field.get(this);
    field.setAccessible(false);

    if (value != null && PopupWindow.class.isAssignableFrom(value.getClass())) {
        return (PopupWindow) value;
    }
  } catch (NoSuchFieldException e) {
  } catch (IllegalAccessException e) {}

  return null;
}

@Override
public vod showPopupWindow() {
  super.showPopupWindow();
  PopupWindow pw = getPopupWindow();
  if (defaultWidth == 0) 
    defaultWidth = pw.getWidth();
  pw.setFixedWidth((int)(defaultWidth * 1.15));  
}
...
}
HChen
  • 258
  • 1
  • 3
  • 12