How can I add TextView to Toolbar when I'm using the <include> tag?
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);
Ali Bdeir
Updated on June 07, 2022Comments
-
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 aTextVIew
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:
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 almost 8 yearsBut my titles will vary, how will I access the textview in my activity if the toolbar is in a seperate
toolbar.xml
? -
Ali Bdeir almost 8 yearsDo I have to do any inflation? Or do I simply say
TV = (TextView) findViewById(R.id.toolbar);
even though the toolbar is intoolbar.xml
? -
Sharjeel almost 8 yearsYou can simply get it using findViewById. But if you
R.id.toolbar
if your TextView id is toolbar in the layout. -
Ali Bdeir almost 8 yearsThat's the problem. I'm using
<include>
in my actual layout, not<android.support.v7.widget.Toolbar
. -
Ali Bdeir almost 8 yearsI don't think my activity could access
toolbar.xml
without any inflation or things like that, can it/ -
Sharjeel almost 8 yearsPlease post your complete activity layout and toolbar layout file and Activity code how you are trying to access Toolbar & Textview
-
Ali Bdeir almost 8 yearsI have mentioned my toolbar layout above, it's the first code block in the question. I'll upload my activity.
-
Sharjeel almost 8 yearsThere's no view with id
toolbar
- if you look at your includedToolbar
its ID isandroid:id="@+id/toolbarLL"
- you have to get Toolbar using the id you set in your layout file. -
Ali Bdeir almost 8 yearsyes, 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 usesinclude
to use it. -
Ali Bdeir almost 8 yearsI'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 almost 8 yearsYou 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 almost 8 yearsAah. Now I get it. Thanks.
-
Sharjeel almost 8 yearsIf it fixed you problem then you can accept the answer :)
-
Ali Bdeir almost 8 yearsWait a sec, I still can't access the TextView inside the toolbar. I get a NullPointerException when I try accessing it :|
-
Ali Bdeir almost 8 yearsYou 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 almost 8 yearsIt's really straight forward. If you post your complete clean code in the question then I can take a look at it.