Skip to content
DeveloperMemos

Using keepScreenOn in Android

Android, Development, keepScreenOn2 min read

Whether you're building an app that requires continuous screen visibility or want to enhance the user experience by preventing the screen from timing out during certain activities, Android provides a convenient attribute called keepScreenOn. In this article, we'll explore how to utilize keepScreenOn in your Android applications and provide practical examples of its usage.

The keepScreenOn Attribute

The keepScreenOn attribute is a simple yet powerful feature in Android that allows you to keep the screen turned on while a specific activity or view is active. By enabling this attribute, you ensure that the device's screen doesn't turn off automatically due to inactivity, providing a consistent and uninterrupted user experience.

Enabling keepScreenOn

To enable keepScreenOn, you need to access the android.view.View object corresponding to the desired activity or view and call the setKeepScreenOn(true) method. This method can be invoked programmatically in the appropriate lifecycle method or XML layout file.

Here's an example of how to enable keepScreenOn programmatically in a Kotlin-based Android activity:

1import android.app.Activity
2import android.os.Bundle
3import android.view.WindowManager
4
5class MainActivity : Activity() {
6 override fun onCreate(savedInstanceState: Bundle?) {
7 super.onCreate(savedInstanceState)
8 setContentView(R.layout.activity_main)
9
10 // Enable keepScreenOn
11 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
12 }
13}

In the example above, we're using the WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON flag to keep the screen on throughout the lifecycle of the MainActivity. You can place this code in the onCreate() method or any other appropriate lifecycle method depending on your specific use case.

Enabling keepScreenOn via XML Layouts

Alternatively, you can enable keepScreenOn directly from your XML layout files, saving you the need to write additional code. This approach is useful when you want to enable keepScreenOn for a specific view within your layout hierarchy.

To enable keepScreenOn in an XML layout, simply add the android:keepScreenOn="true" attribute to the corresponding view. Here's an example:

1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:keepScreenOn="true">
6
7 <!-- Your view hierarchy -->
8
9</RelativeLayout>

In the example above, we've set android:keepScreenOn="true" for the root RelativeLayout element, which ensures that the screen remains on as long as this view is visible.

Practical Use Cases

Now that you understand how to enable keepScreenOn in Android, let's explore a few practical use cases where this feature can be particularly useful.

Video Playback

When developing a video player application, it's essential to keep the screen on during video playback. By enabling keepScreenOn, you ensure that the screen doesn't time out while the user is immersed in their favorite videos.

Navigation and GPS Apps

Navigation and GPS applications heavily rely on the screen being visible at all times. By utilizing keepScreenOn, you can guarantee that the screen doesn't turn off during navigation, preventing the need for frequent screen taps to keep it active.

Presentations and Slideshows

If you're building an application that showcases presentations or image slideshows, keepScreenOn can enhance the user experience by preventing interruptions due to screen timeouts. Users can focus on the content without worrying about the screen going dark unexpectedly.

Custom Activities

In many custom activities or games, developers often want to ensure uninterrupted screen visibility to maintain a seamless experience. By using keepScreenOn, you can achieve this by keeping the screen on while the user is engaged in the activity.

Conclusion

The keepScreenOn attribute in Android provides a simple yet effective way to prevent the screen from timing out during critical activities. By using the setKeepScreenOn(true) method programmatically or the android:keepScreenOn="true" attribute in XML, you can ensure continuous screen visibility and deliver a better user experience for your Android applications.

Remember to use keepScreenOn thoughtfully and disable it when it's no longer necessary to conserve battery life and provide a more energy-efficient app.