Wednesday, June 3, 2015

Android Pop Up Menus

After finishing previous session on Option Menu, Context (Long Press) Menus. Lets see the most popular type of menu in android that is pop up menu. to handle some common functionality into applications we must use such type of menus.

These menus appear in context and can be attached to any view in your hierarchy in a non-intrusive manner. This tutorial will walk you through the basics of PopupMenu. Feel free to follow along with the step-by-step tutorial below.



       1. Create a new Android project in Eclipse. The PopupDemo class requires you to target API level 11 or greater. Be sure to rename your startup activity to PopupDemo.java and the associate layout to activity_popup_demo.xml.
     2. In activity_popup_demo.xml  I am adding one simple button so we can add click Listener on it. so popup menu will appear on same view.


activity_popup_demo.xml

 <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:background="@drawable/main"
    tools:context=".PopupDemo" >

    <Button
        android:id="@+id/button1"
        android:layout_width="142dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
      
        android:background="@drawable/pop" />
  
</RelativeLayout>


Next we will work with PopupDemo.java in the /src folder. The main class extends activity and implements both an on click listener and the on menu item click listener. In the on create override we create a new menu and wire it up. The popup menu class is based on the common menu item entity, so if you've used context menus or standard menus it should look familiar.


PopupDemo.java
 


import android.app.Activity;
import android.content.Intent;
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 PopupDemo extends Activity {
Button p;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_popup_demo);
  p = (Button) findViewById(R.id.button1);
  p.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    //creating the instance of PopMenu class.
    PopupMenu pop = new PopupMenu(getApplicationContext(), p);
    //inflate the xml layout
    pop.getMenuInflater().inflate(R.menu.main, pop.getMenu());
    //registering popup OnMenuItemClickListener.
    pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
     
     @Override
     public boolean onMenuItemClick(MenuItem item) {
      // TODO Auto-generated method stub
      Toast.makeText(getApplicationContext(), " ur choice is "+item.getTitle(), Toast.LENGTH_LONG).show();
      return true;
     }
    });
    pop.show();//showing popup menu.
    
   }
  });
 }
 @Override
 public void onBackPressed() {
  Intent intent = new Intent(this, MainActivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
    | Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);
 }
 
}
 
Now, run this Application.
 

Stay tune for next session happy codding :)

No comments:

Post a Comment