Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 1.82 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. popup menu expand/collapse from an icon in Action Bar
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:orientation="horizontal"
  5.     android:layout_width="fill_parent"
  6.     android:layout_height="fill_parent">
  7.  
  8.       <ImageView
  9.           android:id="@+id/my_option"
  10.           android:layout_gravity="left"  
  11.           android:src="@drawable/ic_launcher"
  12.           android:layout_width="wrap_content"
  13.           android:layout_height="wrap_content"
  14.           android:layout_weight="1"
  15.        />
  16. </LinearLayout>
  17.        
  18. @Override
  19.     protected void onCreate(Bundle arg0) {
  20.     super.onCreate(arg0);
  21.  
  22.         ActionBar actionBar = getSupportActionBar();
  23.         View actionBarView = getLayoutInflater().inflate(R.layout.action_bar, null);
  24.  
  25.         actionBar.setCustomView(actionBarView);
  26.  
  27.         ImageView actionBarImg = (ImageView) actionBarView.findViewById(R.id.my_option);
  28.         actionBarImg.setOnClickListener(new OnClickListener(){
  29.  
  30.               public void onClick(View view) {
  31.                   //how to pop up a menu which is expand/collapse below the image icon                        
  32.               }
  33.         });
  34.    }
  35.        
  36. private PopupWindow mPopupMenu;
  37. private View mMenuLayout;
  38. private boolean isPopupOpened = false;
  39.        
  40. mMenuLayout = getLayoutInflater().inflate(R.layout.menu_layout, null);
  41. mPopupMenu = new PopupWindow(mMenuLayout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
  42.  
  43. actionBarImg.setOnClickListener(new OnClickListener() {
  44.  
  45.     public void onClick(View view) {
  46.         if (isPopupOpened)
  47.         {
  48.             mPopupMenu.dismiss();
  49.             isPopupOpened = false;
  50.         }
  51.         else
  52.         {
  53.             mPopupMenu.showAsDropDown(tv);
  54.             isPopupOpened = true;
  55.         }                        
  56.     }
  57.  
  58. });