3
votes

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 Main Activity extends AppCompatActivity (I think this is required in order for Navigation Drawer to work)
  • The theme of my main activity is "AppTheme.NoActionBar" (this was provided in the default code in the beginning, but makes no sense to me since there clearly is a bar at the top of the screen and it's part of the navigation drawer)
  • My Main Activity imports the following: import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.util.Log; import android.view.View; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import com.android.volley.RequestQueue;

  • build.gradle contains: dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile 'com.android.volley:volley:1.0.0' compile 'org.immutables:gson:2.1.8' }

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();
1
can you please tell in brief ?Amit Vaghela
Is your fragment attached to activity?Sahil Shokeen
Share your github repoSahil Shokeen
@BooberBunz add ActionBar instead of toolbar as you will have more control and getSupportActionBar will return ActionBar and theme is no ActionBar then it will not have ActionBar but you are getting i think because u added thats y u are seeing ActionBar Refer here might u get some help :- coderzpassion.com/android-working-with-material-designJagjit Singh

1 Answers

4
votes

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