Skip to content
Related Articles

Related Articles

Improve Article

How to Prevent Screenshot Or Screen Recorder in Android?

  • Difficulty Level : Hard
  • Last Updated : 24 Feb, 2021

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 version="1.0" encoding="utf-8"?>
        android:text="Preventing App from Taking Screeenshot"
        app:layout_constraintTop_toTopOf="parent" />

Method 1

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



Below is the complete code for the file.


import android.os.Bundle;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        // Adding this line will prevent taking screenshot in your app

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 file


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

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

        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :