Android programming popupwindow pop up screen background to become translucent effect

  • 2020-12-21 18:11:00
  • OfStack

This article describes the Android programming popupwindow pop-up screen background to become translucent effect method. To share for your reference, the details are as follows:

When popupwindow pops up in android, it's not uncommon for the screen background to become translucent. There are many ways to implement it. I've probably used the simplest one, which is to set the transparency of getWindows under 1. Let's not talk about code


/**
*  Sets the background transparency for the add screen 
* @param bgAlpha
*/
public void backgroundAlpha(float bgAlpha)
{
    WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
}

Set to method because when popwindow is turned off we need to change this transparency back.


popWin = new PopupWindow(popAddNoteType, mScreenWidth *8 /10, ViewGroup.LayoutParams.WRAP_CONTENT);
// in PopupWindow So I'm going to add the following code so that when the keyboard pops up, it doesn't block it pop Window. 
popWin.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popWin.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
// When clicking on a blank, hide pop window 
popWin.setFocusable(true);
popWin.setBackgroundDrawable(new BitmapDrawable());
backgroundAlpha(1f);
// add pop Window closing event 
popWin.setOnDismissListener(new poponDismissListener());

As someone might ask here, when I click on the margins, the pop window is gone, but the background is still translucent, which doesn't work. We're just going to use one event here


/**
*  When you add a new note popWin Closed events, mainly in order to change the background transparency back 
* @author cg
*
*/
class poponDismissListener implements PopupWindow.OnDismissListener{
    @Override
    public void onDismiss() {
      // TODO Auto-generated method stub
      //Log.v("List_noteTypeActivity:", " I am closing the event ");
      backgroundAlpha(1f);
    }
}

The above

popWin.setOnDismissListener(new poponDismissListener());

This event is called in the code for

I hope this article has been helpful in Android programming.


Related articles: