Search code examples
androidtextviewandroid-toolbar

How to place two text views in Tool bar ?


I want to show two text view widgets in the Tool bar. Where one text view acts as title and other one acts as sub title which should be place extreme right of the tool bar widget.

Here is the xml code that I have done for showing the two text views one at the left and the other one at the right.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/tool_bar_color"
    app:layout_scrollFlags="scroll|enterAlways"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:titleTextAppearance="@style/AppTheme.Toolbar.Title">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="start|center"
        android:textSize="20sp"
        android:textColor="@color/white"
        android:layout_gravity="center"
        android:textStyle="bold"
        android:text="Hello"
        android:singleLine="true"
        android:id="@+id/toolbar_title" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="end|center"
        android:textSize="15sp"
        android:textColor="@color/white"
        android:layout_gravity="center"
        android:textStyle="bold"
        android:text="India Hello"
        android:singleLine="true"
        android:id="@+id/toolbar_right_subtitle" />
</android.support.v7.widget.Toolbar>

I didn't get the desired output after placing two text view widgets inside the tool bar. How can I accomplish the desired output of showing two text view widgets, one at left and other one at right?


Solution

  • This is the code I eventually used, adapted from the example given by Gunaseelan:

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/tool_bar_color"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextAppearance="@style/AppTheme.Toolbar.Title">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="start|center"
            android:textSize="20sp"
            android:textColor="@color/white"
            android:layout_gravity="center"
            android:textStyle="bold"
            android:layout_weight="1"
            android:singleLine="true"
            android:id="@+id/toolbar_title" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="end|center"
            android:textSize="12sp"
            android:layout_weight="1"
            android:textColor="@color/white"
            android:layout_gravity="center"
            android:visibility="gone"
            android:layout_marginRight="10dp"
            android:singleLine="true"
            android:id="@+id/toolbar_right_subtitle" />
    
        </LinearLayout>
    </android.support.v7.widget.Toolbar>