I was developing a quiz app for a customer. And the requirement was to have a custom header / title for the application that’s horizontally center-aligned on the display. This article shows how to set a custom window title using theme styles, a custom layout, and activity code.
Prerequisites
- Basic Android project knowledge.
- Android project — example project named "Quiz" in this article.
- Files we will touch or create:
- res/values/styles.xml
- res/values/colors.xml
- res/values/themes.xml
- res/layout/header.xml (custom title layout)
- AndroidManifest.xml
- MainActivity.java (or the activity where you want the custom title)
1) Create / update styles.xml
Add the style(s) you want to use for your custom title to res/values/styles.xml. (The original article showed the styles as an image — keep that image below as a visual reference.)
(If styles.xml is missing in your project, create res/values/styles.xml and add the relevant style tags.)
2) Add colors (colors.xml)
Create res/values/colors.xml and add the color resources you will use in the title / theme. The original colors.xml content was provided as an image (kept below):
3) Add / update themes.xml
Add the theme that references your custom title style or window features in res/values/themes.xml. The article’s example theme content is available as an image here:
Make sure the style you created is referenced from the final theme applied to your activity or application.
4) Apply the theme in AndroidManifest.xml
Apply the theme to the whole application or to specific activities in AndroidManifest.xml. The manifest snippet from the original article is included below as an image for reference:
5) Create the custom title layout (header.xml)
Create a layout resource (for example res/layout/header.xml) that contains the controls you want in the title bar. You can include text, images, logos, or other controls. The original article’s example layout (only text in this case) is shown below as an image:
(Another preview of the header layout in the original article:)
6) Add the code in your Activity's onCreate()
In your activity's onCreate() (e.g., MainActivity.java), add the code required to enable and set the custom window title. The exact code used in the original article was provided as an image — kept below:
The article’s final run screenshot (customized title bar on top) is below:
Troubleshooting
If you forget any of the steps above you may see a runtime exception like this in logcat:
Caused by: android.util.AndroidRuntimeException:
You cannot combine custom titles with other title features
To fix this, add the following in your theme (themes.xml) to disable default title features so the custom title can be used:
<item name="android:windowNoTitle">true</item>
Alternatively, remove the activity-level theme that includes a title bar (for that particular activity) from AndroidManifest.xml. The original article lists this example manifest attribute used in some cases:
android:theme="@android:style/Theme.Black.NoTitleBar"
Notes and recommendations
- Keep the images above as visual references, but add textual code blocks (as I’ve suggested in the "Troubleshooting" section) for each code example so readers can copy-paste. In the original post many code examples were only images — that should be corrected.
- For new projects consider using the AppCompat Toolbar (androidx.appcompat.widget.Toolbar) as the recommended approach. The custom window title method is useful for legacy projects or specific use cases.
That’s it. Happy Coding…
No comments:
Post a Comment