Android
Activity
Title Change
Android Development
Mobile App Development

How to change title of Activity in Android?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Changing an Android Activity title can be static (defined in manifest/resources) or dynamic (updated at runtime based on screen state). The right method depends on your UI architecture: classic Activity with ActionBar, AppCompat Toolbar, or Jetpack Compose. Developers often update one layer while another still controls the title, which leads to no visible change. Another common issue is setting title too early or in the wrong lifecycle path. This article covers reliable title management across common Android setups and shows how to keep titles consistent during navigation and configuration changes.

Core Sections

1. Set a static title in AndroidManifest.xml

For fixed titles, define it per activity:

xml
<activity
    android:name=".DetailActivity"
    android:label="@string/detail_title" />

This is simple and localizable. It becomes the default title unless overridden at runtime.

2. Change title at runtime in an Activity

In AppCompat activities, call supportActionBar?.title after toolbar setup.

kotlin
1class DetailActivity : AppCompatActivity() {
2    override fun onCreate(savedInstanceState: Bundle?) {
3        super.onCreate(savedInstanceState)
4        setContentView(R.layout.activity_detail)
5
6        setSupportActionBar(findViewById(R.id.toolbar))
7        supportActionBar?.title = "Order #1234"
8    }
9}

If you are not using a custom toolbar, title = "..." also works in many cases.

3. Update title from fragments/navigation

When fragments represent screens, set title when the fragment becomes visible.

kotlin
1override fun onResume() {
2    super.onResume()
3    (requireActivity() as? AppCompatActivity)
4        ?.supportActionBar
5        ?.title = getString(R.string.profile_title)
6}

With Navigation Component, centralize title mapping using destination labels or an OnDestinationChangedListener.

4. Compose-based screens

In Jetpack Compose, top bars are usually composables, so title is state-driven.

kotlin
1@Composable
2fun DetailScreen(orderId: String) {
3    Scaffold(
4        topBar = { TopAppBar(title = { Text("Order #$orderId") }) }
5    ) {
6        // content
7    }
8}

Avoid mixing legacy action bar title updates with Compose top bars for the same screen.

5. Handle configuration and process recreation

If title depends on dynamic data, restore it from ViewModel or saved state rather than recomputing from transient UI state. This keeps title stable across rotation and process death restore flows.

Validation and production readiness

A reliable implementation should include more than a working snippet. Add a small reproducible dataset or input fixture that exercises expected behavior and edge cases, then codify it in automated tests. Include at least one “happy path,” one malformed input case, and one boundary condition so regressions are caught early. Instrument key steps with structured logs or metrics to make failures diagnosable in runtime environments, not just local development. If performance is relevant, keep a lightweight benchmark that can be rerun after refactors to ensure behavior stays within budget.

Operationally, document assumptions near the code: required library versions, environment variables, timezone/locale expectations, and failure handling strategy. For team workflows, add one integration test that mirrors real usage rather than only unit-level checks. This reduces drift between example code and production behavior. Treat these checks as part of feature completion, because most long-term issues are caused by unvalidated assumptions rather than syntax errors.

Common Pitfalls

  • Updating supportActionBar?.title before calling setSupportActionBar.
  • Setting activity title while a fragment or Compose top bar actually controls visible title text.
  • Hardcoding strings in code instead of using string resources for localization.
  • Forgetting to update title on destination changes in multi-fragment navigation.
  • Losing dynamic title after rotation because state is not persisted.

Summary

Android activity titles are simple when ownership is clear. Use manifest labels for static screens, runtime updates for dynamic contexts, and navigation-aware logic for fragment-based apps. In Compose, treat title as UI state in the top app bar. By centralizing title ownership and restoring state correctly, you avoid inconsistent headers and provide a cleaner user experience. This also makes QA verification and localization reviews significantly easier.


Course illustration
Course illustration

All Rights Reserved.