35

I have an xml that I use with so many activities with fragments file but my problem is that I can't display the text I want in the toolbar, I use that xml that way because I have a navigation drawer and I needed to handle somethings so I had to do it that way.

my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StatusActivity"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        style="@style/ToolBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="@dimen/abc_action_bar_default_height_material" />

</RelativeLayout>

One of my activities:

public class GroupChatActivity extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
         getSupportActionBar().setDisplayShowHomeEnabled(true);

         ActionBar actionBar = getSupportActionBar();
         actionBar.setTitle("Groups history");

        Aniways.init(this);

        if(savedInstanceState == null)
        {
            FragmentManager manager = getSupportFragmentManager();

            Fragment fragment = GroupChatFragment.newInstance(getIntent().getIntExtra("uid", 0));
            manager.beginTransaction().add(R.id.frame_container, fragment).commit();
        }
    }
}

as you can see I try to set title to the action bar but it doesn't work.

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
Kareem Essam Gaber
  • 643
  • 1
  • 6
  • 16

10 Answers10

49
getSupportActionBar().setDisplayShowTitleEnabled(true);
massaimara98
  • 7,401
  • 1
  • 18
  • 18
  • 1
    For me it works without this on Android 5.1.1 i.e. with only getSupportActionBar().setTitle the title inside the toolbar is set, but if I try to set the title directly from the toolbar reference it doesn't, only through getSupportActionBar() – Alex Bitek Jun 29 '15 at 14:53
23

Setting,

app:title="@string/my_title"

within the declaration of the the android.support.v7.widget.Toolbar, hard codes the title in the toolbar.

To set the title programatically,

Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
toolbar.setTitle("my title");
setSupportActionBar(toolbar);

in your activity class.

Chad Mx
  • 1,266
  • 14
  • 17
8

Try this .. this method works for me..!! hope it may help somebody..!!

<android.support.v7.widget.Toolbar  
 xmlns:app="http://schemas.android.com/apk/res-auto"  
 android:id="@+id/my_awesome_toolbar"  
 android:layout_width="match_parent"  
 android:layout_height="wrap_content"  
 android:background="?attr/colorPrimary"  
 android:minHeight="?attr/actionBarSize"  
 app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >  

<TextView  
   android:id="@+id/toolbar_title"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:layout_gravity="center"  
   android:singleLine="true"  
   android:text="Toolbar Title"  
   android:textColor="@android:color/white"  
   android:textSize="18sp"  
   android:textStyle="bold" />  

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

EDIT

You can also use this.

setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
      getSupportActionBar().setTitle("Toolbar title");
Swaminathan V
  • 4,663
  • 2
  • 22
  • 32
3

I spent about a day looking for the cause of the issue. Neither supportActionBar?.title = "SomeTitle" nor supportActionBar?.setDisplayShowTitleEnabled(true) did not work, nor hacks with custom toolbars from answers here looked good.

My activity is using CollapsingToolbarLayout but not displaying any title, nor label from manifest, nor dynamically set one. But the sample ScrollingActivity (New - Activity - ...) displayed the title.

Finally I set up a sample project, copied MyActivity and ScrollingActivity and looked through the diff.

layout_height both of the AppBarLayout and CollapsingToolbarLayout must be set to match_parent or fixed size. Thats all. See working code below.

<com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay"
            >

        <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:toolbarId="@id/toolbar"
                app:contentScrim="?attr/colorPrimary"
                >

            <androidx.appcompat.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"
                    app:layout_collapseMode="pin"
                    />

        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
MainActivity
  • 1,650
  • 2
  • 12
  • 16
  • You can achieve the same by setting app:titleEnabled="false" on the CollapsingToolbarLayout – Milan May 19 '21 at 10:31
1

Try toolbar.setTitle('Groups history');

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
csmurphy84
  • 269
  • 2
  • 10
1

getActionBar().setTitle("Groups History");

or if you are using AppCompat libraries;

getSupportActionBar().setTitle("Groups History");

knoxgon
  • 1,070
  • 2
  • 15
  • 31
Kyle Mew
  • 11
  • 2
1

I did a custom action bar.

Layout iguepardos_action_bar.xml with this code

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blanco"
android:minHeight="?attr/actionBarSize">

    <TextView
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:singleLine="true"
    android:text="Toolbar Title"
    android:textColor="@color/naranja"
    android:textSize="18sp" />

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

In my Class extended AppCompatActivity I had this:

protected void onCreate(Bundle savedInstanceState) {
.... 
....

getSupportActionBar().setDisplayShowCustomEnabled(true); // is for specify use custom bar 
getSupportActionBar().setCustomView(R.layout.iguepardos_action_bar);  // my custom layout
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Button for come back

View mCustomView = getSupportActionBar().getCustomView(); // Set the view
TextView TitleToolBar = (TextView) mCustomView.findViewById(R.id.toolbar_title); // find title control
TitleToolBar.setText("The Title Show"); // Set the Title

}
A. Senna
  • 131
  • 1
  • 6
0

I actually had to get the toolbar_title to set the text into each different activity:

    toolbar = findViewById(R.id.toolbar);
    toolbarTitle = findViewById(R.id.toolbar_title);   //<----- here
    toolbarTitle.setText(getString(R.string.my_activity_toolbar_title));
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Matteo
  • 183
  • 1
  • 10
0

if your title color is white and the toolbar is also white, you will not differentiate it. So try to change the toolbar color

Nurseyit Tursunkulov
  • 8,012
  • 12
  • 44
  • 78
0

Given below code worked me:

 Toolbar myToolBar = findViewById(R.id.my_toolbar);
 setSupportActionBar(myToolBar);
 getSupportActionBar().setTitle(getString(R.string.toolbar_title_note));

First I tried with getSupportActionBar().setDisplayShowTitleEnabled(true); and then without it. It worked for both of the cases.

These are gradle settings for my project:

minSdkVersion 16
targetSdkVersion 30
compileSdkVersion 30
buildToolsVersion "30.0.3"
Dharman
  • 30,962
  • 25
  • 85
  • 135
Aman Srivastava
  • 1,007
  • 1
  • 13
  • 25