Widescreen adaptation, disabling screenshots, and keeping the screen on are three small Android developers’ knowledge of screens that may come in handy at some point.

Widescreen adaptation

More and more phone manufacturers are moving toward full-screen designs, such as Samsung’s 18.5:9 Galaxy S8 and the OnePlus 5T, which was released just two days ago with an 18:9 screen ratio, both exceeding the 16:9 ratio common in the past. The design of a large screen ratio has a screen fit problem for Android apps.

We need to define a larger android.max_aspect attribute in the Manifest file to make sure our App takes full advantage of the extra screen space.

<meta-data 
	android:name="android.max_aspect"
	android:value="2.1"/>
Copy the code

The max_aspect attribute represents the maximum screen ratio that the App can support, and we are officially advised to set this value to 2.1 or higher.

* * note: * * if you don’t have to use meta – data label set max_aspect properties, while android: resizeableActivity attribute value is true, The maximum aspect used by App defaults to 1.8 (about 16:9).

Note: The resizeableActivity attribute is false for applications with targetSDKVersion less than 24, and true for others.

The following figure compares the display effect of apps with different aspect values on devices with high screen ratio:

The 16:9 aspect setting on the left and the 18.5:9 setting on the right also use a larger 18.5:9 screen ratio device. Obviously, when the App with a low ratio value is opened, there is white space on both sides of the screen, which means that the screen space cannot be fully utilized.

Ban screenshots

For the interface with high security requirements, such as alipay payment QR code, the application will generally choose to shield the device screen capture function. When you press a combination of shortcut keys (usually the start key and volume up key), the system automatically prompts related information, such as a screenshot failure.

Disabling screenshots isn’t too difficult to implement, just add a line of code to the onCreate() method of the Activity lifecycle that needs to be set to disable screenshots:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
Copy the code

Normally on the screen

We usually set our phones to automatically adjust the brightness of the screen or automatically lock the screen after a fixed time. However, some apps or some pages need to keep the screen always on, such as the real-time stock trend page.

It’s also easy to implement by adding code to the onCreate() lifecycle method of the desired Activity, like disabling screenshots:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Copy the code

Supplementary tips:

Code that relies on lifecycle methods, such as disabling screenshots and keeping the screen on, can be placed in the BaseActivity base class if you need to apply it to multiple activities to avoid writing repetitive code. Or you can use this amazing Activity lifecycle interface:

ActivityLifecycleCallbacks

Add this to the custom Application class onCreate() method:

registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
  @Override
  public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  }

  // Omit other related Activity lifecycle functions here. });Copy the code

About me: Yifeng, blog address: Yifeng. Studio /, Sina Weibo: IT Yifeng

Scan the QR code on wechat, welcome to follow my personal public account: Android Notexia

Not only share my original technical articles, but also the programmer’s workplace reverie

Easter egg: public number reply keyword “interview information”, obtain BAT interview bull for you to prepare a full set of interview information!