
Untitled
By: a guest on
Apr 29th, 2012 | syntax:
None | size: 1.82 KB | hits: 22 | expires: Never
popup menu expand/collapse from an icon in Action Bar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/my_option"
android:layout_gravity="left"
android:src="@drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
ActionBar actionBar = getSupportActionBar();
View actionBarView = getLayoutInflater().inflate(R.layout.action_bar, null);
actionBar.setCustomView(actionBarView);
ImageView actionBarImg = (ImageView) actionBarView.findViewById(R.id.my_option);
actionBarImg.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
//how to pop up a menu which is expand/collapse below the image icon
}
});
}
private PopupWindow mPopupMenu;
private View mMenuLayout;
private boolean isPopupOpened = false;
mMenuLayout = getLayoutInflater().inflate(R.layout.menu_layout, null);
mPopupMenu = new PopupWindow(mMenuLayout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
actionBarImg.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (isPopupOpened)
{
mPopupMenu.dismiss();
isPopupOpened = false;
}
else
{
mPopupMenu.showAsDropDown(tv);
isPopupOpened = true;
}
}
});