Home > OS >  How to inflate Custom Layout in Xamarin Android like Reaction in FB messenger?
How to inflate Custom Layout in Xamarin Android like Reaction in FB messenger?

Time:06-20

I want to show a custom layout in Popmenu. But I get this error

Java.Lang.RuntimeException: 'Expecting menu, got LinearLayout'

Is there a way to inflate this layout to PopupMenu? This is my code and xml

private void ShowMenu(ImageButton anchor, Dictionary<string, object> data)
{
    Android.Widget.PopupMenu menu = new Android.Widget.PopupMenu(_context, anchor);
    menu.Inflate(Resource.Layout.menu_outlet_buttons);
    menu.Show();
}

menu_outlets_buttons.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="left"
        android:background="@drawable/bg_round_corner_full">

        <ImageButton
            android:id="@ id/view_outlet_more_button"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:foreground="?android:attr/selectableItemBackgroundBorderless"
            android:background="@color/transparent"
            android:tint="@color/primaryColor"
            android:scaleType="fitCenter"
            android:src="@drawable/outline_more_vert_24"
            android:padding="10dp"/>
                    
        <ImageButton
            android:id="@ id/view_outlet_edit_button"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:foreground="?android:attr/selectableItemBackgroundBorderless"
            android:background="@color/transparent"
            android:tint="@color/primaryColor"
            android:scaleType="fitCenter"
            android:src="@drawable/outline_edit_24"
            android:padding="10dp"/>

        <Button
            android:id="@ id/view_outlet_call_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Make a call"
            android:background="?android:attr/selectableItemBackground"
            android:backgroundTint="@color/whiteFull"
            android:textColor="@color/secondaryColor"
            android:textSize="15dp"
            android:layout_marginRight="10dp"
            android:drawableLeft="@drawable/shopping_cart_24px"
            android:drawableTint="@color/secondaryColor"/>
    </LinearLayout>
</LinearLayout>

This is what it look likes:

enter image description here

I want to do the reaction in FB messenger

enter image description here

I already search in internet but most of the post is not Xamarin.Android. Thanks in advance

CodePudding user response:

Using PopupWindow. How can I use PopupWindow in Xamarin android?

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

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

    PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent, true);
    popupWindow.ShowAtLocation(view, GravityFlags.NoGravity, x, y);
}
  • Related