In Android development, removing the title bar and status bar is often done to create a fullscreen immersive experience for the user. Here’s how you can hide the title and status bar using both Java and Kotlin.
1. Remove Title Bar
The title bar is typically the app’s name displayed at the top of the activity. To remove it:
- In Java:
You can hide the title bar by callingrequestWindowFeature(Window.FEATURE_NO_TITLE)
in youronCreate()
method beforesetContentView()
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
- In Kotlin:
Similarly, in Kotlin, you can hide the title bar as shown below:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.activity_main)
}
2. Remove Status Bar
The status bar displays system information like the clock and battery level. To hide it, use the following code in your onCreate()
method:
- In Java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
- In Kotlin:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
setContentView(R.layout.activity_main)
}
3. Hiding Both Title and Status Bar
To completely remove both the title and status bar, combine the above steps:
- In Java:
@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.activity_main);
}
- In Kotlin:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
setContentView(R.layout.activity_main)
}
4. Fullscreen Mode with System UI Visibility
For a more immersive experience where the system UI, such as navigation bars, is hidden, you can use View.SYSTEM_UI_FLAG_FULLSCREEN
in Android API 30+:
- In Java:
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
- In Kotlin:
val decorView = window.decorView
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
This method hides the status bar, giving you a more immersive look.
Example: Hiding Title and Status Bar in Android Manifest
You can also hide the title bar directly in the AndroidManifest.xml by adding the following attributes to the activity
tag:
<activity
android:name=".MainActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
</activity>
This will make your activity fullscreen without needing to modify your code.