Nerdy Bunz
Nerdy Bunz

Reputation: 7437

Accessing Toolbar/ActionBar object from inside fragment

After much success in developing an app and learning a ton about Java and Android, I'm running into a problem. I have been googling (things like toolbar, app bar, fragment, null pointer exception, actionbar, appcompatactivity, v7, v4 support libraries, theme=noactionbar etc etc.).

I also read this page.

I wouldn't have a problem if I was just using a normal Activity, but I'm getting very confused and I'm wondering if anyone can help.

Originally, I started a project with a boilerplate Navigation Drawer Activity in Android Studio 1.5.1. The app's main purpose is to display a list of people. This is done with a listFragment (because there are other things (fragments) the app does that can be selected from the navigation drawer).

My problems started when I decided to consolidate 3 very similar listfragments into one. I would do this by instead using a single listFragment and replacing the title on my Navigation Drawer's (Main) Activity's App Bar/Toolbar/Action Bar with a Spinner when this listFragment is selected from the Navigation Drawer and loaded into the main view.

The purpose of the spinner is as a way to filter the listview (listFragment) elements. The list is a list of people and the Spinner options would be something like, "Sort By:" [Recently Contacted, Favorites, Closest] (these were the initial three separate listFragments).

So... the closest thing I could find to a solution was:

How to combine Navigation Drawer and Spinner [like in Google+ App]

So I tried to adapt this solution to my problem... the only difference being that I'm using a Fragments and trying to access getActionBar() (?) from inside my Fragment.

From inside the fragment, I put:

ActionBar actionBar = getActivity()).getActionBar();

I get a warning that it may return null and it does.

I also tried:

ActionBar actionBar = getActivity()).getSupportActionBar();

with the same result.

Then I learned (and read in the main activity) that Toolbar is the new version of ActionBar... and I can see the following in the boilerplate code of my main activity:

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

So "Ahaa!!" I changed my code to:

Toolbar toolbar = getActivity()).getT.......

But there is no such getToolbar() method provided... and apparently I dont know what I'm doing.

Keep in mind:

My basic questions are:

1) What class of object (App bar, actionbar, toolbar, ??) do I need to access from within the fragment?

2) How do I access that object from inside the fragment?

Thank you for your patience!

UPDATE:

Since posting this, I have learned that:

Toolbar and ActionBar are not the same thing. I am using a Navigation Drawer that uses "Material Design." My Main Activity extends AppCompatActivity and uses Toolbar, not ActionBar.

From my fragment, I can access the toolbar object using:

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);

However, I can't find any tutorials that explain how to add a spinner to a TOOLBAR from a fragment. They only explain how to do it using Actionbar. (See this link I referenced earlier)

UPDATE!!!

It doesn't seem possible to create/control a spinner within a main activity toolbar from inside a fragment by accessing only the toolbar object, so Naresh and Sahil's solutions below are correct... I must use getSupportActionBar() and cast it using either (AppCompatActivity) or ([TheActualNameOfMyMainActivity]) like this:

ActionBar actionbar = ((AppCompatActivity)getActivity()).getSupportActionBar();

Upvotes: 3

Views: 4566

Answers (1)

Naresh
Naresh

Reputation: 108

If you are using AppCompatActivity then do it like this.

ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();

Or, If you are using ActionBarActivity then try this.

ActionBar actionBar = ((ActionBarActivity )getActivity()).getSupportActionBar();

Upvotes: 4

Related Questions