Allay Khalil
Allay Khalil

Reputation: 693

How to change the toolbar name and put icon with tool bar

I have just made the app using android studio template of Navigation view. Its all working fine but I have the following two requirements

  1. I want to center the toolbar title.
  2. I want to put the image beside the title.

Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.abdulsalam.lahoreqalander.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<!--<android.support.design.widget.FloatingActionButton-->
    <!--android:id="@+id/fab"-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_gravity="bottom|end"-->
    <!--android:layout_margin="@dimen/fab_margin"-->
    <!--android:src="@android:drawable/ic_dialog_email" />-->

Problem :

I do not know where the title string is saved, I have checked the string.xml and there is the app name and that is exactly showing on the toolbar title.

So Any suggestion where can I change the title string without changing the app name , In fact I want to change it custom and also how to put the image beside it.

Note I am using the template made by Android studio

.

Upvotes: 5

Views: 9823

Answers (3)

Jason Saruulo
Jason Saruulo

Reputation: 1897

First of all from the documentation:

The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

For setting an icon through xml you could use android:navigationIcon inside your Toolbar. For setting the text I would also go with the Java code approach as I could not find anything with xml in the documentation. The text would not be centered anyway.

If you want to center the text you have to modify your Toolbar. Toolbar is a view and you can put anything inside the Toolbar you want.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingEnd="@dimen/activity_horizontal_margin"         
        android:id="@+id/toolbar_content">

        // define text and icon here

    </RelativeLayout>

</android.support.v7.widget.Toolbar>

Upvotes: 1

Vipul Asri
Vipul Asri

Reputation: 9223

You can implement like this in XML.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:layout_gravity="center">

          <ImageView
            android:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

          <TextView
            style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
            android:text="@string/app_name"
            android:background="?attr/selectableItemBackground"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</android.support.v7.widget.Toolbar>

Upvotes: 4

Avinash Joshi
Avinash Joshi

Reputation: 1308

in you activity class do this on create

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("example");
toolbar.setLogo(R.drawable.icon);

or edit the string resourse and change the title value and icon

Upvotes: 4

Related Questions