Show navigation icon without implementing navigationDrawer - android

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.

Related

NavigationDrawer - Different toolbar for each fragment

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");

Android Toolbar set title explanation

I have my app Toolbar here:
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
When i want to set the screen title, i have to do the following:
ActionBar bar = getSupportActionBar();
if (bar != null) {
bar.setTitle(SCREEN_TITLE);
}
My questions is, why when calling the toolbar.setTitle() is not working. But instead i need to get the action bar first and then i can set the title?
P.S. In the other hand, I can set the Toolbar icon normally without getting the action bar as so:
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white);
A Toolbar is a generalization of action bars for use within application layouts.
Unlike Action bar , toolbar is not part of window decor.You defines it and place it just like any other widget...therefor you have freedom to place it anywhere in the parent layout.
You have freedom to put any widget inside toolbar.
You can define multiple toolbars.
Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setActionBar() method.
As Toolbar is generalization of ActionBar. you have to first set Toolbar as ActionBar then you can use all method of ActionBar.
EDIT:
Here is Link that explains Toolbar concept.
Here is bug link reported in android issues: In issue they suggest that Once you call setSupportActionBar(Toolbar), the Action Bar is then responsible for handling the title, meaning that you need to call getSupportActionBar().setTitle(...) to set a custom title. It also gives explanation about why toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white); works.
I hope it helps!

Adding a custom Toolbar to a FragmentActivity (that uses a ViewPager)?

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.

Material Design Menu Item

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("");

Hide Home button on ActionBar Sherlock Actionbar extending Navigation Activity

I am using ActionBarSherlock on my simple app and would like to hide the HOME button if the user is on the home/main activity. I understand how to do so with the setHomeButtonEnabled(false), however, I am extending a class that contains my navigation and has setHomeButtonEnabled(true) and I cannot seem to overwrite that setting in my main activity.
Thanks to #andy I am able to get rid of the icon, however, I cannot get rid of the < arrow. Any ideas?
Thanks for any help.
With ActionBarSherlock just put this in your home/main activity onCreate() method:
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
The only way I have found to do this is to not have this set on your baseActivity:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and then set it as you will for each activity that requires the menu:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
or
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
Get the home button view and apply this
mHomeButton.setVisibility(View.INVISIBLE);

Categories

Resources