Android Toolbar Title
Asked Answered
C

9

8

Unable to change toolbar title I set title in manifeast.xml & also used setTitle("TITLE");

I set title as History & Reports but it display different title Notifications which is another activity title.

I checked manifeast.xml but No change, Can anyone please help me from this.

Here is my code

public class Nav_HistoryAndReports extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("History & Reports");
    setContentView(R.layout.nav_history_and_reports);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setToolbar(toolbar);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

public void setToolbar(Toolbar toolbar) {
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new FragmentHistoryAndReports_Visits(), "VISITS");
    adapter.addFragment(new FragmentHistoryAndReports_Visitors(), "VISITORS");
    adapter.addFragment(new FragmentHistoryAndReports_Map(), "MAP");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

toolbar.xml

<?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="wrap_content"
    android:layout_margin="1dp"
    android:background="@color/white"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    android:titleTextColor="@color/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:theme="@style/GalaxyZooThemeToolbarDarkOverflow">

</android.support.v7.widget.Toolbar>
Cortex answered 21/3, 2017 at 5:34 Comment(9)
Please add code where you have set title "History".Veratridine
I'm sorry title is History & ReportsCortex
Whatever title you have set add that code on your question.Veratridine
You can find that in OnCreate Method where I added the code aboveCortex
setTitle() after setToolbar()Lole
Changed, but No ChangeCortex
share your toolbar layoutScrimp
Call getSupportActionBar().setTitle("History & Reports"); at the end of, or after calling, your setToolbar() method.Gigigigli
It WORKS..!!! Thanks Mike M.Cortex
I
7

Toolbar Title can be set in two ways

1.In Layout XML

<android.support.design.widget.AppBarLayout

android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:elevation="4dp"
android:theme="@style/AppTheme.AppBarOverlay"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        app:title="Main Activity"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:navigationIcon="@drawable/ic_arrow_back_white_24dp"

        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

2. In Java It will work in AppCompatActivity

Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Notice Board"); 
setSupportActionBar(toolbar);
Inevasible answered 13/3, 2018 at 11:14 Comment(1)
Make sure activity is AppCompatActivityInevasible
A
4

I used below line to change the toolbar title

getSupportActionBar().setTitle("YOUR_TITLE");
Adminicle answered 21/3, 2017 at 5:54 Comment(0)
S
1

After this line toolbar = findViewById(R.id.toolbar); add toolbar.setTitle("History & Reports");

and remove setTitle("History & Reports");

Spelter answered 21/3, 2017 at 5:39 Comment(2)
Thanks for the quick response, but No ChangeCortex
@Cortex add this also with Rachit suggestion getSupportActionBar().setDisplayShowTitleEnabled(true);Kill
S
1

Set title in your layout as i did below

    <?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="wrap_content"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        android:titleTextColor="@color/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:navigationIcon="?attr/homeAsUpIndicator"
        app:theme="@style/GalaxyZooThemeToolbarDarkOverflow">

        <TextView
            android:id="@+id/textViewTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:layout_marginLeft="5dp"
            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"/>

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

and in your activity simply set the toolbar and disable its default title as follows

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ActionBar actionBar = getSupportActionBar();
    if(actionBar != null)
        actionBar.setDisplayShowTitleEnabled(false);
Scrimp answered 21/3, 2017 at 5:53 Comment(0)
T
0

try adding getsupportActionBar().setTitle();

Thou answered 21/3, 2017 at 5:50 Comment(1)
setSupportActionBar(toolbar) - They're already using a NoActionBar theme.Gigigigli
L
0

Litle change your code look like this..

 toolbar = (Toolbar) findViewById(R.id.toolbar);
 setToolbar(toolbar);

And after setToolbar look

 public void setToolbar(Toolbar toolbar) {
    toolbar.setTitle("History & Reports");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Loralorain answered 21/3, 2017 at 5:51 Comment(0)
C
0

As Mike M said

I just added getSupportActionBar().setTitle("History & Reports"); at the end of, or after calling, your setToolbar() method.

Cortex answered 21/3, 2017 at 5:55 Comment(0)
V
0

I have found solution

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if (getSupportActionBar() != null)
        getSupportActionBar().setTitle(getString(R.string.calender));

This one work for me

Vanhorn answered 19/9, 2017 at 10:24 Comment(0)
W
0

If you use toolbar that supports actionBar, you should do as the following code:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

try {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
} catch (NullPointerException e) {
    Log.d(TAG, "onCreate: back button not supported!");
}

// here you set your activity title
getSupportActionBar().setTitle("Your Activity Title");
Whom answered 9/2, 2018 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.