Search code examples
javaandroidxmltoolbarandroid-toolbar

Does a TextView inside a Toolbar become immutable?


I tried following a tutorial (https://guides.codepath.com/android/using-the-app-toolbar#custom-title-view) on creating a custom TextView inside a Toolbar. What I want to do is have the ability to dynamically change the text inside the TextView using Java. However, the problem is that I can't and no error messages are being displayed.

Toolbar code in XML:

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:elevation="4dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

                <TextView
                    android:id="@+id/toolbarTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Test1"
                    android:textSize="16sp"
                    android:textColor="@color/textColor" />

        </androidx.appcompat.widget.Toolbar>

Toolbar Java code in the Activity Class:

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        TextView toolbarTitle = toolbar.findViewById(R.id.toolbarTitle);

        toolbarTitle.setText("Test");

Styles.xml:

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorSecondary</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

A simple toolbarTitle.setText() should do the job but for some unknown reasons it doesn't. Even if I remove the android:text from the XML code and use setText() right after the Java code it still doesn't work.

Does this mean that TextViews inside Toolbars are immutable?

Thanks!

Edit 1:

So I managed to find the problem but haven't particularly found a solution. The problem is caused by the Data Binding in the activity_main.xml file. Once the all code related to Data Binding is removed, I'm able to use setText() the way I want. Why is this causing a problem?

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout
    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"
    tools:context=".activity.MainActivity">
    <data>
        <variable
            name="VM"
            type="com.tahmidu.app.view_model.IMainActivityNavViewModel" />
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/toolbarTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/textColor" />

        </androidx.appcompat.widget.Toolbar>

        <FrameLayout
            android:id="@+id/audioMainFragment"
            android:name="com.tahmidu.app.fragment.AudioFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolbar" />


        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorSecondary"
            app:itemIconTint="@color/bottom_navigation_color"
            app:labelVisibilityMode="unlabeled"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/bottom_navigation_main" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Solution

  • TextViews inside toolbars are definitely not immutable. Maybe the problem is where you are calling the textview.setText() method. It should be after after the view is created, so on onCreate() or onViewCreated()