Skip to content Skip to sidebar Skip to footer

Change Custom Toolbar Text

Unable to get the text on my toolbar to change. I have done a tone of searching but I am seeing no results. I have tried so many combinations of things but maybe something will pop

Solution 1:

You forgot to set this:

setSupportActionBar(toolbar);

and this:

getSupportActionBar().setDisplayShowTitleEnabled(false); 

YOUR ACTIVITY LIKE THIS:

Toolbartoolbar= (Toolbar) findViewById(R.id.toolbarCustom);

setSupportActionBar(toolbar);

TextViewtextView= (TextView)toolbar.findViewById(R.id.toolbarTextView);
textView.setText("String");

getSupportActionBar().setDisplayShowTitleEnabled(false);

Solution 2:

It will be like that. Java Code:

Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar); //here toolbar is your id in xml
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("String"); //string is custom name you want

Solution 3:

Here is how you change custom toolbar text in Android Kotlin:

First go to the Manifest file and i your activity add the line android:theme="@style/AppTheme.NoActionBar" in your activity tag

<activityandroid:name=".MainActivity"android:label="@string/app_name"android:theme="@style/AppTheme.NoActionBar"></activity>

Second go your Activity and add the following:

overridefunonCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        toolbar.title = "My New Title"
}

Solution 4:

I never get it, Why anyone doesn't simply write all toolbar custom text inside the xml? why bother writing code in .java file? e.g.

<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            <TextView
                android:id="@+id/tv_toolbar_custom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#fff"
                android:textSize="@dimen/_22ssp"
                android:textStyle="bold"
                android:text="Profile"/>
        </androidx.appcompat.widget.Toolbar>

Post a Comment for "Change Custom Toolbar Text"