Open In App

How to Prevent Screenshot Or Screen Recorder in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

In some situations, we don’t want to allow to take screenshots or screen recordings of our android application. Here we are going to explain how to prevent Android from taking a screenshot or screen recording when the app goes to the background. Generally, when we take a screenshot, we will see a Screen Capture notification in the notification bar and you can see that screenshot in the Gallery app if you click that notification. And some of the android application we can’t take a screenshot of the visible screen because of the screen secured by the developer. In this situation, we’ll see the message in the notification bar or in the Toast over the screen. Some of the payment transfer apps or course-containing apps have this feature. In this article, we’re going to create a simple project to demonstrate how this feature works. A sample GIF 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 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. We will create a simple TextView inside the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Preventing App from Taking Screenshot"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:textStyle="bold"
        android:textSize="18sp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
</androidx.constraintlayout.widget.ConstraintLayout>


 
 

Method 1

 

In the MainActivity.java file simply add the following code and it will prevent taking Screenshot in Android App.

 

getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,

                                  WindowManager.LayoutParams.FLAG_SECURE);

 

Below is the complete code for the MainActivity.java file.

 

Java




import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.WindowManager;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Adding this line will prevent taking screenshot in your app
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
                             WindowManager.LayoutParams.FLAG_SECURE);
        
    }
}


 
 

Method 2

 

Method 1 is only appropriate for a single activity but what’s the solution for block screenshot for all the activities. We are going to discuss this in method 2. First of all, make a Custom Application class and add a registerActivityLifecycleCallbacks. Then register it in your manifest.

 

Step 1: Here write the given code in the MyApplicationContext.java file

 

Java




import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
 
public class MyApplicationContext extends Application {
    private Context context;
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        setupActivityListener();
    }
 
    private void setupActivityListener() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);            }
            @Override
            public void onActivityStarted(Activity activity) {
            }
            @Override
            public void onActivityResumed(Activity activity) {
 
            }
            @Override
            public void onActivityPaused(Activity activity) {
 
            }
            @Override
            public void onActivityStopped(Activity activity) {
            }
            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }
            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        });
    }
}


 

 

Step 2: Register it in the manifest file like the following

 

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:name=".MyApplicationContext"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Output:

 

Here you can see it clearly it neither allows taking screenshots nor screen recordings as when we try to take screen recordings it turns black.

 

 



Last Updated : 23 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads