Add Custom View To The Right Of Toolbar


Answer :

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> 

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> 

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> 

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable