android title won't show in toolbar

35,087

Solution 1

getSupportActionBar().setDisplayShowTitleEnabled(true);

Solution 2

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.

Solution 3

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");

Solution 4

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>

Solution 5

Try toolbar.setTitle('Groups history');

Share:
35,087
Kareem Essam Gaber
Author by

Kareem Essam Gaber

Updated on June 04, 2021

Comments

  • Kareem Essam Gaber
    Kareem Essam Gaber almost 3 years

    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.