2

I am trying to show a popup window at the specific view which is in recycler view but it is not showing at that position.

This is what I did to show a transparent popup window:

private void showPopup(View view) {

    LayoutInflater inflater = (LayoutInflater) HomeActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(R.layout.fadepopup, findViewById(R.id.fadePopup));
    PopupWindow fadePopup = new PopupWindow(layout, 300, 500, true);
    fadePopup.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, android.R.color.transparent)));
    fadePopup.setOutsideTouchable(true);

    fadePopup.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER, 0, 0);
}

Am calling it from recycler view like:

horizontalRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
            @Override
            public void onClick(View view, int position) {

                String menuTitle = menuTitles[position % menuTitles.length];
                Fragment selectedFragment = null;

                switch (menuTitle) {

                    case "Messenger":
                        showPopup(view);
                        break;

                }

            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));

Also, I tried with showAsDropDown but not working.

The result is:

UPDATE:

XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fadePopup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/messenger_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/circular_white"
        android:padding="10dp"
        android:src="@drawable/nav_messanger" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/circular_white"
        android:padding="10dp"
        android:src="@drawable/nav_contacts" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/circular_white"
        android:padding="10dp"
        android:src="@drawable/nav_boss" />

</LinearLayout>
gotwo
  • 663
  • 8
  • 16
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138

2 Answers2

0

From my opinion, you should try a [relative layout][1]. When you have a relative layout filling the whole screen, the Popup should show up at the bottom of the screen when you add android:layout_alignParentBottom to the XML and add the Popup as an Element to the XML that is displayed with on fadePopup with setVisibility:Visible and hidden with setVisibility:Invisible. So you could basically add a custom Layout for the Popup that you pass to the fadePopup Method with the parameter setVisibility:visible.

DWA
  • 530
  • 1
  • 5
  • 29
0

This is the implementation of showAtLocation() in PopupWindow.java:

/**
 * <p>
 * Display the content view in a popup window at the specified location. If the popup window
 * cannot fit on screen, it will be clipped. See {@link android.view.WindowManager.LayoutParams}
 * for more information on how gravity and the x and y parameters are related. Specifying
 * a gravity of {@link android.view.Gravity#NO_GRAVITY} is similar to specifying
 * <code>Gravity.LEFT | Gravity.TOP</code>.
 * </p>
 *
 * @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from
 * @param gravity the gravity which controls the placement of the popup window
 * @param x the popup's x location offset
 * @param y the popup's y location offset
 */
public void showAtLocation(View parent, int gravity, int x, int y) {
    mParentRootView = new WeakReference<>(parent.getRootView());
    showAtLocation(parent.getWindowToken(), gravity, x, y);
}

As you can see the parameter parent is the root view to which the popup will be shown, something like the layout of the activity.
But you pass for this parameter the layout that you inflate inside the popup!
Edit:
You must pass the view on which you want the popup to be anchored and x, y will be the offsets from its position.