Open In App
Related Articles

How to Draw Over Other Apps in Android?

Like Article
Save Article
Report issue

Sometimes we require our app to show some content on the main screen irrespective of the app running in the foreground, this process is known as drawing over other apps. There are many apps that use this functionality to provide maximum features with minimum screen coverage. This also enables the user to do multitasking. For example, the chat bubble of Facebook Messenger, Mobile Over Usage notice of YourHour App, Voice Command of Google, and many more. We can create similar functionality using the WindowManager interface provided by Android SDK.

Android WindowManager

The Android WindowManager is a system service that controls which windows are displayed and how they are arranged on the screen. When you launch or close an app or rotate the screen, it automatically executes window transitions and animations, among other things.

Every activity has its own Window that displays its content on the screen. When you execute setContentView on an activity, it adds the view to the default window of the activity. Because the default window fills the screen and hides any other activities, the WindowManager will show whatever window is on top. So, in most cases, you don’t need to bother about windows; simply build activity, and Android will take care of the rest. In order to manipulate the Window, we need to interact with the window manager. Now in order to display over other apps, we need to create some kind of service, because the activity will close when some other app comes into the foreground.

What we are going to build in this article? 

A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the AndroidManifest.xml file

In order to draw over other apps we require, android.permission.SYSTEM_ALERT_WINDOW permissions and for android with API version > 23 we need to ask for this on runtime. android.permission.SYSTEM_ALERT_WINDOW allows an app to create windows using the type WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, shown on top of all other apps.


<?xml version="1.0" encoding="utf-8"?>
    <!-- add required permission -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
        <!-- register the service -->
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
        android:text="Hello World!"
        app:layout_constraintTop_toTopOf="parent" />


Step 4: Creating the popup_window.xml layout file


<?xml version="1.0" encoding="utf-8"?>
            android:text="Displaying over other apps!"
            android:text="Remove" />


Step 5: Working with


package com.raghav.gfgwindowmanager;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import static android.content.Context.WINDOW_SERVICE;
public class Window {
    // declaring required variables
    private Context context;
    private View mView;
    private WindowManager.LayoutParams mParams;
    private WindowManager mWindowManager;
    private LayoutInflater layoutInflater;
    public Window(Context context){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // set the layout parameters of the window
            mParams = new WindowManager.LayoutParams(
                    // Shrink the window to wrap the content rather 
                      // than filling the screen
                    WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
                    // Display it on top of other application windows
                    // Don't let it grab the input focus
                    // Make the underlying application window visible 
                      // through any transparent parts
        // getting a LayoutInflater
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // inflating the view with the custom layout we created
        mView = layoutInflater.inflate(R.layout.popup_window, null);
        // set onClickListener on the remove button, which removes
         // the view from the window
        mView.findViewById( View.OnClickListener() {
            public void onClick(View view) {
        // Define the position of the
          // window within the screen
        mParams.gravity = Gravity.CENTER;
        mWindowManager = (WindowManager)context.getSystemService(WINDOW_SERVICE);
    public void open() {
        try {
            // check if the view is already 
              // inflated or present in the window 
            if(mView.getWindowToken()==null) {
                if(mView.getParent()==null) {
                    mWindowManager.addView(mView, mParams);
        } catch (Exception e) {
    public void close() {
        try {
            // remove the view from the window
            // invalidate the view
            // remove all views
            // the above steps are necessary when you are adding and removing
            // the view simultaneously, it might give some exceptions
        } catch (Exception e) {


Step 6: Creating the ForegroundService class


import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.RequiresApi;
public class ForegroundService extends Service {
    public ForegroundService() {
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    public void onCreate() {
        // create the custom or default notification 
          // based on the android version
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            startForeground(1, new Notification());
          // create an instance of Window class 
          // and display the content on screen
        Window window=new Window(this);;
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
      // for android version >=O we need to create
      // custom notification stating 
      // foreground service is running
    private void startMyOwnForeground()
        String NOTIFICATION_CHANNEL_ID = "example.permanence";
        String channelName = "Background Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_MIN);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setContentTitle("Service running")
                .setContentText("Displaying over other apps")
                // this is important, otherwise the notification will show the way
                // you want i.e. it will show some default notification
        startForeground(2, notification);


Step 7: Working with

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
    // method for starting the service
    public void startService(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // check if the user has already granted
              // the Draw over other apps permission
            if(Settings.canDrawOverlays(this)) {
                // start the service based on the android version
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    startForegroundService(new Intent(this, ForegroundService.class));
                } else {
                    startService(new Intent(this, ForegroundService.class));
            startService(new Intent(this, ForegroundService.class));
    // method to ask user to grant the Overlay permission 
    public void checkOverlayPermission(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                // send user to the device settings
                Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
    // check for permission again when user grants it from 
    // the device settings, and start the service
    protected void onResume() {



Last Updated : 06 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads