How to use Material Design Actionbar for Pre-Android L

Hey devs,

Android developers released support libraries for pre L Androids, but they haven’t updated their official documentation,

and I couldn’t find detailed tutorials or examples online.

I just followed the instructions in this page AppCompat v21 — Material Design for Pre-Lollipop Devices!.

It’s easy to use AppCompat in your existing application instead of the deprecated Actionbar in API 21

Step 1

If you are using Android Studio, add the following line to your Gradle (build.gradle):

compile ‘com.android.support:support-v4:21.0.0’
compile ‘com.android.support:appcompat-v7:21.0.0’

 

Note

Make sure targetSdkVersion is 21 and

compileSdkVersion 21
buildToolsVersion ‘21.0.2’

 

Step 2

Make your activity extend ActionBar Activity instead of Activity

public class HomeUI extends ActionBarActivity

 

Step 3

Replace getActionbar with getSupportActionBar()

and that’s it 😀

 

I had an issue with implementing SearchView in my Activities (my question in StackOverflow link),

the following code solved the problem

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(this);

 

Resources

1. AppCompat custom style
2. Flatten your icons: tutorial or use this generator

 

Happy Coding 🙂