Creating a custom popup dialog menu Creating a custom popup dialog menu xml xml

Creating a custom popup dialog menu


You have to use PopupMenu instead of PopupWindow.

enter image description hereenter image description here

Sample Code:

public class MainActivity extends AppCompatActivity {    private Context context;    private ImageView img;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        context = this;        img = (ImageView) findViewById(R.id.imageView);        img.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                PopupMenu popup = new PopupMenu(MainActivity.this, v);                popup.getMenuInflater().inflate(R.menu.pop_up, popup.getMenu());                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {                    public boolean onMenuItemClick(MenuItem item) {                        Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();                        return true;                    }                });                popup.show();//showing popup menu            }        });    }}

layout:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/base"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/white"    android:orientation="horizontal">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="50dp"        android:layout_alignParentStart="true"        android:layout_alignParentTop="true"        android:background="#D6D7D7">        <ImageView            android:id="@+id/imageView"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentEnd="true"            android:layout_centerVertical="true"            android:layout_marginEnd="20dp"            android:gravity="center"            android:src="@mipmap/more" />    </RelativeLayout></RelativeLayout>

menu/pop_up.xml

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:id="@+id/add"        android:title="@string/one" />    <item        android:id="@+id/sub"        android:title="@string/two" />    <item        android:id="@+id/mul"        android:title="@string/three" />    <item        android:id="@+id/div"        android:title="@string/four" /></menu>

Update: Menu Background color change

Use this style in your applied theme.

<resources>    <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>        <!--Add modified themes-->        <item name="android:popupMenuStyle">@style/PopupMenu</item>        <item name="popupMenuStyle">@style/PopupMenu</item>        <item name="android:itemTextAppearance">@style/itemTextStyle.AppTheme</item>    </style>    <style name="PopupMenu" parent="@android:style/Widget.PopupMenu">        <item name="android:popupBackground">#B4B52B</item>    </style>    <style name="itemTextStyle.AppTheme" parent="@android:style/TextAppearance.Widget.IconMenu.Item">        <item name="android:textColor">@color/white</item>    </style></resources>