Add custom view to the right of toolbar
Asked Answered
T

6

31

I am trying to achieve this(A timer in toolbar with red background):

A timer in the toolbar along with red background

I am trying to add customView in toolbar. It always end's up being on extreme left just beside the back arrow. Like the text YP in image below is a custom Textview added to toolbar.enter image description here.

The code for toolbar:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/base_activity_toolbar"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:minHeight="?attr/actionBarSize"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

The code for adding the layout:

    LayoutInflater mInflater= LayoutInflater.from(context);
    View mCustomView = mInflater.inflate(R.layout.textview, null);
    toolbar.addView(mCustomView);

This is the layout I am adding right now for testing:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="wrap_content"
              android:gravity="end"
              android:layout_height="wrap_content">
    <TextView android:layout_width="wrap_content"
              android:text="yp"
              android:layout_height="wrap_content"/>
</LinearLayout>

I am missing something majorly, could not figure out. Pulling my hairs out.

Tolmann answered 25/11, 2015 at 6:13 Comment(1)
Change the LinearLayout's layout_width to match_parent.Maidenhair
J
24

You can add ViewGroups inside Toolbar

          <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/toolbar_height"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <RelativeLayout
             ....
            </RelativeLayout>

           </android.support.v7.widget.Toolbar>
Jacal answered 25/11, 2015 at 6:25 Comment(3)
When I do something like this, though, I see the TextView I've added but it no longer shows the Toolbar title. Any idea why?Sherwoodsherwynd
Have you specified getSupportActionBar().setDisplayShowTitleEnabled(false);? or might be the TextView overlaid the toolbar title.Jacal
Yes because the RelativeLayout gets added to the left of the Toolbar title. Specially if you add alignParentRight="true", it pushes the title out of view. I have added another answer to address this issue, a comment with all that code would be too long.Billman
A
30

I just set layout_gravity to the rightand it worked

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="?android:actionBarSize"
    app:title="NEW REQUESTS"
  >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="blahblah"
      android:layout_gravity="right"  
      />
  </android.support.v7.widget.Toolbar>
Arte answered 14/6, 2017 at 14:0 Comment(2)
I can't believe it, that actually worked! A.S. doesn't give layout_gravity as an option for TextView inside a Toolbar, so I hadn't tried it.Horseflesh
@Anarchofascist, yes you are right it doesn't give layout_gravity option inside Toolbar, but if you will put it by yourself, it would be accepted.Valonia
J
24

You can add ViewGroups inside Toolbar

          <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/toolbar_height"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <RelativeLayout
             ....
            </RelativeLayout>

           </android.support.v7.widget.Toolbar>
Jacal answered 25/11, 2015 at 6:25 Comment(3)
When I do something like this, though, I see the TextView I've added but it no longer shows the Toolbar title. Any idea why?Sherwoodsherwynd
Have you specified getSupportActionBar().setDisplayShowTitleEnabled(false);? or might be the TextView overlaid the toolbar title.Jacal
Yes because the RelativeLayout gets added to the left of the Toolbar title. Specially if you add alignParentRight="true", it pushes the title out of view. I have added another answer to address this issue, a comment with all that code would be too long.Billman
B
10

Given answers work but if you want to set a title to the toolbar (most likely you will), then adding a RelativeLayout pushes the title out of view to the right. In this case you need to disable the toolbar title by setting getSupportActionBar().setDisplayShowTitleEnabled(false) and add a custom TextView next to the other view you wish to add to Toolbar. So as an example you would add a ImageView this way:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/base_activity_toolbar"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:minHeight="?attr/actionBarSize"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/custom_toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            style="@style/ActionBarTitle"/>

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="centerCrop"
            android:adjustViewBounds="true"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/custom_toolbar_title"
            android:src="@drawable/arrow"/>

    </RelativeLayout>

</android.support.v7.widget.Toolbar>
Billman answered 1/8, 2016 at 2:9 Comment(1)
works well, for those who need style="@style/ActionBarTitle" there is style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"Mu
L
8

Just had the same issue but I found a simpler (in my humble opinion) solution.

You can slightly change your code from :

LayoutInflater mInflater= LayoutInflater.from(context);
View mCustomView = mInflater.inflate(R.layout.textview, null);
toolbar.addView(mCustomView);

To :

LayoutInflater mInflater= LayoutInflater.from(context);
View mCustomView = mInflater.inflate(R.layout.textview, null);
toolbar.addView(mCustomView, new Toolbar.LayoutParams(Gravity.RIGHT));

The view is then added with gravity set to the right !

Laccolith answered 21/2, 2017 at 15:30 Comment(3)
Nice one this was the perfect solution if you are building views programmatically. Thank youExurb
Perfect solution ! Works like a charmWhitman
Worked perfectly. I had to change the parent layout of R.layout.textview to FrameLayoutStretto
B
0

give it a try. change R.layout.textview to

 <RelativeLayout android:layout_height="match_parent"
                android:layout_width="match_parent"
                >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TV"
                    android:id="@+id/textView2"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>
Barthelemy answered 25/11, 2015 at 6:22 Comment(0)
J
0

I'd simply go for

<androidx.appcompat.widget.Toolbar
 ...>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="text aligned right" />

</androidx.appcompat.widget.Toolbar>
Jarietta answered 4/5, 2020 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.