JJSmith
JJSmith

Reputation: 1903

Changing Application title during tab change

I am following this tutorial in order to set up some tabbed activities. All is going fine until I try change the main app title at the top of the screen. What I want to happen is change the title to reflect the title of the open tab.

I am quite new to fragments so this is causing some trouble. I am able to change the title for the activities but when ever I try change the fragments it sets the title the same for all tabs and not individually.

From my understanding the fragments are launched from the main activity and replace the current view, i just can seem to figure out where the tutorial is changing the title per fragment

Upvotes: 1

Views: 1059

Answers (3)

pRaNaY
pRaNaY

Reputation: 25312

Try below code:

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Toolbar Title String");

Upvotes: 1

Sagar Chavda
Sagar Chavda

Reputation: 125

//Here created a string array for store one or more titles
String[] tabsTitles = {"Title 1", "Title 2", "Title 3"};

viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

     @Override
     public void onPageSelected(int position) {
     // TODO Auto-generated method stub
            //here in array pass position as an index value for get title and set that
            actionBar.setTitle(tabsTitles[position]);
     }

     @Override
     public void onPageScrolled(int arg0, float arg1, int arg2) {
     // TODO Auto-generated method stub

     }

     @Override
     public void onPageScrollStateChanged(int pos) {
     // TODO Auto-generated method stub

     }
});

Upvotes: 2

Basil Battikhi
Basil Battikhi

Reputation: 2668

if i understood probably, you want to change title of that activity right ? if so just type

setTitle("My Title");

Upvotes: 0

Related Questions