ANDROID FULL SCREEN MODE PROGRAMMATICALLY
To make an Android activity go into full-screen mode programmatically, you need to hide the status bar, navigation bar, and sometimes even the action bar, depending on your requirements. Android provides several ways to achieve this effect, and it’s often used in applications like games or video players, where you want to immerse the user in the content without distractions.
Here's a step-by-step guide on how to enable full-screen mode in your Android app programmatically:
1. Hiding the Status Bar and Navigation Bar
The most basic method to achieve full-screen mode is by hiding the status bar (the bar at the top with the time and notifications) and the navigation bar (the bar at the bottom with the home, back, and recent buttons).
Programmatically Hiding Status and Navigation Bars
In your activity's onCreate() method, use the following code to enable full-screen mode:
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class FullScreenActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);
// Hides both the status bar and the navigation bar
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN | // Hide status bar
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | // Hide navigation bar
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY // Stick to full-screen until user interacts
);
}
}
Explanation of the Flags:
SYSTEM_UI_FLAG_FULLSCREEN: Hides the status bar.SYSTEM_UI_FLAG_HIDE_NAVIGATION: Hides the navigation bar (back, home, and recent buttons).SYSTEM_UI_FLAG_IMMERSIVE_STICKY: Keeps the system bars hidden until the user interacts with the screen. This allows users to swipe from the edges to temporarily reveal the system UI, but it will hide again automatically after a few seconds.
This method works on Android 4.4 (API level 19) and above.
2. Hiding the ActionBar (if Present)
If your activity has an ActionBar (the top bar with the title and navigation buttons), you may want to hide it to maximize the screen area for content.
To hide the ActionBar, you can use the following code inside onCreate():
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class FullScreenActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);
// Hide ActionBar (if present)
getSupportActionBar().hide();
// Hides the system bars
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
}
3. Handling Orientation Changes
When the device orientation changes (from portrait to landscape or vice versa), Android may reset your UI and remove full-screen mode. To ensure that full-screen mode persists after orientation changes, you can override the onResume() method to reapply full-screen settings.
Ensure Full-Screen Mode on Orientation Change:
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class FullScreenActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);
// Initially set to full-screen
setFullScreen();
}
@Override
protected void onResume() {
super.onResume();
// Re-enable full-screen mode when the activity resumes
setFullScreen();
}
private void setFullScreen() {
// Hide status bar, navigation bar, and action bar
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
// Optionally hide the action bar
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
}
}
By applying the full-screen configuration every time the activity is resumed, you ensure that the full-screen mode stays active even when the orientation changes.
4. Handling Full-Screen Mode with No Action Bar in the Manifest
If you want the activity to always be in full-screen mode, you can declare it in the AndroidManifest.xml file with a custom theme that hides the title bar and the status bar.
Steps:
- Define a Custom Theme:
In
res/values/styles.xml, define a theme that removes the title bar and sets the activity to full-screen:
<resources>
<style name="AppTheme.FullScreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
- Apply the Theme in the
AndroidManifest.xml: InAndroidManifest.xml, apply the custom theme to the activity:
<activity
android:name=".FullScreenActivity"
android:theme="@style/AppTheme.FullScreen">
</activity>
This ensures that the activity will always start in full-screen mode, without needing to modify the code every time.
5. Using Android's Immersive Mode for a Better Full-Screen Experience
Immersive Mode is a more advanced form of full-screen mode that hides both the status bar and the navigation bar, and allows users to swipe from the edges of the screen to temporarily reveal the system bars.
Here’s how you can set immersive mode in Android:
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class FullScreenActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);
// Set immersive full-screen mode
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN | // Hide status bar
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | // Hide navigation bar
View.SYSTEM_UI_FLAG_IMMERSIVE | // Enable immersive mode
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY // Sticky immersive mode
);
}
}
6. Full-Screen Mode with Video or Game Apps
For video or game apps, it’s common to use full-screen mode to provide a more immersive experience. This can be achieved with the combination of the methods shown above, ensuring the entire screen is available for the media or gameplay.
For example, a video player app might hide the system UI during video playback, only revealing the system UI when the user taps the screen.
Conclusion
Making an activity go into full-screen mode in Android can enhance user experience by removing distractions such as the status bar, navigation bar, and action bar. Whether you are building a game, video player, or any other immersive app, using the appropriate flags and settings can help you achieve a clean, distraction-free interface.
- Use
SYSTEM_UI_FLAG_FULLSCREENto hide the status bar. - Use
SYSTEM_UI_FLAG_HIDE_NAVIGATIONto hide the navigation bar. - Use
SYSTEM_UI_FLAG_IMMERSIVE_STICKYfor an immersive mode that re-hides the system UI after user interaction. - Use a custom theme or
getSupportActionBar().hide()to hide the action bar for a complete full-screen experience.
Always make sure to handle orientation changes and ensure that full-screen mode remains active throughout the app’s lifecycle.

0 Comments