JJJones_3860
JJJones_3860

Reputation: 1542

SupportActionBar: How to change the title?

This Layout came from a Studio 3.5.1 template. I can't figure it out. But my question hopefully has an easy answer.

Here is the XML layout:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ChecklistActivity">

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabAppBarLayout"
            android:theme="@style/AppTheme.AppBarOverlay">

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabLayoutView"
                app:tabMode="scrollable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary" />

        </com.google.android.material.appbar.AppBarLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

In my app Activity (Java), I would like to change the android:label="@string/app_name text (specified in the Activity Manifest) as it appears on the Activity header (appBar, toolBar, whatever it is). I have not been able to do that as of yet.

Screenshot of AppBar with tilte

Upvotes: 0

Views: 2290

Answers (1)

Richard Dapice
Richard Dapice

Reputation: 885

No, you cannot add or remove or alter anything in the manifest as the name says, it's a manifest file, which system reads when it installs your app.

If you are just trying to change the title you can use:

If you are using ActionBar:

getActionBar().setTitle(int titleId)

If you are extending AppCompatActivity:

getSupportActionBar().setTitle(int titleId)

For example:

public class ChecklistActivity extends AppCompatActivity {

    @Override 
    public void onCreate(...) {
        doMyTimerThing();
    }

    public void doMyTimerThing() {
       onTick(long timeLeft) {
           ...(long to string here)...
           getSupportActionBar().setTitle(timeToShowOnActionBar)
       }
    }
}

Set Up the app bar Android Dev Guide.

ActionBar vs SupportActionBar

ActionBar Docs

This is a simple implementation, If you want to have more control you over your ActionBar as your app progresses then you can use Toolbar. Toolbar has to be added to your XML.

Upvotes: 2

Related Questions