0

i have a problem with changing my title. i can change the title using fragment but each time i click or change to another fragment the title is didnt change to the correct title. here is my code:

This is my main tabactivity

public class TabActivity extends AppCompatActivity {


private SectionsPagerAdapter mSectionsPagerAdapter;


private ViewPager mViewPager;


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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_tab, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                profile_menu tab1 = new profile_menu();

                return tab1;
            case 1:
                group_menu tab2 = new group_menu();
                return tab2;
            case 2:
                world_menu tab3 = new world_menu();
                return tab3;
            case 3:
                prize_menu tab4 = new prize_menu();
                return tab4;

            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Profile ";
            case 1:
                return "Group ";
            case 2:
                return "World ";
            case 3:
                return "Prize";
        }
        return null;

    }




}

public void setActionBarTitle(String title){
    getSupportActionBar().setTitle(title);
} 
}

and this is my fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.prize_menu, container, false);
    getActivity().setTitle("Prize");
    return rootView;
}
Aymei Aymei
  • 3
  • 1
  • 4
  • Have you looked this, [Please check here](http://stackoverflow.com/questions/15560904/setting-custom-actionbar-title-from-fragment) – Sunisha Guptan May 03 '17 at 10:24

6 Answers6

1
  if (getActivity().getActionBar() != null) {
        getActivity().getActionBar().setTitle("YourTitle");
    }

Use this line in onViewCreated() of each fragment will solve your problem

Rishabh Mahatha
  • 1,251
  • 9
  • 19
1

I assume that you want to change the title when tab change?

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                getSupportActionBar().setTitle("Profile ");
                break;
            case 1:
                getSupportActionBar().setTitle("Group");
                break;
            case 2:
                getSupportActionBar().setTitle("World");
                break;
            case 3:
                getSupportActionBar().setTitle("Prize");
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});
ZeroOne
  • 8,996
  • 4
  • 27
  • 45
  • didnt work my Title stuck on Prize it didnt want to change at all. did i put it in the wrong place? i put inside @Override protected void onCreate(Bundle savedInstanceState) { – Aymei Aymei May 03 '17 at 12:21
  • @AymeiAymei yup..sorry my mistake..tq – ZeroOne May 03 '17 at 12:52
0

call getActivity().setTitle("Prize"); it in onResume of each fragment

tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
Rajesh N
  • 6,198
  • 2
  • 47
  • 58
0

In Fragment Set your title in simple way. Add this method in your common class. Add your title with back arrow in action bar.

public void setPageTitle(String title) {
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowCustomEnabled(false);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>"+title+ "</font>"));
    }

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstantState){
        view = inflater.inflate(R.layout.fragment_name,container,false);
        setPageTitle("title");
        return view;
    }

If you want title alone, use this line alone in your fragment onCreateView(). But this code does not handle back arrow.

    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Title");
Priya Rajan
  • 687
  • 8
  • 21
  • it didnt works still on random. when i click group fragment the title change to profile and sometime it change to world fragment – Aymei Aymei May 03 '17 at 12:08
  • Im 100% sure, im using this code in all my projects. Have you placed the last line ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Title"); in your fragment onCreateView? – Priya Rajan May 03 '17 at 12:22
  • yes i did maybe it just doesnt match with mine i dont know why – Aymei Aymei May 03 '17 at 12:29
  • Do you want to set unique page title or to set App name in Action bar? – Priya Rajan May 03 '17 at 12:30
0

Quick way to do: Declare your ToolBar as static and access from the fragment:

MainActivity:

public static Toolbar mToolbar;

in your fragment:

mToolbar.setTitle("your title here");
0

You set the Fragment title in the mobile_navigation.xml file in res -> navigation. Use the android:label=""