Remove optionMenu from Fragment Toolbar - android

I am having an optionMenu at my MainActivity inflated inside onCreateOptionsMenu of my MainActivity and there are some fragments which are inflated inside the MainActivity which have their own Toolbars. But the problem is that the OptionMenu is also visible on the Toolbars inside the fragments. I don't want the fragments to have the OptionMenu for Fragments toolbar

setHasOptionsMenu(true) in your fragment onCreate();
in onCreateOptionsMenu inflate your fragment menu and remove MainActivity by using menu.removeItem(R.id.menu_id_to_be_removed); in onPrepareOptionsMenu

For the fragment you don't want onCreateOptionsMenu include following code in those fragments
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}

You can remove/hide overflow icon from toolbar by following the below steps
In your fragment where you want to hide it.
#Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_create_group,container,false);
setHasOptionsMenu(true);
}
override "onPrepareOptionsMenu". Like below
#Override
public void onPrepareOptionsMenu(#NonNull Menu menu) {
menu.clear();
}

Related

Android Hide or Show Title Bar from the Activity and Fragment

I have a Activity as below:
// My Activity Holding Fragment
public class MyActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity_main);
}
}
// My Fragment
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.my_fragment, container, false);
return view;
}
}
I wan to have the Title Bar for my Activity.
I want to Hide or Show Title Bar from Activity and Frgment on some conditions.
I tried below solutions, but as my activity does not derive from ActionBarActivity it does not work.
// getSupportActionBar returns null
getSupportActionBar().hide();
getSupportActionBar().show();
// getActionBar returns null
getActionBar().hide();
getActionBar().show();
// findViewById returns null
findViewById(android.R.id.title).setVisibility(View.GONE);
findViewById(android.R.id.title).setVisibility(View.VISIBLE);
// Are deprecated
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
None of the solutions did work for hiding Title Bar from Activity or Fragment.
Basically I want to hide the Activity title bar for the Activity.
First Change your App theme from res/value/styles.xml to this
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
//leave everything as it is
</style>
After that Add toolbar in your Activity's XML Layout
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:background="#color/colorAccent"
app:titleTextColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
//add your toolbar design here
</toolbar>
Now finally set up your toolbar in Activity Class
public class MyActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
//need to set toolbar before calling getSupportActionBar
setSupportActionBar(toolbar);
//Now show or hide according to your need
getSupportActionBar().hide();
getSupportActionBar().show();
}
}
Same would be applicable for fragment but for fragment you need to call view first
Toolbar toolbar = getView().findViewById(R.id.toolbar);

OnCreateOptionsMenu is not called during fragment transaction

I have a fragment that contains a search menu in the toolbar. when user first time click on navigation item, Fragment is added in activity which uses add method of FragmentManager and then it will be show, hide or remove from fragment container according to the logic.
My problem is when the first time I click on navigation item search menu is displayed in the toolbar but afterwards when I come back to this fragment, sometimes there will be blank toolbar without search menu. How can I solve this?
Here is the main part code of fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_search, container, false);
searchFragmentInstance = this;
filterManager = new FilterManager();
//set toolbar
Toolbar toolbar = view.findViewById(R.id.toolbar_search);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
//other code...
return view;
}
Display search menu in toolbar using this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onPrepareOptionsMenu(menu);
inflater.inflate(R.menu.search, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getContext().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) item.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
//other code...
}
I solved this problem by using adding this code:
//this method is called when the hidden state of the fragment is changed
//so when fragment is beign displayed using show method of fragment transaction
//this will be called
#Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if(!hidden){
//when fragment is not hidden anymore
//convert the toolbar into actionbar
Toolbar toolbar=(Toolbar)(((AppCompatActivity)getActivity()).findViewById(R.id.toolbar_search));
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
}
}

Change SupportActionBar from inside a fragment?

I just googled for "setsupportactionbar fragment", but all I found was "getsupportactionbar from fragment". Is that similar to each other, or is there something special?
Yes it is similar, but you have to keep in mind that you have to place setSupportActionBar() in onCreateView if you want to use elements of fragments layout-file:
..
public class FragmentOne extends Fragment {
..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
Toolbar toolbar = view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
return view;
}
..
}
you should not use setSupportActionBar() in fragment. because every time you add this fragment on activity a new instance of toolbar is created. so every time a new Toolbar is showing in screen. so it is not a good approach.

Working with OptionsMenu in nested fragment

I use a NavigationDrawer pattern that is implemented in my hostactivity MenuActivity. My navigation has 3 items: Item 1, Item 2, Item 3.
Each itemis bonded to a fragment.
When I click on Item 1, I displayed a fragment A that implements a ViewPager with several fragments (nested fragments).
In my nested fragments, I inflate a menu with the following method (It works fine !) :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
But when I click on another element of my menu (Item 2 -> display Fragment B or Item 3->display Fragment C), my menu (which was inflated in my nested fragment) is always visible but I want it to disappear.
Would you have a solution to this problem? Thank you in advance.
Just save child fragment and then override setMenuVisability:
#Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
if (savedFragment!= null)
savedFragment.setMenuVisibility(menuVisible);
}
it works for me
When I encountered this problem, I solved it by setting setHasOptionMenu(true) for both the child fragment and the "root" fragment. If the "root" fragment or another child fragment doesn't use option items, it's fine anyway, since you only inflate a menu in the child fragment needing it.
I just came across the problem and solved it by the following:
#Override
public void onDestroyOptionsMenu() {
this.setMenuVisibility(false);
super.onDestroyOptionsMenu();
Log.e(TAG, "onDestroyOptionsMenu");
}
#Override
public void onDestroyView() {
onDestroyOptionsMenu();
super.onDestroyView();
}
I noticed that onDestroyOptionsMenu is not being called so what I only did is call it from the OnDestroyView method and I set the menu visibility to false.
A slightly different approach from SafiS answer:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(...) {
View view = ...
setMenuVisibility(true);
return view;
}
#Override
public void onDestroyView() {
setMenuVisibility(false);
super.onDestroyView();
}
Add setRetainInstance(true) and setHasOptionMenu(true) in onCreate() in fragment.

How change Actionbar title by switching between Fragments

I have an application with two fragments when the application starts the Actionbar title is the one of the first fragment. But when I'm going to the second Fragment the title doesn't change.
So how do I change the ActionBar title?
This is my code:
public class ClientList extends Fragment {
#Override
public void onResume() {
super.onResume();
String title;
title = getResources().getString(R.string.client_title);
ActionBarActivity action = ((EmployeeActivity)getActivity());
action.getSupportActionBar().setTitle(title);
}
#Override
public void onDestroyView() {
super.onDestroyView();
String title2;
title2 = getResources().getString(R.string.action_settings);
ActionBarActivity action = ((EmployeeActivity)getActivity());
action.getSupportActionBar().setTitle(title2);
}
}
so if you say you cannot reach the method from onCreateView, so this is your solution I think:
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment,
container, false);
getActivity().setTitle("Title");
return view;
}
Because if you return before you call setTitle() you cannot reach it, that's true.
Hope it helps.
You can set your action bar title inside onCreate(if activity)/onCreateView(if fragment) method as getSupportActionBar().setTitle("Your Title");
Whenever you switch from one fragment to another fragment you can set the title in ActionBar. This can be done on onCreateView(...) of a fragment. Advisable try to change the name in Activity in which you are trying to load the fragment.
To do so ....
If you are using appcompat library
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(" your title ");
If you are not using appcompat library
ActionBar actionBar = getActionBar();
actionBar.setTitle(" your title ");
This should work:
getActivity().setTitle(YOUR_TITLE);
Call it from your Fragment when you wanted to set.

Categories

Resources