How to create a Checkable Popup Menu in Android
Checkable Menus provides more functionality to the menus by providing the option of checking the menu item. 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.
Now lets begin the programming for Popup Menu:
- 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.
- First start your eclipse and select new -> project and click next.
- Now fill with following details:
- 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">
<group android:checkableBehavior="single">
<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>
</group>
</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
Checkable Menus provides more functionality to the menus by providing the option of checking the menu item. 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. how to delete Facebook account
ReplyDeleteGiven so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
ReplyDeleteAndroid Training in chennai
Very informative ..i suggest this blog to my friends..Thank you for sharing
ReplyDeleteAndroid Training in chennai