2

I know this is a common question to ask but I tried different ways to replace the title of the navigation bar but it didn't work for me. I just want to replace the title of the navbar whenever I use toolbar. Since the default title of my app_name is OldTitle.

enter image description here

What I've been tried but it's not working

android:label="new title" 

I tried also this but it crashes my app

getSupportActionBar().setTitle("New Title");

error :

Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference

I used menu folder and display in Tool Bar layout

my main_menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/app_bar_search"
    android:icon="@drawable/ic_search_black_24dp"
    android:title="Search"
    android:label="My new title"
    app:actionViewClass="android.widget.SearchView"
    app:showAsAction="always" />
</menu>

activitymain.xml

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/mainToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:backgroundTint="@android:color/white"
        app:liftOnScroll="true"/>

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

My Activity class

    Toolbar mToolbars = findViewById(R.id.mainToolbar);
    setSupportActionBar(mToolbars);
Henry
  • 165
  • 13
  • try this `getSupportActionBar().setTitle("New Title");` – Sniffer Sep 27 '21 at 04:41
  • use setTitle() method to set title of your toolbar. – Bhavin Sep 27 '21 at 04:42
  • @Sniffer Thanks for the response but I already Tried that but it crashes my app it says `Attempt to invoke virtual method on a null object reference` – Henry Sep 27 '21 at 04:46
  • 1
    you have to `getSupportActionBar().setTitle("New Title");` add this line after `setSupportActionBar(mToolbars);` else check [this](https://stackoverflow.com/a/58961071/5978440). And Share your Theme Style code and Manifest code – Sniffer Sep 27 '21 at 04:52
  • @Sniffer Yes you're right Thanks it works fine now! I should put that after the toolbars – Henry Sep 27 '21 at 04:54
  • Can i post it in answer and please accept it , it would be helpful for others – Sniffer Sep 27 '21 at 04:55

2 Answers2

2

Try below code

Java:

Toolbar mToolbars = findViewById(R.id.mainToolbar);
setSupportActionBar(mToolbars);
getSupportActionBar().setTitle("New Title");

Kotlin:

  val mToolbars: Toolbar = findViewById(R.id.mainToolbar)
  setSupportActionBar(mToolbars)
  supportActionBar!!.setTitle("New Title")
piet.t
  • 11,718
  • 21
  • 43
  • 52
Sniffer
  • 1,495
  • 2
  • 14
  • 30
-1
getSupportActionBar().setTitle("New Title");

This line will give null error as you have to use setSupportActionBar() first, then you can use getSupportActionBar() for getting the toolbar. As for changing toolbar title on every navigation menu item click, you have to impelement NavigationView.OnNavigationItemSelectedListener like this.

 Toolbar mToolbars = findViewById(R.id.mainToolbar);
 setSupportActionBar(mToolbars);
    
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.nav_dashboard:
                    getSupportActionBar().setTitle("Dashboard");
                    break;
            }

            // to close the drawer layout on every click
            DrawerLayout drawer = findViewById(R.id.drawer_layout); 
            drawer.closeDrawer(GravityCompat.START); 

            return true;
        }
    });
Tarun Yadvendu
  • 230
  • 3
  • 11