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.
- First start your eclipse and select new -> project and click next.
- Now fill with following details:
- 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
No comments:
Post a Comment