15 Jan 2013


How to create a Popup Menu in Android

In Android application, menus are the common user interface components which gives user a dynamic user experience. Popup menu is also a type of menu anchored to the view.When some event happens like click on button the it appear below the anchor view otherwise above the anchor view.Other details.
    • Popup menus are available after Android API 11 or higher.
    • They can be use for command giving or providing functionality to the user for some view.
popup menu
Now lets begin the programming for Popup Menu:
  • First start your eclipse and select new -> project and click next.
  • Now fill with following details:
  • Popup menu
    • Project name- PopupMenu
    • Application name- PopupMenu
    • package name-menu.popup
    • Minimum required SDK- select android 3.0
    • tick on create custom laucher icon.
    • tick on create project in workspace and then final.
  • Now open the activity_main.xml layout under the res-> menu-> activity_main.xml and the replace the given code with this new code given below.
  •  <menu xmlns:android="http://schemas.android.com/apk/res/android">  
         <item android:id="@+id/facebook"   
           android:title="facebook">  
         </item>  
         <item android:id="@+id/wordpress"   
           android:title="wordpress">  
         </item>  
         <item android:id="@+id/google"  
            android:title="google">  
         </item>  
       </menu>  
    
  • Now open the activity_main.xml layout under the res-> layout-> activity_main.xml and the replace the given code with this new code given below.

     <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" >  
       <Button  
         android:id="@+id/button1"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_centerHorizontal="true"  
         android:layout_centerVertical="true"  
         android:onClick="showPopup"  
         android:text="Select Website" />  
     </RelativeLayout>  
    

  • Now open the MainActivity.java and replace the previous given code with the new given code below.
  •  package menu.popup;  
     import android.os.Bundle;  
     import android.annotation.SuppressLint;  
     import android.app.Activity;  
     import android.view.MenuInflater;  
     import android.view.MenuItem;  
     import android.view.View;  
     import android.widget.PopupMenu;  
     import android.widget.Toast;  
     import android.widget.PopupMenu.OnMenuItemClickListener;  
     @SuppressLint("NewApi")  
     public class MainActivity extends Activity {  
       @Override  
       public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.activity_main);  
       }  
       public void showPopup(View v) {  
         PopupMenu popup = new PopupMenu(this, v);  
         MenuInflater inflater = popup.getMenuInflater();  
         inflater.inflate(R.menu.activity_main, popup.getMenu());  
         popup.show();  
       }  
       public void showMenu(View v) {  
         PopupMenu popup = new PopupMenu(this, v);  
         popup.setOnMenuItemClickListener((OnMenuItemClickListener) this);  
         popup.inflate(R.menu.activity_main);  
         popup.show();  
       }  
       public boolean onMenuItemClick(MenuItem item) {  
         switch (item.getItemId()) {  
         case R.id.facebook:  
           Toast.makeText(this,"you hava selected the facebook",Toast.LENGTH_SHORT).show();  
            return true;  
         case R.id.wordpress:  
               Toast.makeText(this,"you hava selected the wordpress",Toast.LENGTH_SHORT).show();  
           return true;  
         case R.id.google:  
               Toast.makeText(this,"you hava selected the google",Toast.LENGTH_SHORT).show();  
           return true;  
           default:   
                return false;  
         }  
       }  
     }  
    
  • Now lets understand some imp. keyword in MainActivity.java
    • android.view.MenuInflater for inflating the menu from xml format.
    • android.view.MenuItem ,an interface for direct access to previously created menu item.
    • android.widget.Toast for a view containing little message for the user.
    • android.annotation.SuppressLint indicates the lint to ignore warning for specific annotated elements.
    • android.widget.PopupMenu, displays menu in model popup window anchored to a view.
    • android.widget.PopupMenu.OnMenuItemClickListener,interface responsible for receiving menu item click events if item don't have individual click listener.
    • @SuppressLint("NewApi"), used for ignoring the NEW API issue as popup menu works after API 11 or higher.
    • onMenuItemClick(MenuItem item) called when menu item has been invoked.
  • Now it time to run the project and when you click on the button it will show the popup menu

Popup Menu

No comments:

Post a Comment

Translate