Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom PopupWindow with BitmapDrawable deprecated method

In my App using custom PopupWindow where am using

popup.setBackgroundDrawable(new BitmapDrawable());

method. Now it is deprecated here and without this method i can't be able to give background for my popup. I read from article that its alternative is

popup.setsetBackgroundDrawable(new BitmapDrawable(Context.Resources,Drawable);

but here i am not using any drawable for my popUp. My code is given below where i am making my custom Popupwindow,here to fix this problem.

@Override
    public void onWindowFocusChanged(boolean hasFocus) {

        int[] location = new int[2];
        Button button = (Button) findViewById(R.id.VBG_img_pin_x);

        // Get the x, y location and store it in the location[] array
        // location[0] = x, location[1] = y.
        button.getLocationOnScreen(location);

        // Initialize the Point with x, and y positions
        p = new Point();
        p.x = location[0];
        p.y = location[1];
    }

    // The method that displays the popup.
    private void showPopup(final Activity context, Point p) {

//      Display display = getWindowManager().getDefaultDisplay();

        DisplayMetrics display = this.getResources().getDisplayMetrics();

        int width = display.widthPixels;
        int height = display.heightPixels;

//      int width = display.getWidth(); // deprecated
//      int height = display.getHeight(); // deprecated

        int popupWidth = width / 2;
        int popupHeight = height;

        ChatUtils.getScreenHeight(getBaseContext());

        // Inflate the popup_layout.xml
        LinearLayout viewGroup = (LinearLayout) context
                .findViewById(R.id.popup);

        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

        onSetAlpha(240, layout);
//      layout.setAlpha((float) 0.95);

        // Creating the PopupWindow
        final PopupWindow popup = new PopupWindow(context);

        popup.setTouchable(true);
        popup.setFocusable(false);
        popup.setOutsideTouchable(true);
        popup.setContentView(layout);
        popup.setWidth(popupWidth);
        popup.setHeight(popupHeight);
        popup.setFocusable(true);

        // Some offset to align the popup a bit to the right, and a bit down,
        // relative to button's position.
        // Clear the default translucent background
        popup.setBackgroundDrawable(new BitmapDrawable());

}
like image 559
Muhammad Usman Khan Avatar asked Feb 10 '23 17:02

Muhammad Usman Khan


2 Answers

if want to clean the background, as stated in your comment

//Clear the default translucent background

you can use

popup.setBackgroundDrawable(null);
like image 195
Blackbelt Avatar answered Feb 15 '23 06:02

Blackbelt


Use this if you want to color the background

 popup.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));

This is not deprecated.

like image 32
Vikas Avatar answered Feb 15 '23 04:02

Vikas