Kenny Gunderman Kenny Gunderman - 6 months ago 113
Android Question

setTitle not working Android

I created a sliding tab activity and I am trying to dynamically change the title of the app bar. but what ever i do it doesnt seem to work! here 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 have also tried to set the title of the app bar in the onCreate method using:

getSupportActionBar().setTitle("Test");

which doesn't work either.

How can I solve this? Thanks!

Answer

Call setTitle method on the MainActivity object.

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

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

In conclusion, use getActivity().setTitle(...); in order to set the title from within a fragment.

Comments