-2

Hi there I have been using the ViewPagerIndicator library for some time now and I want my action-bar's title to change every time the user swipes to another Fragment to the other pages here is the code I am thinking off but it is just not working.

Activity miz = getActivity();
    miz.setTitle("Miz");

I have put this code in all my Fragments displaying different titles every time but for some reason it is not working properly as it is always late. I think it may have something to do with the OnCreate , OnResume or OnPause and can not lay a finger on it can some one help ? I want the Title of the Action Bar to change while the next Fragment is visible to the user.

I have also though of some code like this

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Activity miz = getActivity();
    miz.setTitle("Miz");
}
Mizzeeboy
  • 31
  • 9

3 Answers3

2

Regular :

this.getActivity().getActionBar().setTitle(title);

With ActioBarSherlock :

this.getSherlockActivity().getSupportActionBar().setTitle(title);
Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
  • @Mizzeeboy What are you trying to actually do? When the tab switches you want the title of the ActionBar to change? Or do you wan the subtitle? – Emil Davtyan Apr 14 '13 at 12:40
  • 1
    @Mizzeeboy I didn't down vote it but I should because you failed to communicate what you are trying to do to and what exactly is going wrong. Both Ahmad and I tried helping you and you are giving us attitude now? Go hit your head against the wall till you learn something because you clearly don't appreciate another's help. – Emil Davtyan Apr 14 '13 at 20:54
0

You can do this:

ActionBar actionBar = getActionBar();  // or getSupportActionBar() for ActionBarSherlock
actionBar.setTitle("test");

In a Fragment:

ActionBar actionBar = getActivity().getActionBar();  
actionBar.setTitle("test");
Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • Well with this code I get an error at the part where it says getActionBar this is probably cause I am in a Fragment not an Activity – Mizzeeboy Apr 14 '13 at 12:26
  • I have tried the other method and I have imported the Library of ActionBarSherlock and I still cannot get it to work with the getSupportActionBar() as I get a red line – Mizzeeboy Apr 14 '13 at 12:28
  • Nope this still doesn't work as I am putting this in Multiple tabs not just one. It sets the last one I code – Mizzeeboy Apr 14 '13 at 12:36
  • Thanks for Down voting my answer just cause you can't answer it that's very nice! – Mizzeeboy Apr 14 '13 at 13:19
0

Try

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Miz");

It will work

Reference: Setting a subtitle on my Toolbar from a fragment

Wais
  • 1,739
  • 1
  • 13
  • 13