Theory
The bar eating your header
FestConnect Mobile opens with a big welcome banner, but across the top sits the default grey title bar (the action bar) showing the app name, stealing space and clashing with your design. For a splash or a full-screen form, you want it gone.
There are two clean ways to hide it: change the theme (declarative, recommended) or call a line in Java (imperative). Both are examinable, and the Java one has a timing rule that trips people.
Theory
Remove the shop's signboard
The action bar is like a fixed signboard bolted above a shop window: useful for wayfinding, but sometimes you want the whole window for a display. You can either order a shopfront model that comes without the board (choose a NoActionBar theme) or unscrew the board after the fact (hide it in code). Same empty top, two ways to get there.
Follow along
Two ways to hide the title bar
- Theme way (recommended) in res/values/themes.xml use a parent like Theme.MaterialComponents.DayNight.NoActionBar, or set android:theme to a NoActionBar theme on the <activity> in the manifest.
- Code way: hide the action bar in onCreate, after setContentView: if (getSupportActionBar() != null) getSupportActionBar().hide();
- Code way: no-title feature requestWindowFeature(Window.FEATURE_NO_TITLE); this MUST come BEFORE setContentView(...).
- Pick one the theme approach is cleanest for a permanent look; code is handy to toggle at runtime.
Practical
The code way, in order
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// MUST be before setContentView, or it throws
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// alternative: hide the AppCompat action bar (after setContentView)
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
}Think first
Why the ordering rule?
requestWindowFeature(Window.FEATURE_NO_TITLE) must be called before setContentView(). What goes wrong if you call it after, and why does the order matter?
Show the answer
Called after setContentView, it throws an error (the window's features are already locked once the content view is inflated). Window features must be requested before the window builds its decor and content, so the no-title flag has to be set first. The theme approach avoids this timing entirely, which is one reason it is preferred. If you use the code feature call, it is line one of the visual setup, ahead of setContentView.
Quiz
Which is the cleanest, recommended way to permanently remove the title bar from a FestConnect Activity?
- Give it a NoActionBar theme in themes.xml or the manifest
- Call requestWindowFeature after setContentView
- Delete the AndroidManifest.xml
- Remove the TextView from the layout
Show the answer
Give it a NoActionBar theme in themes.xml or the manifest
Using a NoActionBar theme (in themes.xml or via android:theme in the manifest) is the clean, declarative, recommended way to remove the title bar for good (A). Calling requestWindowFeature after setContentView actually crashes (B). Deleting the manifest breaks the whole app (C), and removing the TextView deletes your content, not the bar (D). Theme for a permanent look; code to toggle at runtime.
Watch out
Title-bar traps
1. requestWindowFeature after setContentView: throws. It must come before.
2. getSupportActionBar() is null: if the theme already has no action bar, there is nothing to hide, so guard with a null check before calling hide().
3. Confusing the title bar with the status bar: the action/title bar shows the app name; the status bar shows the clock/battery. Hiding one does not hide the other (that needs separate flags).
Theory
Next: which way up?
With a clean top, the next screen decision is orientation: should the FestConnect form lock to portrait, or rotate to landscape? The next lesson sets screen orientation in the manifest and explains the configuration-change behaviour (why a rotating screen can restart your Activity), another practical widget-era topic.
Summary
Key takeaways
- Hide the title/action bar two ways: a NoActionBar theme (recommended) or Java code.
- Theme: set a *.NoActionBar parent in themes.xml or android:theme on the <activity> in the manifest.
- Code: getSupportActionBar().hide(), or requestWindowFeature(Window.FEATURE_NO_TITLE) which must run BEFORE setContentView.
- getSupportActionBar() can be null (if the theme has no action bar), so null-check before hide().
- The title/action bar is not the status bar; hiding one does not hide the other.
- Memory hook: order a shopfront without the board, or unscrew it later.