Change Custom Toolbar Text
Asked Answered
V

4

12

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 out. Here is the code for the last thing I have tried for my activity.

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

Here is my XML Code

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_item_action"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.carson.tryeverything.ItemActionActivity"
android:orientation="vertical"

   >
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbarCustom"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:background="?attr/colorPrimary"
    >

    <TextView
        android:id="@+id/toolbarTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="1"
        android:text="Line one"
          android:textAppearance="@android:style/TextAppearance.WindowTitle"
        android:visibility="visible" />
</android.support.v7.widget.Toolbar>


 <FrameLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"

    >


    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="6dp"
        android:layout_marginTop="0dp"
        />

  </FrameLayout>

  </LinearLayout>
  android:textAppearance="@android:style/TextAppearance.WindowTitle"
        android:visibility="visible" />
 </android.support.v7.widget.Toolbar>

Lastly here is my Manifest File. Probably needs a little cleaning

 <application
    android:name="android.support.multidex.MultiDexApplication"
    android:allowBackup="true"
    android:icon="@mipmap/icon2"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar"/>
    <activity
        android:name=".SplashScreen"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
       >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
            <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="no spoilers" />
        </intent-filter>
    </activity>
    <activity android:name=".ItemActionActivity"
        android:theme="@style/AppTheme.NoActionBar"
        />


    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps"/>
</application>

I have three activities, the Splash, the main screen which has 2 dozen listview options and then the final activity screen where I want the toolbar to show a custom title depending on which you click on.

Vladamir answered 26/4, 2017 at 20:14 Comment(4)
Are you using supportActionBar()?Free
Please post all the code that involves Toolbar.Free
We need the code of your third activity (the one that has toolbar) too.Fluff
Sorry thought it was extra unnecessary information, added my third activity which my toolbar coded is included into, I have tried various code involving supportActionBar as well and have had no results. Though if there is a specific code you want me to try let me know and I'll let you know what happens.Vladamir
O
14

You forgot to set this:

 setSupportActionBar(toolbar);

and this:

getSupportActionBar().setDisplayShowTitleEnabled(false); 

YOUR ACTIVITY LIKE THIS:

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

setSupportActionBar(toolbar);

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

getSupportActionBar().setDisplayShowTitleEnabled(false);
Oscillation answered 12/5, 2017 at 11:27 Comment(1)
Thats exactly what I need!Jaconet
H
9

It will be like that. Java Code:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //here toolbar is your id in xml
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("String"); //string is custom name you want
Handcrafted answered 21/9, 2017 at 17:4 Comment(0)
F
1

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>
Furculum answered 21/5, 2020 at 8:37 Comment(1)
Often in my projects, the title is dependent on the data passed in to the activity.Vladamir
G
0

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

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

Second go your Activity and add the following:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        toolbar.title = "My New Title"
}
Glance answered 29/3, 2019 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.