Preventing Screenshots
It is possible in some cases to prevent user from taking screenshots of your Android application.
Limitation
This method cannot prevent all kinds of screenshot taking methods. Here's a table summarizing the limitation of this method:
| Screenshot Method | Specific Method | Can be prevented? | 
|---|---|---|
| Hardware button presses | ✅ | |
| With emulator | ⛔️ | |
| With logcat | ✅ | |
| With Android Debug Bridge (adb) | screencap command | ✅ | 
KEYCODE_SYSRQ keyevent | ✅ | |
| Programmatically | ⛔️ | |
| Within instrumented testing | DeviceCapture API | ✅ | 
captureToImage or captureToBitmap API | ⛔️ | 
Basic Usage
The key component to preventing screenshots is WindowManager.LayoutParams.FLAG_SECURE flag. You only need to add this flag to your Activity to prevent users from taking screenshots.
Below is a simple utility file that we can use:
SimpleScreenshotUtil.kt
package com.hanmajid.androidnotebook
import android.view.Window
import android.view.WindowManager
/**
 * Screenshot-related utility object.
 */
object SimpleScreenshotUtil {
    /**
     * Returns true if [window] is currently protected from screenshot.
     */
    fun isScreenshotPreventionEnabled(window: Window): Boolean {
        return (window.attributes.flags and WindowManager.LayoutParams.FLAG_SECURE) != 0
    }
    /**
     * Enable screenshot prevention on [window].
     */
    fun enableScreenshotPrevention(window: Window) {
        window.setFlags(
            WindowManager.LayoutParams.FLAG_SECURE,
            WindowManager.LayoutParams.FLAG_SECURE,
        )
    }
    /**
     * Disable screenshot prevention on [window].
     */
    fun disableScreenshotPrevention(window: Window) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
    }
}
Here's an example of using the utility object in a simple Activity:
MainActivity.kt
package com.hanmajid.androidnotebook
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.hanmajid.androidnotebook.ui.theme.MyApplicationTheme
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // Enable screenshot prevention on this Activity.
        if (!SimpleScreenshotUtil.isScreenshotPreventionEnabled(window)) {
            SimpleScreenshotUtil.enableScreenshotPrevention(window)
        }
        setContent {
            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting("Android")
                }
            }
        }
    }
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        Greeting("Android")
    }
}
You can test this method by trying to take a screenshot of this Activity.