Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style PopupMenu Android

How do i style a PopupMenu in android? i want a grey background and a icon before the text.

  • can i fix this dynamic in java code.
  • do i fix this with a global style file?

like this:

<style name="GreenText" parent="@android:style/PopupMenu">
    <item name="android:textColor">#00FF00</item>
</style>

or can i better build an PopupWindow like in this thread?

inflate popup menu items programatically

Thank you.

like image 362
Mitch Avatar asked Nov 05 '22 03:11

Mitch


1 Answers

i couldn't find an easy way to style my PopupMenu so i used "PopupWindow" instead, pass a listview to it , and style it as i want.

popView=layoutInflater.inflate(R.layout.pop_layout, null); // layout with a listview to     put in the popup window
lv=(ListView)popView.findViewById(R.id.pop_listview); // then set listview parameters

final PopupWindow contentsopupWindow = new PopupWindow(popView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
           contentsopupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pixel_dot));// set a background so the menu disappears when you click outside it
          contentsopupWindow.setTouchable(true);
          contentsopupWindow.setFocusable(true);
          contentsopupWindow.setOutsideTouchable(true);
          contentsopupWindow.setTouchInterceptor(new OnTouchListener() {
           public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        contentsopupWindow.dismiss();
                        return true;
                        }
                        return false;
                        }
            });
           WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);


            contentsopupWindow.showAsDropDown(anchorView); 
like image 72
Bassem Wissa Avatar answered Nov 15 '22 00:11

Bassem Wissa