Search code examples
androidpopupwindowpopupmenu

Open PopupWindow at top of the view


I want to have a show more button on click of which a popup should be opened with 3 options by default, but as per some logic then options would change ie. hide one of the options based on some condition.

enter image description here

I tried to add a popup menu:

popup = new PopupMenu(this, _showMore, GravityFlags.Top);

popup.MenuInflater.Inflate(Resource.Menu.showMoreMenu, popup.Menu);
if (!string.IsNullOrEmpty(someValue))
{
    popup.Menu.FindItem(Resource.Id.ABC1).SetVisible(false);
}

popup.MenuItemClick += Popup_MenuItemClick;
popup.Show();

But in this case, initially, when I have all 3 options the popup is displayed above the button as there is less space below the show more button. But if I hide one of the options then my popup menu opens up at the bottom of the button. I want my popup menu to always be opened above the anchor button.

Then I also tried

PopupWindow popupWindow = new PopupWindow(this); // inflet your layout or diynamic add view

LayoutInflater inflater = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
View view = inflater.Inflate(Resource.Layout.TestLayout, null, false);

TextView abc1 = (TextView)view.FindViewById(Resource.Id.abc1);
TextView abc2 = (TextView)view.FindViewById(Resource.Id.abc2);
TextView abc3 = (TextView)view.FindViewById(Resource.Id.abc3);
abc1.Click += abc1_Click;
abc2.Click += abc2_Click;
if (!string.IsNullOrEmpty(somecondition))
{
    abc1.Visibility = ViewStates.Gone;
}
else
{
    abc2.Visibility = ViewStates.Gone;
}

popupWindow.Focusable = (true);
popupWindow.Width = (WindowManagerLayoutParams.WrapContent);
popupWindow.Height = (WindowManagerLayoutParams.WrapContent);
popupWindow.ContentView = (view);
popupWindow.SetBackgroundDrawable(new ColorDrawable(Color.Transparent));

/*
//tried
var tttt= (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, Resource.Dimension._15sdp, Resources.DisplayMetrics);

//tried
int test=(int)(Resource.Dimension._15sdp * Resources.DisplayMetrics.Density);

//tried
float scale = Resources.DisplayMetrics.Density;
test= (int)(Resource.Dimension._15sdp * scale + 0.5f);

//tried
var dp = Resource.Dimension._15sdp;
int pixel = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, dp, Resources.DisplayMetrics);
*/

popupWindow.ShowAsDropDown(_showMore, -220, -570); <-- fixed offset which works for 3 options

The above code works but I have set fixed offset values which I assume will not render it correctly on different resolutions of the phone and also when I hide one of the options from the menu.

I also tried using ListPopupWindow but the same issue of setting the location. Can anybody please help me in setting offsets dynamically.


Solution

  • could it work like this :

    int[] location = new int[2];
    _showMore.getLocationOnScreen(location);
    popupWindow.showAtLocation(_showMore,Gravity.NO_GRAVITY,location[0], location[1] - popupWindow.getHeight());