Thursday, February 19, 2015

Android - Showing Overlay Window on top of all activities

This can be achieved if we are using the SYSTEM_ALERT_WINDOW. As per the API document, this should be used by only very few Apps. 
these windows are intended for system-level interaction

Code looks something like this 

public class OverlayViewService extends Service
{
private WindowManager windowManager; 
private ImageView overlayViewImage;
@override public IBinder onBind(Intent intent)
{
return null;
}
@override public voice onCreate()
{
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
overlayViewImage = new ImageView(this);
overlayViewImage.setImageResource(R.drawable.overlay_image);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelForamt.TRANSLUCENT
);

params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(overlayViewImage, params);
}

@override
public void onDestroy()
{
super.onDestroy();
if(overlayViewImage != null) windowManager.removeView(overlayViewImage);
}
}

now start the service like below 

startService(new Intent(context, OverlayViewService.class));

References:

No comments:

Post a Comment