I have a toolbar that was set up using setSupportActionBar();, so at some point i want the toolbar not to be the actionbar again.
So is there anyway you can remove a toolbar as actionbar after setting it up using setSupportActionBar.
Then just try hiding the toolbar view when it is not needed...
Related
I have an app with a single Activity(MainActivity) and a Navigation Drawer. The navigation drawer, of course, opens the fragments in the activity. The problem I am facing is that I have already linked the MainActivity toolbar to the ActionToggle (hamburger) when I created an object of the ActionToggle. So the MainActivity toolbar shows in all fragments, but I want different toolbar XML for each fragments. If I want to hide the MainActivity toolbar in the fragments, the ActionToggle (hamburger) will also get hidden.
Please, how can I have different toolbar for each fragment, with the MainActivity ActionToggle in all the toolbars?
I would have expected the question to be clearer, but as I understand it, I can help you in the following way.
-If you want to change your toolbar from within different fragments,
below code block can help you.
Toolbar toolbar = view.findViewById(R.id.toolbar);
((MainActivity) getActivity()).getSupportActionBar().hide();
((MainActivity) getActivity()).setSupportActionBar(toolbar);
-If you just want to change the toolbar properties such as the title, you can use the code block below.
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setTitle("title");
I am little bit confuse about standalone toolbar & toolbar as actionbar.
Standalone toolbar can do the work of toolbar as actionbar then what is the need of toolbar as actionbar ?
take a peek at the documentation for android toolbar and notice that toolbar is portable and generic.
A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy.
I am using a custom navigation drawer (or something similar) which doesn't extend the default NavigationDrawer. Therefore it doesn't automatically show the navigation button on the top left of the ActionBar. I would like to implement that functionality that normally comes with the NavigationDrawer.
I have tried many things, such as :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_menu_black_24dp);
setSupportActionBar(toolbar);
or:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
or:
getSupportActionBar().setIcon(R.drawable.ic_menu_black_24dp);
but I just can't seem to get it to work. Any suggestions?
Put the code into your styles.xml file:
<item name="homeAsUpIndicator">#drawable/ic_drawer</item>
<item name="android:homeAsUpIndicator">#drawable/ic_drawer</item>
you have to set ActionBarDrawerToggle like below to set home icon.
mToggle.setHomeAsUpIndicator(R.mipmap.ico_menu);
mToggle is Object of your ActionBarDrawerToggle.
I fixed this problem by implementing a custom toolbar layout and using it as the supportActionBar.
I'm trying to make a Toolbar in a fragment an Action Bar. This is what I do:
View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false);
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity) mListener).setSupportActionBar(toolbar);
where mListener is the Activity that contains the fragment.
However, if I have the following straight after
ActionBar actionbar = ((AppCompatActivity) mListener).getActionBar();
actionbar is null. How come it's still null even when the ActionBar has been set in the previous line already? Otherwise, what's a good way to set the property of the newly set ActionBar?
Thanks
Since you use AppCompatActivity you use the supportActionBar and therefor you need to use getSupportActionBar().
This supports older android versions than the normal Activity and ActionBar.
What is the difference between the two? what all features are included in toolbar thats not in v7-21 toolbar? What all limitations are there in using v7-21 tollbar in place of toolbar?
If you look at the documentation for the Toolbar and the AppCompat Toolbar you can see that functionally, there aren't any differences between the two. Of course the SDK level required differs between the two and to use the Toolbar you have to call setActionBar(mToolbar); but with the support Toolbar you have to call setSupportActionBar(mToolbar);