Reputation: 820
I am using a recyclerview
which has a menu for each of its item. I am using a pop up window to achieve the same instead of pop up menu
as I have a customized layout. The issue that I am facing is that when I open the menu from the items at bottom of the screen my menu is not fully visible and gets cut is there a way to solve it?
Here is my Menu( it has 4 options)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menuItemLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shadow_menu"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:id="@+id/menuAttendeeMeet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_marginTop="9dp"
android:paddingBottom="12dp"
android:paddingTop="12dp">
<ImageView
android:id="@+id/ivattendee_meet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/meet_icon" />
<TextView
android:id="@+id/tvattendee_meet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Meet"
android:layout_marginRight="16dp"
android:textColor="@color/textPrimary"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/menuAttendeeChat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="12dp"
android:paddingTop="12dp">
<ImageView
android:id="@+id/ivattendee_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/chat_icon" />
<TextView
android:id="@+id/tvattendee_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chat"
android:layout_marginRight="16dp"
android:textColor="@color/textPrimary"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/menuAttendeeTakeANote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="12dp"
android:paddingTop="12dp">
<ImageView
android:id="@+id/ivattendee_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/take_a_note_icon" />
<TextView
android:id="@+id/tvattendee_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take a note"
android:layout_marginRight="16dp"
android:textColor="@color/textPrimary"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/menuAttendeeBusinessCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:gravity="center_vertical"
android:layout_marginBottom="9dp"
android:orientation="horizontal"
android:paddingBottom="12dp"
android:paddingTop="12dp">
<ImageView
android:id="@+id/ivattendee_business_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/business_card_icon" />
<TextView
android:id="@+id/tvattendee_business_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Business Card"
android:layout_marginRight="16dp"
android:textColor="@color/textPrimary"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Here is my pop up window inside the adapter:
final PopupWindow popupWindow = new PopupWindow(v.getContext());
LayoutInflater inflater = (LayoutInflater) v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_pop_up_menu_attendee, null);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setOutsideTouchable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
int ivHeight = holder.ivAttendeeMenu.getHeight() - 20;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
popupWindow.showAsDropDown(holder.ivAttendeeMenu, 0, -ivHeight, Gravity.END);
}
View container;
if (android.os.Build.VERSION.SDK_INT > 22) {
container = (View) popupWindow.getContentView().getParent().getParent();
} else {
container = (View) popupWindow.getContentView().getParent();
}
WindowManager wm = (WindowManager) v.getContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams p = (WindowManager.LayoutParams) container.getLayoutParams();
p.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
p.dimAmount = 0.3f;
wm.updateViewLayout(container, p);
Upvotes: 4
Views: 3727
Reputation: 118
You can open the menu to the top if there is not enough space to show the entire menu and use the following code
final PopupWindow popupWindow = new PopupWindow(v.getContext());
LayoutInflater inflater = (LayoutInflater) v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_pop_up_menu_attendee, null);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setOutsideTouchable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
//First we get the position of the menu icon in the screen
int[] values = new int[2];
holder.ivAttendeeMenu.getLocationInWindow(values);
int positionOfIcon = values[1];
System.out.println("Position Y:" + positionOfIcon);
//Get the height of 2/3rd of the height of the screen
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int height = (displayMetrics.heightPixels * 2) / 3;
System.out.println("Height:" + height);
//If the position of menu icon is in the bottom 2/3rd part of the screen then we provide menu height as offset but in negative as we want to open our menu to the top
if (positionOfIcon > height) {
popupWindow.showAsDropDown(holder.ivAttendeeMenu, 0, -320);
} else {
popupWindow.showAsDropDown(holder.ivAttendeeMenu, 0, 0);
}
Upvotes: 8