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");
Related
I want to hide toolbar in my fragment and display my own custom toolbar for that fragment.
ps. i have darkactionbar in styles.xml. after hiding and showing my own custom toolbar, here is the images. please help me out in this.enter image description here
before navigating to fragment - image 1
on fragment - image 2
closing fragment and returning to activity - image3
There are multiple ways to hide the inbuilt toolbar -
Let's talk about all steps for Kotlin code:
If you want to hide toolbar in Activity you can use -
actionBar?.hide
If you want to hide toolbar in Fragment you can use -
(requireActivity() as MainActivity).supportActionBar!!.hide()
Another way is to change DarkActionBar to NoActionBar in your themes.xml
Now these were the ways by which you can disable inbuilt toolbar.
Next is to see how to create your own toolbar.
So simply in MainActivity xml create your design and add clicks to it in kt file.
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 am using navigation drawer with fragments.
Generally, I want the fragments to change the activity's toolbar title.
When starting the app, I supply a default fragment by using fragmentManager replace method.
But the fragment can't change the title of the activity's toolbar.
On the other hand, when changing fragments after App is started, it can change the activity's toolbar title
Can someone elaborate on this?
Thank you
Best regards Morten
I'm using a ViewPager to slide across 3 screens at the moment, which is initialised in a FragmentActivity.
My aim is to have a Toolbar or ActionBar that remains the same for each page in the ViewPager - apart from the title, which changes depending on the page you are on.
Does anybody know how I can achieve this?
I have tried the following
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
setActionBar(mActionBarToolbar);
getActionBar().setTitle(mViewPager.getCurrentItem());
But I keep getting an error setActionBar (android.widget.Toolbar) in Activity cannot be applied to android.widget.support.v7.Toolbar.
Any ideas?
When using the support libraries, you need to use the support methods.
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mActionToolbar);
getSupportActionBar().setTitle(mViewPager.getCurrentItem());
You should be golden if you replace your code to the code above.
So here I thought after implementing Material Design everything would be just like it says in the guideline. But when I used a menu item with only a title (THE RIGHT ONE, NOT HOME ICON), it's not the same as what it would like in the guideline :
Instead, it's still like this :
Or do I have to create a custom view to have a menu item like that? if so, how?
Thank you for your help..
You can try calling getSupportActionBar().setIcon(R.drawable.your_icon); in the onCreate method of your activity
Or if you are using toolbar, you can do the following:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.your_icon);
toolbar.setTitle("");