Java – setTitle does not work on Android

setTitle does not work on Android… here is a solution to the problem.

setTitle does not work on Android

I’ve created a sliding tab activity and I’m trying to dynamically change the title of the app bar. But no matter what I do, it doesn’t seem to work! This is my MainActivity .java:

public class MainActivity extends AppCompatActivity {

ViewPager mPager;
SlidingTabLayout mTabs;

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

getSupportActionBar().setDisplayShowHomeEnabled(true);

mPager = (ViewPager) findViewById(R.id.pager);

mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

mTabs = (SlidingTabLayout) findViewById(R.id.tabs);

mTabs.setCustomTabView(R.layout.custom_tab_view, R.id.tabText);

mTabs.setDistributeEvenly(true);

mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
    mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

mTabs.setViewPager(mPager);

}

class MyPagerAdapter extends FragmentPagerAdapter {

int tabIcons[] = {R.drawable.ic_events, R.drawable.ic_clients, R.drawable.ic_history};
    String[] tabText = getResources().getStringArray(R.array.tabs);

public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

@Override
    public Fragment getItem(int position) {

switch (position){
            case 0:
                getSupportActionBar().setTitle("Events");
                EventsFragment eventsFragment = EventsFragment.getInstance(position);
                return eventsFragment;

case 1:
                getSupportActionBar().setTitle("Clients");
                ClientsFragment clientsFragment = ClientsFragment.getInstance(position);
                return clientsFragment;

case 2:
                getSupportActionBar().setTitle("History");
                HistoryFragment historyFragment = HistoryFragment.getInstance(position);
                return historyFragment;

default:
                getSupportActionBar().setTitle("Events");
                EventsFragment defaultFragment = EventsFragment.getInstance(position);
                return defaultFragment;

}

}

@Override
    public CharSequence getPageTitle(int position) {

Drawable drawable = getResources().getDrawable(tabIcons[position]);

drawable.setBounds(0,0,64,64);

ImageSpan imageSpan = new ImageSpan(drawable);

SpannableString spannableString = new SpannableString(" ");

spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

return spannableString;
    }

@Override
    public int getCount() {
        return 3;
    }
}

I also tried setting the title of the app bar in the onCreate method using:

 getSupportActionBar().setTitle("Test");

This also doesn’t work.

How do I fix this? Thanks!

Solution

Call the setTitle method of the MainActivity object.

Called from within MainActivity: just setTitle(...). Called from a different class (MyPagerAdapter): activity.setTitle(...).

activity is a reference to the MainActivity object. You can use getActivity(), which returns the activity associated with the fragment.

To summarize, use getActivity().setTitle(...); to set the title in the fragment.

Related Problems and Solutions