home top ad

Android Popup Menu Example

Android Popup Menu Example

Android Popup Menu shows the menu beneath the anchor text if space is accessible in any case over the anchor text. It vanishes on the off chance that you click outside the popup menu.

Android.widget.PopupMenu is the closest section of the Java.lang.Object section. An example of the popup menu for Android We have to see how you can create an popup menu on android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="10dp"
    tools:context=".Pop">


    <Button
        android:id="@+id/pop_up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:onClick="showPopup"
        android:textStyle="bold"
        android:text="Create PopUp Menu" />
</RelativeLayout>
In this layout we have used a button to show the popup menu.To show the popup menu, you will have to right-click on the res folder and create a popup menu (res >> new >> Android Resource Directory). After that you will create a menu layout file by clicking on that menu folder.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item1"
        android:title="Item1"/>

    <item android:id="@+id/item2"
        android:title="Item2"/>

    <item android:id="@+id/item3"
        android:title="Item3"/>

    <item android:id="@+id/item4"
        android:title="Item3"/>
</menu>
After this we will implement all these layouts in MainActivity.java

MainActivity.java

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;

public class MainActivity  extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup);
    }
    public void showPopup(View view)
    {
        PopupMenu pop=new PopupMenu(this,view);
        pop.setOnMenuItemClickListener(this);
        pop.inflate(R.menu.pop_up);
        pop.show();
    }



    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item1:
                Toast.makeText(this, "Item 1 clicked", Toast.LENGTH_SHORT).show();
                return true;

            case R.id.item2:
                Toast.makeText(this, "Item 2 clicked", Toast.LENGTH_SHORT).show();
                return true;

            case R.id.item3:
                Toast.makeText(this, "Item 3 clicked", Toast.LENGTH_SHORT).show();
                return true;

            case R.id.item4:
                Toast.makeText(this, "Item 4 clicked", Toast.LENGTH_SHORT).show();
                return true;

            default:
                return false;

        }

    }
}
Here we have done a method named showPopup by OnClick in the layout.In which we have inflated popup menu.After that, through the onMenuItemClick override method, we will run onclick method on it, in which we will print a toast, here you can do any coding.

Here is a screenshot of this project.
Hope you like this article very much. thanks ...

Post a Comment

0 Comments