Inflate Popup Menu Dialog in Android

Narayan Panthi
2 min readMay 26, 2019

--

Simple way to Implement popup window in android.

Hi, This article we will talk about inflating popup menu dialog in android. We can inflate any layout over any view as we want. Here’s a sample we are going to make.

Android PopupView Before & After Setting icon clicked

Create a showPopupMenu() function.

private void showPopupMenu() throws RuntimeException {

/**
* pop up window for showing list of logged In
*/

LayoutInflater layoutInflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
@SuppressLint("InflateParams")
View popupView = layoutInflater.inflate(R.layout.popup_menu_item_main, null);
LinearLayout logoutParent = popupView.findViewById(R.id.logout_parent);
LinearLayout switchParent = popupView.findViewById(R.id.switch_project_parent);
popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.showAsDropDown(toolbar);
popupWindow.showAsDropDown(toolbar, 50, -48);


logoutParent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
showLogoutDialog();

}
});


switchParent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
showSiteChooseDialog("switch", Utilities.getLoginResponse());
}
});

}

Drawable

popupmenu_item_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:background="@drawable/card_drawable_bottom_corner"
android:orientation="vertical">

<LinearLayout
android:id="@+id/switch_project_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackgroundBorderless"
tools:targetApi="lollipop"
android:layoutAnimation="@anim/layout_animation">

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:src="@drawable/ic_repeat"
android:tint="@color/colorBlack" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:fontFamily="@font/normal"
android:includeFontPadding="false"
android:text="Switch Project"
android:textColor="@color/colorBlack"
android:textSize="16sp" />
</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="0.7dp"
android:background="@color/colorLightLightGrey" />

<LinearLayout
android:id="@+id/logout_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
tools:targetApi="lollipop"
android:layoutAnimation="@anim/layout_animation">

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:src="@drawable/ic_logout"
android:tint="@color/colorBlack" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:fontFamily="@font/normal"
android:includeFontPadding="false"
android:text="Logout"
android:textColor="@color/colorBlack"
android:textSize="16sp" />
</LinearLayout>

</LinearLayout>

Thank you. :)

--

--

Narayan Panthi
Narayan Panthi

Written by Narayan Panthi

Software Engineer | Writes about Android |🇳🇵

No responses yet