4

I created this popup menu but the background shadow is missing. How can I add some? It would be cool if the shadow is only on the left and the bottom.

Here is a picture: You can see that the color of the popup and the background of the activity below the toolbar go hand in hand.

picture

Here is my code:

acitivity snippet

public void showPopup(final MenuItem menuItem) {
        View view = findViewById(R.id.action_alarm);

        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.popup, null);
        final ListView listView = (ListView) popupView.findViewById(R.id.listView);
        String[] functions = {getString(R.string.benachrichtigung), getString(R.string.benachrichtigungUm)};
        final ListAdapter adapter = new CustomPopupAdapter(this, functions, listView);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = (TextView) listView.getChildAt(1).findViewById(R.id.tvTime);
                showTimePickerDialog(tv);
            }
        });

        PopupWindow popupWindow = new PopupWindow(
                popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(view);
    }

popup.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:background="@color/white">

        <ListView
            android:id="@+id/listView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </ListView>

</RelativeLayout>

EDIT:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shadow_192256));
        } else {
            popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shadow_192256));
        }
Oliver U.
  • 336
  • 1
  • 4
  • 14

2 Answers2

4

I had the same problem few days ago :) This is how I solved it. Below link will take you to a website where you can generate a shadow whatever you want :)

http://inloop.github.io/shadow4android/

This is a 9 patch image :) Once done all you have to do is :)

  Display display = (yourActivity.getWindowManager().getDefaultDisplay();
  Point size = new Point();
  display.getSize(size);
  int width = size.x;
  int height = size.y;

  Resources resources = yourActivity.getResources();
  int navigationBarHeight = 0;
  int statusbarHeight = 0;
  int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
  if (resourceId > 0) {
      navigationBarHeight = resources.getDimensionPixelSize(resourceId);
   }

  resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
  if (resourceId > 0) {
       statusbarHeight = resources.getDimensionPixelSize(resourceId);
            }
 popupWindow = new PopupWindow(yourActivity);
 popupWindow.setContentView(yourlayout);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                popupWindow.setBackgroundDrawable(resources.getDrawable(R.drawable.shadow, yourActivity.getTheme()));
            } else {
                popupWindow.setBackgroundDrawable(resources.getDrawable(R.drawable.shadow));
            }
            popupWindow.setWidth(width - 20);//20 is padding i have added 10 from right 10 from left
            popupWindow.setHeight(height - (navigationBarHeight +40));
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);   
            popupWindow.showAtLocation(youractivityView, Gravity.CENTER, 0, statusbarHeight);

Thats it :) You are done :)

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
2

I think the most straightforward way to do this is by setting the view's elevation, which is available in API 21 and above. This is what worked well for me:

if (Build.VERSION.SDK_INT >= 21) {
    popupWindow.setElevation(10);
}
Alan Kinnaman
  • 877
  • 12
  • 20