Android: Hiding the Action Bar

The action bar on Android devices lives at the top of the screen, and provides you with the most common actions for your app, with the others hidden in an overflow button (the 3 vertical dots). For the app that I’m trying to create, I didn’t want to have this action bar shown on the login page, as there isn’t anything useful that could go in it. However, the code that’s needed to hide it correctly isn’t too easy to find, and the official Android documentation for hiding the action bar seems to be out of date.

Here’s the code that you get shown:

However, when you add this to part of your code and compile it, you’ll helpfully be told “Attempt to invoke virtual method ‘void android.app.ActionBar.hide()’ on a null object reference” and your app will crash immediately. Hmm… that’s not right at all.

It turns out that if you’re using the AppCompat v7 library (sometimes shown as AppCompat v21), you need to use the getSupportActionBar() function instead, and your class needs to extend the ActionBarActivity, like so:

If you’re not sure you’re using this library, look in the dependencies section in your build.gradle file for compile ‘com.android.support:appcompat-v7:21.0.+’. Hopefully it won’t take you too long to find this information out if you need it, and Google – please update your documentation!

Leave a Reply

Your email address will not be published. Required fields are marked *