Achieving Full-Screen Mode in Android Applications

In modern mobile application development, particularly for Android, creating an immersive user experience is key. One way to enhance this experience is by allowing your app’s activities to run full-screen, removing distractions like the status bar and navigation buttons. This tutorial explores various methods to achieve a full-screen mode in Android applications.

Introduction

The concept of full-screen mode involves hiding system UI components such as the status bar, navigation bar, or action bar within an activity. Achieving this requires different approaches depending on your app’s requirements and the version of Android you are targeting. This guide will cover:

  1. Basic Full-Screen Setup
  2. Immersive Mode for Deeper Integration
  3. Advanced Techniques with AndroidX

Basic Full-Screen Setup

Using AndroidManifest.xml

You can set an activity to full-screen by specifying a theme in the AndroidManifest.xml file.

<activity android:name=".ActivityName"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
</activity>

This method is straightforward and works well for simple applications. However, it might not offer the flexibility needed for more complex UIs.

Programmatically Setting Full-Screen

For greater control within your activity’s lifecycle, you can programmatically set full-screen mode:

public class ActivityName extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
        setContentView(R.layout.main);
    }
}

This approach allows you to adjust full-screen settings at runtime, providing dynamic control over the UI.

Custom Themes

If your application uses custom themes and still requires full-screen behavior, you can define a style with specific window attributes:

<style name="MyTheme" parent="AppTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
</style>

<activity android:name=".MyActivity"
    android:theme="@style/MyTheme">
</activity>

This method offers flexibility, allowing the use of your app’s branding while maintaining full-screen behavior.

Immersive Mode for Deeper Integration

Introduced in Android KitKat (API level 19), immersive mode allows applications to hide both the status bar and navigation bar. This is ideal for media apps where an unobstructed view is crucial.

Enabling Immersive Mode

public class MyActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Hide system UI
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 
                      | View.SYSTEM_UI_FLAG_FULLSCREEN 
                      | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(uiOptions);
    }
}

Handling UI Visibility Changes

To maintain immersive mode, you should reapply the UI visibility flags when they are altered:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 
                      | View.SYSTEM_UI_FLAG_FULLSCREEN 
                      | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(uiOptions);
    }
}

Advanced Techniques with AndroidX

For apps targeting newer versions of Android, especially from API level 30 and above, using the WindowInsetsController is recommended.

Using WindowInsetsController

The WindowInsetsController provides a modern way to handle UI visibility:

val windowInsetsController = 
    WindowCompat.getInsetsController(window, window.decorView)

windowInsetsController?.let {
    it.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    it.hide(WindowInsetsCompat.Type.statusBars())
}

This method is part of the AndroidX library and offers more granular control over system bars.

Hiding Both Status Bar and Navigation Bar

To hide both, use:

windowInsetsController?.hide(WindowInsetsCompat.Type.systemBars())

Conclusion

Full-screen mode in Android can significantly enhance user experience by providing a distraction-free environment. Whether you choose to implement it through themes, programmatically, or using advanced techniques with AndroidX, understanding these methods allows for flexibility and control over your application’s UI.

Remember to test across different devices and Android versions to ensure consistent behavior, as some manufacturers may customize their OS implementations that can affect standard behaviors.

Leave a Reply

Your email address will not be published. Required fields are marked *