1

Android application screen can be prevent from taking screenshot via this code

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
    super.onCreate(savedInstanceState);

}

But it is messy to write this code in every screen. Have any way to declare it in manifest application or any where.

Zahidul
  • 389
  • 5
  • 15
  • 1
    AFAIK the only attribute available is `android:windowSoftInputMode` which will set SoftInput flag . So you need to set WindowManager's Flags at runtime . – ADM Aug 29 '18 at 09:10

1 Answers1

7

Create a BaseActivity for your app and make all the other Activities extend it.

public class BaseActivity extends AppCompatActivity {
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
       super.onCreate(savedInstanceState);

   }
}

Then you create your activities like this:

public class MyActivity extends BaseActivity {
       @Override
       protected void onCreate(@Nullable Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           // Your code
       }
}
Eric B.
  • 4,622
  • 2
  • 18
  • 33