Monday, 24 October 2016

PopupMenu

A popup menu shows a rundown of things in a vertical rundown that is secured to the view that conjured the menu. It's useful for giving a flood of activities that identify with particular substance or to give choices to a second part of a summon. Activities in a popup menu ought not specifically influence the relating substance—that is the thing that relevant activities are for. Or maybe, the popup menu is for stretched out activities that identify with areas of substance in your action.

Example :-
               PopupMenu ppm = new PopupMenu(this,view);
               getMenuInflater().inflate(R.menu.main,ppm.getMenu());
                ppm.show();

Code for PopupMenu in android.

activity_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="match_parent" 
 android:orientation="vertical" 
 tools:context="com.kiranapp.MainActivity">
<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Popup" 
 android:id="@+id/button" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

popup_menu.xml :-

<?xml version="1.0" encoding="utf-8"?> 
<menu 
 xmlns:android="http://schemas.android.com/apk/res/android">

<item 
 android:id="@+id/android" 
 android:title="Android"/>
<item 
 android:id="@+id/ios" 
 android:title="IOS"/>
<item 
 android:id="@+id/phonegap" 
 android:title="Phonegap"/>
<item 
 android:id="@+id/blackberry" 
 android:title="Blackberry"/>
</menu>

MainActivity.java :-

package com.kiranapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
public class MainActivity extends Activity {
    Button popupbtn;

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

 popupbtn = (Button) findViewById(R.id.button);
 popupbtn.setOnClickListener(new OnClickListener() {

@Override 
 public void onClick(View v) {

 PopupMenu popup = new PopupMenu(MainActivity.this, popupbtn);
 popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
 popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
 
 public boolean onMenuItemClick(MenuItem item) {
 Toast.makeText(MainActivity.this,"Click menu:" + item.getTitle(),
                                                  Toast.LENGTH_SHORT).show();
 return true;
                    }
 });
 popup.show();
 }
 });
 }
}

Output :-

PopupMenu 1
PopupMenu fig :1





















PopupMenu2
PopupMenu fig :2





















PopupMenu3
PopupMenu fig :3















































1 comment: