Skip to content Skip to sidebar Skip to footer

Can A View On A Popupwindow Display A Popupmenu?

In Android API11+ I'm displaying a button inside a PopupWindow. I'd like to show a PopupMenu when the button is clicked, without closing the PopupWindow. Is this possible at all? I

Solution 1:

You can't anchor a PopupMenu from a PopupWindow view what you can do is define or anchor from a view inside roow view or top-level view here is an example:

main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity" >


        <ImageView
            android:id="@+id/IVoptionsMenuInvis"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_gravity="center_horizontal"
            android:layout_weight="25"            
            android:paddingBottom="6dp"
            android:paddingTop="6dp">

</FrameLayout>

popupwindow.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/settingseditback"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity" >

<ImageView
            android:id="@+id/IVoptionsMenu"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_gravity="center_horizontal"
            android:layout_weight="25"
            android:onClick="showSettingsPopup2"
            android:paddingBottom="6dp"
            android:paddingTop="6dp"
            android:src="@drawable/ic_actionbar_overflow_dark" />

</LinearLayout>

and finally your MainActivity.java:

publicclassMainActivityextendsActivity {
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
    }
.
.
.
    publicvoidshowSettingsPopup2(View v) {
        PopupMenupopup=newPopupMenu(MainActivity.this, findViewById(R.id.IVoptionsMenuInvis));
        popup.setOnMenuItemClickListener(this);
        popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
        popup.show();
    }
}

Post a Comment for "Can A View On A Popupwindow Display A Popupmenu?"