2

My PopupWindow is working perfectly fine on my API 28,29 Emulators, but not sure why it's not showing any view on my API 19 real device. The window is definitely created though because when I click on the button that inflates the view, it is focused.

My custom popup window:

    public class MessagesMoreMenu extends PopupWindow {

    private static final String TAG = "MessagesMoreMenu";

    private Context mContext;
    public MessagesMoreMenu(Context context) {
        super(context);
        this.mContext = context;
        setupView();
    }

    private void setupView(){
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.popupmenu_messagesmoremenu, null);
        ButterKnife.bind(this, view);

        setContentView(view);
    }
}

Usage of it:

    private void inflateMoreMenu(View view){
         PopupWindow popupWindow = new MessagesMoreMenu(mContext);
         popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
         popupWindow.setOutsideTouchable(true);
         popupWindow.setFocusable(true);
         popupWindow.showAsDropDown(view);
    }

XML layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/popupmenu_messagesmore_report"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Report"
        android:padding="16dp"
        app:drawableLeftCompat="@drawable/icon_messages_report"
        android:textColor="@color/colorBlackFont"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:drawablePadding="16dp"/>


    <TextView
        android:id="@+id/popupmenu_messagesmore_block"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="16dp"
        android:padding="16dp"
        android:text="Block"
        app:drawableLeftCompat="@drawable/icon_alertdialog_block"
        android:textColor="@color/colorBlackFont"
        android:layout_below="@+id/popupmenu_messagesmore_report"
        android:gravity="center_vertical"/>

</RelativeLayout>
DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
  • Do you get any error. Try on a real device with API 28,29 or an emulator with API 19. Also, what is mContext here? Is it getApplicationContext() or ClassName.this? – Basu Jan 13 '20 at 08:02
  • classname.this and no errors – DIRTY DAVE Jan 13 '20 at 08:03
  • Please try on a real device with API 28,29 or an emulator with API 19. I will try to re-run the code on my end. – Basu Jan 13 '20 at 08:09
  • @davids. please show your layout xml – Lenoarod Jan 13 '20 at 08:18
  • @davids. give the RelativeLayout an exact `dp` or `match_parent` to try – Lenoarod Jan 13 '20 at 08:39
  • try this solution: [https://stackoverflow.com/questions/39673099/android-nougat-popupwindow-showasdropdown-gravity-not-working](https://stackoverflow.com/questions/39673099/android-nougat-popupwindow-showasdropdown-gravity-not-working) – Rabiun Islam Jan 13 '20 at 08:56

2 Answers2

1

Very weird but adding this fixed my problem...

popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

https://stackoverflow.com/a/51177435/11110509

This is not needed on my other non API 19 devices

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
0

please try

popupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);

here mRelativeLayout is a view reference from your activity/fragment