2

I have 4 fragments

ISSUE

there is no proper title displayed in each fragment when I am jumping between them.

I have created a gif to show that: http://i.imgur.com/QFTdH3w.gif

Titles should be the same as a text in each fragment.

My MainActivity looks like this:

public class MainActivity extends AppCompatActivity {  
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

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

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new InfoFragment(), "INFO");
        adapter.addFragment(new NavFragment(), "NAV");
        adapter.addFragment(new PetrolFragment(), "PETROL");
        adapter.addFragment(new CalcFragment(), "CALC");
        viewPager.setAdapter(adapter);
    }

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

Each of my fragments looks like this:

public class InfoFragment extends Fragment {

    public InfoFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((MainActivity) getActivity()).setActionBarTitle("INFO"); // here are other names according to each fragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_info, container, false);
    }
}
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
user3448282
  • 2,629
  • 3
  • 25
  • 47
  • Follow this link http://stackoverflow.com/questions/15560904/setting-custom-actionbar-title-from-fragment – Ajit Kumar Dubey Oct 14 '15 at 10:38
  • Does your code not work? – PatrickMA Oct 14 '15 at 10:39
  • @Ajit I saw that thread and I tried several approaches to this problem, but anything helped me. My code does work, but not in a proper way. There are shown other titles, that should be. – user3448282 Oct 14 '15 at 10:42
  • 1
    When you changed your fragment find that fragment in activity then you can update you title in activity according to fragment. As per the my thought no need to update from fragment you can update in activity – Ajit Kumar Dubey Oct 14 '15 at 10:50
  • 1
    I know once setup pager after that never called fragment that's why your code is not working – Ajit Kumar Dubey Oct 14 '15 at 10:52

3 Answers3

4

I don't know if it's a proper way to do that but you can add a page change listener to your viewpager and in onPageSelected method you can change your title:

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

            }

            @Override
            public void onPageSelected(int position) {
                // change your title
                // inflate menu
                // customize your toolbar
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
savepopulation
  • 11,736
  • 4
  • 55
  • 80
1

Try the following code

For Fragment title :

getActivity().getActionBar().setTitle("Fragment1");
LionC
  • 3,106
  • 1
  • 22
  • 31
Naveen
  • 814
  • 2
  • 9
  • 22
1

Add this lines to all your Fragments onCreate or OnCreateView method

((MainActivity) getActivity()).setActionBarTitle("Your title");

by Default set a fragment in MainActivity

viewPager.setCurrentItem(0);
LS_
  • 6,763
  • 9
  • 52
  • 88
AndroidDev
  • 74
  • 1
  • 11