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.