How to Use PopupWindow to implement pop-up menus for Android

Source: Internet
Author: User

When you use UC-WebBrowser, you will find that the pop-up menu is different from the menu that comes with the system. It enables display and column sharding of more menu options. In fact, it is a PopupWindow or AlertDialog dialog box. Add two GridView controls, one is the menu title bar and the other is the menu option. Switching the menu option view can be easily implemented through the adapter transformation.
Click to download the instance:
1. Run:

II. Implementation points:
(1) shielding the system's pop-up menu:
1. First, create at least one menu option for the system.
Copy codeThe Code is as follows: @ Override
Public boolean onCreateOptionsMenu (Menu menu ){

Menu. add ("menu ");
Return super. onCreateOptionsMenu (menu );
}

2. display your menu view in the onMenuOpened method and return FALSE.
Copy codeThe Code is as follows: @ Override
Public boolean onMenuOpened (int featureId, Menu menu ){
MyMenu. showAtLocation (findViewById (R. id. layout), Gravity. BOTTOM, 0, 0 );

Return false; // true -- display the built-in menu; false -- not display.
}

(2) Click the menu bar. When switching the menu view, you only need to reset the current adapter object.
Copy codeThe Code is as follows: gv_body.setAdapter (bodyAdapter [arg2]); // you can change the option view.

(3) inherit from PopupWindow and rewrite a class implementation pop-up dialog box, mainly to better and more easily implement the pop-up menu style and Event Response.
Copy codeThe Code is as follows: public class MyDefinedMenu extends PopupWindow {...}

Iii. Specific Code:
(1) Layout:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/layout"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
</LinearLayout>

(2) program code
1. Main class: MyMenu
Copy codeThe Code is as follows: package com. myandroid. test;
Import java. util. ArrayList;
Import java. util. List;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. Gravity;
Import android. view. Menu;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemClickListener;
Import android. widget. Toast;
Public class MyMenu extends Activity {
Private List <String> titles; // Title Bar
Private List <String> item_names; // option name
Private List <Integer> item_images; // option icon
Private MyDefinedMenu myMenu; // the pop-up menu

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

// The menu title bar is displayed.
Titles = addItems (new String [] {"menu 1", "menu 2", "menu 3 "});
// Option icon
Item_images = new ArrayList <List <Integer> ();
Item_images.add (addItems (new Integer [] {R. drawable. bag,
R. drawable. bluetooth, R. drawable. earth, R. drawable. email }));
Item_images.add (addItems (new Integer [] {R. drawable. map,
R. drawable. news, R. drawable. reader, R. drawable. sound, R. drawable. tape }));
Item_images.add (addItems (new Integer [] {R. drawable. telephone,
R. drawable. bluetooth, R. drawable. earth, R. drawable. email }));
// Option name
Item_names = new ArrayList <List <String> ();
Item_names.add (addItems (new String [] {"shopping", "Bluetooth", "Browser", "email "}));
Item_names.add (addItems (new String [] {"map", "news", "Reader", "Speaker", "Recording "}));
Item_names.add (addItems (new String [] {"phone", "Bluetooth", "Reader", "Mailbox "}));
// Create a pop-up menu object
MyMenu = new MyDefinedMenu (this, titles, item_names,
Item_images, new ItemClickEvent ());

}

/**
* Convert to List <String>
* @ Param values
* @ Return
*/
Private List <String> addItems (String [] values ){

List <String> list = new ArrayList <String> ();
For (String var: values ){
List. add (var );
}

Return list;
}

/**
* Convert to List <Integer>
* @ Param values
* @ Return
*/
Private List <Integer> addItems (Integer [] values ){

List <Integer> list = new ArrayList <Integer> ();
For (Integer var: values ){
List. add (var );
}

Return list;
}

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){

Menu. add ("menu ");
Return super. onCreateOptionsMenu (menu );
}
@ Override
Public boolean onMenuOpened (int featureId, Menu menu ){
MyMenu. showAtLocation (findViewById (R. id. layout), Gravity. BOTTOM, 0, 0 );

Return false; // true -- display the built-in menu; false -- not display.
}

/**
* Menu option Click Event
* @ Author Kobi
*
*/
Class ItemClickEvent implements OnItemClickListener {
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2,
Long arg3 ){
// Display which menu is clicked and which option.
Toast. makeText (MyMenu. this, "Menu:" +
Titles. get (myMenu. getTitleIndex () +
"Item:" + item_names.get (myMenu. getTitleIndex (). get (arg2 ),
Toast. LENGTH_SHORT). show ();
MyMenu. dismiss (); // The Menu disappears.
}

}
}

2. pop-up menu class: MyDefinedMenu,
Copy codeThe Code is as follows: package com. myandroid. test;
Import java. util. List;
Import com. myandroid. test. MyMenu. ItemClickEvent;
Import android. content. Context;
Import android. graphics. Color;
Import android. util. Log;
Import android. view. View;
Import android. view. ViewGroup. LayoutParams;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemClickListener;
Import android. widget. GridView;
Import android. widget. LinearLayout;
Import android. widget. PopupWindow;
Public class MyDefinedMenu extends PopupWindow {

Private LinearLayout layout; // Overall layout
Private GridView gv_title; // menu bar
Private GridView gv_body; // option view
Private BodyAdatper [] bodyAdapter; // option Adapter
Private TitleAdatper titleAdapter; // Title Adapter
Private Context context; // Context
Private int titleIndex; // menu No.

Public MyDefinedMenu (Context context, List <String> titles,
List <String> item_names, List <Integer> item_images,
ItemClickEvent itemClickEvent ){

Super (context );
This. context = context;

// Layout Framework
Layout = new LinearLayout (context );
Layout. setOrientation (LinearLayout. VERTICAL );
Layout. setLayoutParams (new LayoutParams (
LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT ));

// Menu bar
TitleIndex = 0;
Gv_title = new GridView (context );
TitleAdapter = new TitleAdatper (context, titles );
Gv_title.setAdapter (titleAdapter );
Gv_title.setLayoutParams (new LayoutParams (
LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT ));
Gv_title.setNumColumns (titles. size (); // Number of menus
Gv_title.setBackgroundColor (Color. WHITE );

// Option view
BodyAdapter = new BodyAdatper [item_names.size ()]; // view adapters
For (int I = 0; I <item_names.size (); I ++ ){
BodyAdapter [I] = new BodyAdatper (context, item_names.get (I), item_images.get (I ));
}
Gv_body = new GridView (context );
Gv_body.setNumColumns (4); // four options are displayed in each row.
Gv_body.setBackgroundColor (Color. TRANSPARENT );
Gv_body.setAdapter (bodyAdapter [0]); // set the adapter

// Switch menu items
Gv_title.setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2,
Long arg3 ){
TitleIndex = arg2; // record the number of the currently selected menu item
TitleAdapter. setFocus (arg2 );
Gv_body.setAdapter (bodyAdapter [arg2]); // you can change the option view.
}
});

// Set the option Click Event
Gv_body.setOnItemClickListener (itemClickEvent );

// Add title bars and options
Layout. addView (gv_title );
Layout. addView (gv_body );

// Add a menu View
This. setContentView (layout );
This. setWidth (LayoutParams. FILL_PARENT );
This. setHeight (LayoutParams. WRAP_CONTENT );
This. setFocusable (true); // The menu gets the focus. If the focus is not obtained, the control event in the menu cannot respond.

}

/**
* Obtain the currently selected menu items
* @ Return indicates the sequence number of the menu item.
*/
Public int getTitleIndex (){

Return titleIndex;
}

}

3. Menu Bar adapter: TitleAdatper
Copy codeThe Code is as follows: package com. myandroid. test;
Import java. util. List;
Import android. content. Context;
Import android. graphics. Color;
Import android. view. Gravity;
Import android. view. View;
Import android. view. ViewGroup;
Import android. view. ViewGroup. LayoutParams;
Import android. widget. BaseAdapter;
Import android. widget. GridView;
Import android. widget. TextView;
Public class TitleAdatper extends BaseAdapter {
Private List <String> titles;
Private Context context;
Private final TextView [] TV _titels;

Public TitleAdatper (Context context, List <String> titles ){
This. context = context;
This. titles = titles;
TV _titels = new TextView [titles. size ()];
}
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return titles. size ();
}
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}

/**
* After selection, change the menu color.
* @ Param position
*/
Public void setFocus (int position ){

For (int I = 0; I <titles. size (); I ++ ){

TV _titels [I]. setBackgroundColor (Color. WHITE );
}

TV _titels [position]. setBackgroundColor (Color. BLUE );

}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// Menu bar text item
TV _titels [position] = new TextView (context );
TV _titels [position]. setGravity (Gravity. CENTER );
TV _titels [position]. setText (titles. get (position ));
TV _titels [position]. setTextSize (18 );
TV _titels [position]. setLayoutParams (new GridView. LayoutParams (
LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT ));

Return TV _titels [position];
}
}

4. menu item view adapter: BodyAdatper
Copy codeThe Code is as follows: package com. myandroid. test;
Import java. util. List;
Import android. content. Context;
Import android. view. Gravity;
Import android. view. View;
Import android. view. ViewGroup;
Import android. view. ViewGroup. LayoutParams;
Import android. widget. BaseAdapter;
Import android. widget. GridView;
Import android. widget. ImageView;
Import android. widget. LinearLayout;
Import android. widget. TextView;
Public class BodyAdatper extends BaseAdapter {

Private List <String> item_names;
Private List <Integer> item_images;
Private Context context;

Public BodyAdatper (Context context, List <String> item_names,
List <Integer> item_images ){
This. context = context;
This. item_names = item_names;
This. item_images = item_images;
}
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return item_images.size ();
}
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}

@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// Total Layout
LinearLayout layout = new LinearLayout (context );
Layout. setOrientation (LinearLayout. VERTICAL );
Layout. setGravity (Gravity. CENTER );
// Option name
TextView TV _item = new TextView (context );
TV _item.setGravity (Gravity. CENTER );
TV _item.setLayoutParams (new GridView. LayoutParams (
LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT ));
TV _item.setText (item_names.get (position ));
// Option chart
ImageView img_item = new ImageView (context );
Img_item.setLayoutParams (new LayoutParams (50, 50 ));
Img_item.setImageResource (item_images.get (position ));
// Add option icons and names
Layout. addView (img_item );
Layout. addView (TV _item );

Return layout;
}
}

Here we use PopupWindow to implement it. Of course, you can also use AlertDialog or other custom dialogs. You can also rewrite the Menu or use Tab to implement it. There are many implementation methods, but the principle is the same. For example, you can use two GridView, one as the menu bar and the other as the menu item view.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.