Issue
In my Android app I need to use the SECURE_FLAG to say to Android: "hey you don't take a screenshoot of my app in background!". Ok it works simply using the following lines of code in my activities:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
...
}
What I obtained is explained in the following screenshot:
Now.. how can I:
- change the icon on the top of the activity preview?
- change the preview of the activity, with another colour or an image?
I look for a solution, but I didn't find anything.. except that it is impossible to do it (if true I think is a pity, for Android). Tnx in advance.
Solution
You can use two layouts.
In your xml code of the layout, make a separate layout and cut & paste the entire ui code in it and give it the id
mainLayout
. Make another layout which will be used for the securing UI. Give this layout the idprotectLayout
and there you can customize what you want like image background and etc...Then follow this in the activity you want to be secure.
LinearLayout mainLayout,secureLayout; @Override protected void onPause() { getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); mainLayout.setVisibility(View.GONE); secureLayout.setVisibility(View.VISIBLE); super.onPause(); } @Override protected void onStart() { getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,WindowManager.LayoutParams.FLAG_SECURE); mainLayout.setVisibility(View.VISIBLE); secureLayout.setVisibility(View.GONE); super.onResume(); }
NOTE: This will best work on Android versions 12 and above as in others the display at the recent does not update immediately.
Answered By - Sambhav Khandelwal
Answer Checked By - Willingham (JavaFixing Volunteer)