How to prevent Screenshot / Screen Capture in Android JAVA & Kotlin

If you do not want to allow people to take screenshots of some data from your android application like proprietary photos or articles etc, then android provides the secure mechanism using which you can do the same.

When you are developing your application, you will need to modify the onCreate function of your android activity as mentioned below to add “FLAG_SECURE” flag for WindowManager.LayoutParams . As per Android website, FLAG_SECURE treats the content of the window as secure, preventing it from appearing in screenshots or from being viewed on non-secure displays

For Kotlin, modify your MainActivity.kt as,

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

}

For Android JAVA, modify your MainActivity.java as,

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
                           WindowManager.LayoutParams.FLAG_SECURE);
}

Now, once you modify the code, compile and use this application and tries to take screenshots as mentioned in our another post “How to Capture Screenshot in android using adb ?” you should see the system dialog as below,

couldn’t save screenshot, Taking screenshots isn’t allowed by the app or your organisation