14 Popup Menu


Algorithm:
Step 1) Create a new Android Studio Project named as popupmenu.

Step 2)Goto Project Structure and open the activity_main.xml file.

Step 3) Create menu Android resource Directory and inside menu folder popup menu resource file.

Step 4) open menu resource file and create the list of items you want to display in popup menu as options.
Step 5) implement PopupMenu.OnMenuItemClickListener Interface to MainActivity class
and Implement the following methods in the MainActivity class.



public void showPopup(View view) {
   /* user defined method by using android:onClick=”showPopup” in activity_main.xml file.*/
//code
}



@Override
public boolean onMenuItemClick(MenuItem item) {
//code





return false;
}

Step 7) Run the application from the menu bar select Run and Click on Run App option.

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example..popupmenu.MainActivity">

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="Click"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<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>


MainActivity.java
package com.example.popupmenu;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.MenuItem; import android.view.View;
import android.widget.PopupMenu; import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
}

public void showPopup(View view) {
PopupMenu popup=new PopupMenu(this,view); popup.setOnMenuItemClickListener(this); popup.inflate(R.menu.popup_menu); popup.show();
}

@Override
public boolean onMenuItemClick(MenuItem item) { int id=item.getItemId();
switch (id){
case R.id.list_item1:
Toast.makeText(this,"Item1 Clicked",Toast.LENGTH_SHORT).show(); case R.id.list_item2:
Toast.makeText(this,"Item2 Clicked",Toast.LENGTH_SHORT).show(); case R.id.list_item3:
Toast.makeText(this,"Item3 Clicked",Toast.LENGTH_SHORT).show(); case R.id.list_item4:
Toast.makeText(this,"Item4 Clicked",Toast.LENGTH_SHORT).show();

}
return false;
}
OUTPUT:


Comments

Popular posts from this blog

20 Simple Content Provider Example

17 Simple Notification Program