Is getActionBar() supposed to return null? - android

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.

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

Remove Toolbar as actionbar after using setSupportActionBar

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...

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!

Change ActionBar overlay from Fragment

I am using ActionBar Compat with NavigationDrawer from this source NavigationDrawer using only Android Support Library
I have a NavigationDrawer with some items, each item is a Fragment. Is it possible to change ActionBar overlay mode from Fragment to Fragment? I´d like to make ActionBar transparent in grid view, but keep it opaque in other case. How do I set this from Fragment?
in your fragment call, getActivity() and cast to ActionBarActivity with a getSupportActionBar()
// Update activity title
ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
actionBar.setTitle("lorem ipsum");
For making it transparent, have a look at this blog post:
http://flavienlaurent.com/blog/2013/11/20/making-your-action-bar-not-boring/

ProgressBar inside ActionBar from fragment

I tried the following method but it does not work.
getActivity().requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
getActivity().setProgressBarIndeterminateVisibility(false);
First, make sure your
getActivity().requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
Is before your
setContentView(R.layout.activity_layout);
Then call
getActionBar();
Before you use your
getActivity().setProgressBarIndeterminateVisibility(false);
How this works, is you are adding the window feature before your view is drawn. Then the getSupportActionBar() is used to just "open" the connection with your actionbar methods.

Categories

Resources