Home > Blockchain >  How can I use PopupWindow in Xamarin android?
How can I use PopupWindow in Xamarin android?

Time:06-17

I am trying to use PopupWindow to inflate layout, but I have not found any example in internet. Could you teach me how to use it.

This is my current code but it is not showing popupWindow

private void ShowMenu(ImageButton anchor, Dictionary<string, object> data)
{
    LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
    View popupView = inflater.Inflate(Resource.Layout.menu_outlet_buttons, null);

    int width = LinearLayout.LayoutParams.WrapContent;
    int height = LinearLayout.LayoutParams.WrapContent;
    bool focusable = true;

    PopupWindow popupWindow = new PopupWindow(popupView.Context);
    popupWindow.ShowAtLocation(_view, GravityFlags.Center, 0, 0);
}

By the way this is an Adapter

public MyAdapter(List<Dictionary<string, object>> data, Context context)
{
    _data = data;
    _context = context;
}

This is my reference, but I could not reproduce the same in Xamarin Android.

How to create a popup window (PopupWindow) in Android

CodePudding user response:

The method you create the instance of the PopupWindow is wrong. I tried the following code in my project, it worked well:

View view =  LayoutInflater.Inflate(Resource.Layout.popup, null);
PopupWindow popupWindow = new PopupWindow(view,LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent,true);
popupWindow.ShowAtLocation(view, GravityFlags.Center, 0, 0);

Update:

I had checked the popupWindow.ShowAtLocation method, the first parameter is the parent view.

And you can get the x,y by the following code:

int[] location = new int[2];
imagebutton.GetLocationOnScreen(location);
int x = location[0];
int y = location[1];
  • Related