Skip to content

Instantly share code, notes, and snippets.

@kevalpatel2106
Created November 23, 2016 17:03
Show Gist options
  • Save kevalpatel2106/cb70438f1a455c9c109150992ad28523 to your computer and use it in GitHub Desktop.
Save kevalpatel2106/cb70438f1a455c9c109150992ad28523 to your computer and use it in GitHub Desktop.
public class FloatingViewService extends Service {
private WindowManager mWindowManager;
private View mFloatingView;
public FloatingViewService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//Inflate the floating view layout we created
mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);
//Add the view to the window.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
params.x = 0;
params.y = 100;
//Add the view to the window
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView, params);
//….
//….
}
@Override
public void onDestroy() {
super.onDestroy();
if (mFloatingView != null) mWindowManager.removeView(mFloatingView);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment