user2261597
user2261597

Reputation:

Android: Transparend background when using WindowManager

Samsung Galaxy S6 Edge, Android 6.0.1 (if it matters)

I have made a floating button active in the current Activity only. It's done with XML resource files where you can set the background to be transparent in different ways. This works ok.

Now I want to make this button "global", that is on top of any window/activity currently running. To acheive this I use WindowManager and grant ACTION_MANAGE_OVERLAY_PERMISSION. This also works ok but the button properties are managed by Windowmanager, not by an XML file. The result is that the background of the button image is not transparent. See pictures, left with XML, right with WindowManager.

enter image description here

How can I either:

a) Use the XML file with WindowsManager so the button image gets a transparent background?

or

b) Add some property to the button image through WindowsManager so the image gets a transparent background?

@Override
public void onCreate()
{
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    m_btnRecord = new ImageButton(this);
    m_btnRecord.setImageResource(R.drawable.ic_recordbutton);
    m_btnRecord.setOnTouchListener(m_recordTouchListener);
    params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
    params.x = 0;
    params.y = 0;
    windowManager.addView(m_btnRecord, params);
}

Upvotes: 0

Views: 3272

Answers (1)

redAllocator
redAllocator

Reputation: 735

There are two ways

1.using xml

LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.RIGHT | Gravity.TOP;
    addview = li.inflate(R.layout.traslucent, null);

    wm.addView(myview, params);
  1. set Alpha option in Imageview

m_btnRecord.setAlpha(0);

Upvotes: 2

Related Questions