Implementing Fullscreen Activities in Android: A Comprehensive Guide

Nov 05, 2025 · Programming · 17 views · 7.8

Keywords: Android | Fullscreen | Activity | ImmersiveMode | SystemBars | WindowInsetsController

Abstract: This article provides a detailed guide on implementing fullscreen activities in Android, covering programmatic methods, theme configurations, immersive mode with WindowInsetsControllerCompat, and handling keyboard interactions. Code examples in Java and Kotlin are included for practical implementation.

Fullscreen activities in Android are essential for applications that require immersive user experiences, such as games, video players, and image galleries. This guide systematically explores various methods to achieve fullscreen mode, including programmatic setup, theme-based configurations, and dynamic immersive mode, with integrated code examples for step-by-step explanation.

Programmatic Fullscreen Setup

The programmatic approach involves setting window properties within the Activity's onCreate method to enable fullscreen. This includes removing the title bar and using flags to hide the status bar. The following code, rewritten based on core concepts, demonstrates implementation in Java.

public class FullscreenActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Remove the title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // Set flags to enable fullscreen mode and hide the status bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_fullscreen);
    }
}

This method is straightforward but may not handle dynamic changes, such as when the keyboard appears and system bars reappear.

Fullscreen via Theme in AndroidManifest

The declarative method achieves fullscreen by defining themes in the AndroidManifest.xml file, suitable for standard Activities. Using built-in themes allows quick application of fullscreen effects.

<activity android:name=".FullscreenActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>

For AppCompatActivity, a custom theme is required to ensure compatibility. Below is an example of defining and using a custom theme.

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>
<activity android:name=".FullscreenActivity"
          android:label="@string/app_name"
          android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"/>

This approach is easy to maintain but lacks fine-grained control over system bar behavior.

Immersive Mode with WindowInsetsControllerCompat

Immersive mode provides dynamic control over hiding and showing system bars, ideal for applications that require user interaction handling. Android recommends using the WindowInsetsControllerCompat API, which supports various behavior modes. Here is a Kotlin implementation example.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    
    val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
    // Configure system bar behavior: show transient bars on swipe gestures
    windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    
    ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { view, windowInsets ->
        // Check if system bars are visible
        if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars()) || 
            windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) {
            // When system bars are visible, set button click to hide them
            binding.toggleFullscreenButton.setOnClickListener {
                windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
            }
        } else {
            // When system bars are hidden, set button click to show them
            binding.toggleFullscreenButton.setOnClickListener {
                windowInsetsController.show(WindowInsetsCompat.Type.systemBars())
            }
        }
        ViewCompat.onApplyWindowInsets(view, windowInsets)
    }
}

The Java version code is provided below, with identical functionality.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    WindowInsetsControllerCompat windowInsetsController = WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
    windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    
    ViewCompat.setOnApplyWindowInsetsListener(getWindow().getDecorView(), (view, windowInsets) -> {
        if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars()) || 
            windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) {
            binding.toggleFullscreenButton.setOnClickListener(v -> {
                windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
            });
        } else {
            binding.toggleFullscreenButton.setOnClickListener(v -> {
                windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
            });
        }
        return ViewCompat.onApplyWindowInsets(view, windowInsets);
    });
}

This method allows users to temporarily access system bars via gestures, enhancing the interactive experience.

Handling Keyboard and System Bar Interactions

In fullscreen mode, keyboard appearance can cause system bars to reappear, disrupting immersion. By monitoring layout changes, system bars can be re-hidden when the keyboard is shown. Below is a Java code example.

public void hideSystemUI() {
    getWindow().getDecorView().setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_FULLSCREEN
        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    hideSystemUI();
    
    final View activityRootView = findViewById(R.id.activityRoot);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            // Get the visible display frame
            activityRootView.getWindowVisibleDisplayFrame(r);
            int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
            // If height difference is more than 100 pixels, likely the keyboard is visible, re-hide system bars
            if (heightDiff > 100) {
                hideSystemUI();
            }
        }
    });
}

This approach ensures that system bars do not interfere with the user experience in fullscreen mode, even when the keyboard is active.

In summary, selecting the right fullscreen implementation depends on the application context: programmatic and theme methods are suitable for static fullscreen, while immersive mode offers dynamic control. Testing on actual devices is recommended to ensure compatibility.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.