Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Action Bar set custom view within fragment

I have an Activity that holds some fragments.
This Activity is associated with a view pager, this view pager uses FragmentPagerAdapter, so every page of view pager is treated as a fragment.
Now, suppose I have customize the action bar view in any one of the fragment, and that custom view could be seen in other fragments also.

getActivity().getSupportActionBar().setCustomView(R.layout.custom_view_home);
        getActivity().getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);   

This is because, we are customising the view, using the activity context.
My Question :
Q. Cant we set the Custom View of action bar within a fragment ? So, it would not get reflected to other fragment.

like image 352
Shanki Bansal Avatar asked Jan 26 '26 18:01

Shanki Bansal


1 Answers

Short answer: Yes.

You should only allow currently visible fragment to add custom view to the ActionBar. Of course, you can do it right from the Fragment, with whichever context.

Requesting invalidation of the options menu will remove current custom view with the new one, i.e invalidating entire ActionBar. Similar approach you can use from the link above.

like image 64
Nikola Despotoski Avatar answered Jan 28 '26 07:01

Nikola Despotoski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!