2

I am having a tablayout, navigationdrawer and toolbar in an activity. Added 3 fragments to viewpager and added viewpager to tablayout: Code snippet of my activity:

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

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

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

Now in each fragment I have to display different options in toolbar: So I tried to access the toolbar in my fragment like this:

public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        _context = getActivity();
    mainView = inflater.inflate(R.layout.frag1, container, false);

    mToolbar =(Toolbar) mainView.findViewById(R.id.app_bar);
    mToolbar.setTitle("");
    toolbarTitle = (TextView) getActivity().findViewById(R.id.toolbar_title);
    toolbarTitle.setText("Fragment1");
    toolbarTitle.setSelected(true);
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    activity.setSupportActionBar(mToolbar);
}

Inflated the required menu in onCreateOptionsMenu(Menu menu, MenuInflater inflater) but not inflating as required the menu in toolbar. How can we change the toolbar options in Fragment.

user448250
  • 1,614
  • 2
  • 13
  • 15
  • 2
    do you call `setHasOptionsMenu(true)` in your fragments? – David Medenjak Feb 09 '16 at 22:46
  • Yes I added it on oncreate of fragment.But no use. – user448250 Feb 09 '16 at 23:35
  • http://stackoverflow.com/a/26989510/2826147 – Amit Vaghela Feb 10 '16 at 05:20
  • When I access the toolbar in fragment as shown in below, navigationdrawer is not working which added in Activity. (Toolbar) getActivity().findViewById(R.id.app_bar); but when I access tool bar like in fragment mToolbar = (Toolbar) view.findViewById(R.id.app_bar); then Navigationdrawer is working but Toolbar is not updating when I swipe fragments. What is best way to access the toolbar which is defined/declared in Activity from Fragment. – user448250 Feb 10 '16 at 19:14
  • 2
    I hope I'm not confusing the terminology (menu vs tool bar) but this works for me. I also have four tabs and want menu options only on my final tab. @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.save, menu); super.onCreateOptionsMenu(menu, inflater); } – BryanT Feb 10 '16 at 22:37
  • Thanks Bryan. Worked for me....I am trying to do some extra things which is not required, removed that code. To update the tool bar options no need to access the toolbar in fragment simply – user448250 Feb 10 '16 at 22:54
  • To update the tool bar options no need to access the Toolbar in fragment simple @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.save, menu); super.onCreateOptionsMenu(menu, inflater); } does the work. – user448250 Feb 10 '16 at 23:01

1 Answers1

1

This is working for me. This is all I have in onCreate. (Refreshable is an interface of my own).

public class SummaryFragment extends Fragment implements Refreshable {

@Override
public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    super.onCreate(savedInstanceState);
}

And this is where I inflate the menu.

  @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.save, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.save:
        {
            File csvFile = saveResults();
etc...

It's gone now, but I even had a different menu in my MainActivity and the two were both present when my Summary tab was active.

BryanT
  • 412
  • 3
  • 12