How to make Android Pop-Up Menu

Posted on .
pop

Download Code

Howdy developers ! Today I am going to show you how to code PopUp Menu in android application.This is used to make a use of space and UI is also userfriendly .All the items is in front you just click on your items and can perform action.

 

Lets start with Designing part ,activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click" />

</RelativeLayout>

Now goto you menu folder and create popup_menu.xml and paste the below code there –

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/one"
        android:title="One"/>
	
	<item
        android:id="@+id/two"
        android:title="Two"/>
      
    <item
        android:id="@+id/three"
        android:title="Three"/>
        
</menu>

Now Activity class , MainActivity.java and inflate the popup menu in java file –

package androidarena.popupmenu;

import androidarena.popupmenu.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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 button1;
		 
		 @Override
		 protected void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		  setContentView(R.layout.activity_main);
		  
		  button1 = (Button) findViewById(R.id.btn);
		  
		  button1.setOnClickListener(new OnClickListener() {
		 
		   @Override
		   public void onClick(View v) {
			//Creating the instance of PopupMenu
		    PopupMenu popup = new PopupMenu(MainActivity.this, button1);
		    //Inflating the Popup using xml file
		    popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
		 
		    //registering popup with OnMenuItemClickListener
		    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
		     public boolean onMenuItemClick(MenuItem item) {
		      Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
		      return true;
		     }
		    });
		 
		    popup.show();//showing popup menu by show() method
		   }
		  });
		 
		 }
		 
	}

Now its time to run the project and see the output –

Output –

pop

Feel free to ask any type of query :p

 

Leave a Reply

Your email address will not be published. Required fields are marked *