Home > Blockchain >  Top Navigation bar Title replace in Android Studio
Top Navigation bar Title replace in Android Studio

Time:09-27

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);

CodePudding user response:

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")

CodePudding user response:

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;
        }
    });
  • Related