How can I add TextView to Toolbar when I'm using the <include> tag?

15,378

If you want to add a view to your Toolbar then you have to add it inside Toolbar as a child, and not a child of the include.

You already have this piece of code:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbarLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
    <TextView 
        android:id="@+id/toolbarTextView"
        android:layout_width="20dp"
        android:layout_height="match_parent"             
        android:visibility="gone"/>
</android.support.v7.widget.Toolbar>

If you only want to show this TextView on one Activity then set TextView visibility to GONE by default and change it to VISIBLE inside your Activity where you want to show it.

You can get the TextView in any Activity where the above layout is included in the main layout.

TextView toolbarTextView = (TextView) findViewById(R.id.toolbarTextView);
Share:
15,378
Ali Bdeir
Author by

Ali Bdeir

Updated on June 07, 2022

Comments

  • Ali Bdeir
    Ali Bdeir almost 2 years

    So, I have a toolbar.xml file:

    <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"/>
    

    And, I have an <include... tag in my layout:

    <include
            layout="@layout/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:id="@+id/toolbar"/>
    

    So I heard if I want to center the title of the activity on the toolbar, I simply need to add a TextVIew to the toolbar, like this:

    <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbarLL"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">
        <TextView .../>
    </android.support.v7.widget.Toolbar>
    

    But, I'm using the include tag, and the text is apparently not showing up:

    <include
                layout="@layout/toolbar"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:id="@+id/toolbar">
    
            <TextView
                android:text="@string/createNewOccasion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView5"
                android:textSize="18sp"
                android:fontFamily="sans-serif-condensed"
                android:gravity="center"
                />
            </include>
    

    So, what can I do to solve the problem? Here's how it's showing up: How it's showing up

    If you want to see my full real layout, go here. Here's my activity if it is helpful in anyway: http://pastebin.com/g6ZXAxe5

  • Ali Bdeir
    Ali Bdeir almost 8 years
    But my titles will vary, how will I access the textview in my activity if the toolbar is in a seperate toolbar.xml?
  • Ali Bdeir
    Ali Bdeir almost 8 years
    Do I have to do any inflation? Or do I simply say TV = (TextView) findViewById(R.id.toolbar); even though the toolbar is in toolbar.xml?
  • Sharjeel
    Sharjeel almost 8 years
    You can simply get it using findViewById. But if you R.id.toolbar if your TextView id is toolbar in the layout.
  • Ali Bdeir
    Ali Bdeir almost 8 years
    That's the problem. I'm using <include> in my actual layout, not <android.support.v7.widget.Toolbar .
  • Ali Bdeir
    Ali Bdeir almost 8 years
    I don't think my activity could access toolbar.xml without any inflation or things like that, can it/
  • Sharjeel
    Sharjeel almost 8 years
    Please post your complete activity layout and toolbar layout file and Activity code how you are trying to access Toolbar & Textview
  • Ali Bdeir
    Ali Bdeir almost 8 years
    I have mentioned my toolbar layout above, it's the first code block in the question. I'll upload my activity.
  • Sharjeel
    Sharjeel almost 8 years
    There's no view with id toolbar - if you look at your included Toolbar its ID is android:id="@+id/toolbarLL" - you have to get Toolbar using the id you set in your layout file.
  • Ali Bdeir
    Ali Bdeir almost 8 years
    yes, that was a typo, but it doesn't matter since I'm getting the toolbar via its layout by <include android:layout="@layout/toolbar.xml.../>. toolbar.xml has the toolbar, and my actual layout uses include to use it.
  • Ali Bdeir
    Ali Bdeir almost 8 years
    I'll fix my typo, but it doesn't really matter; I'm not calling on the toolbar anywhere, I'm only calling on the include tab, which automatically calls on the toolbar in the toolbar.xml . When I put findViewById(R.id.toolbar) I'm referring to the <include> tag in my actual layout, not the actual toolbar in its toolbar.xml .
  • Sharjeel
    Sharjeel almost 8 years
    You are confusing include with a view. Include itself is nothing. When your code is complied that include tag is replaced with the toolbar.xml layout. So you have access to toolbar directly just as any other main layout view.
  • Ali Bdeir
    Ali Bdeir almost 8 years
    Aah. Now I get it. Thanks.
  • Sharjeel
    Sharjeel almost 8 years
    If it fixed you problem then you can accept the answer :)
  • Ali Bdeir
    Ali Bdeir almost 8 years
    Wait a sec, I still can't access the TextView inside the toolbar. I get a NullPointerException when I try accessing it :|
  • Ali Bdeir
    Ali Bdeir almost 8 years
    You know what? I'm also getting another error. I give up, I'm no longer going to use the <include> tag. Thanks anyway. This could help future visitors, so I'll mark it.
  • Sharjeel
    Sharjeel almost 8 years
    It's really straight forward. If you post your complete clean code in the question then I can take a look at it.