Android App Development With AndroidCource: how to create popup menu in android
Showing posts with label how to create popup menu in android. Show all posts
Showing posts with label how to create popup menu in android. Show all posts

Monday, 21 August 2017

PopUp Menu Example in Android

PopUpMenuActivity.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:id="@+id/activity_pop_up_menu"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:orientation="vertical"
              tools:context="com.bhaumik.programmingstudy.PopUpMenuActivity">

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:id="@+id/show_popup"
            android:text="Click Here"
            android:textAllCaps="false"
            android:textSize="18sp"/>

</LinearLayout>
PopUpMenu.java
package com.bhaumik.programmingstudy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class PopUpMenuActivity extends AppCompatActivity {

    Button popups;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_up_menu);

        popups = (Button) findViewById(R.id.show_popup);

        popups.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                PopupMenu popupMenu = new PopupMenu(PopUpMenuActivity.this,popups);
                popupMenu.getMenuInflater().inflate(R.menu.option_menu,popupMenu.getMenu());

                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(PopUpMenuActivity.this,"Menu Selected Is : " + item.getTitle(),Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });
                popupMenu.show();
            }
        });
    }

}

Ads