Aymei Aymei
Aymei Aymei

Reputation: 3

android studio change title using fragment

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;
}

Upvotes: 0

Views: 1330

Answers (6)

T. Ferriera
T. Ferriera

Reputation: 16

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

Upvotes: 0

ZeroOne
ZeroOne

Reputation: 9117

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) {

    }
});

Upvotes: 1

Sanat Chandravanshi
Sanat Chandravanshi

Reputation: 665

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");

Upvotes: 0

Priya Rajan
Priya Rajan

Reputation: 697

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");

Upvotes: 0

Rishabh Mahatha
Rishabh Mahatha

Reputation: 1271

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

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

Upvotes: 1

Rajesh N
Rajesh N

Reputation: 6693

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

Upvotes: 0

Related Questions