New Post has been published on http://droidstack.com/61/tutorial-actionbarcompat/
Using ActionBarCompat is almost as easy as native bar, and even similar to ActionBarSherlock, but there are a few differences that must be know. This tutorial covered the most simple integration.
It is pretty simple to use and implement ActionBarCompat, but we have to pay attention when we add it to our project.
In Android Studio it is very very simple. Open build.gradle and add this:
dependencies compile 'com.android.support:appcompat-v7:18.0.+'
In ADT (Eclipse) you can add it as a library project based on code that you can find in sdk\extras\android\support\v7\appcompat.
It is described here. Pay attention.
In your project you have to add this library project.
So let’s create a new project using API 18 and add the project under sdk\extras\android\support\v7\appcompat folder. I will set light theme with dark Action Bar. It is easy:
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
Now your activity needs to extend ActionBarActivity instead. This activity is based on FragmentActivity, so you will be able to use fragments without any extra effort. It’s easy:
public class MainActivity extends ActionBarActivity /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main);
And here it is!
But what if we want to add some menu items? It’s pretty much the same, but some attributes require our custom namespace:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_refresh" android:title="@string/action_refresh" android:icon="@drawable/ic_action_refresh" app:showAsAction="ifRoom" /> <item android:id="@+id/action_search" android:title="@string/action_search" android:icon="@drawable/ic_action_search" app:showAsAction="ifRoom" /> <item android:id="@+id/action_settings" android:title="@string/action_settings" app:showAsAction="never" /> </menu>
Now you can inflate the menu:
@Override public boolean onCreateOptionsMenu(Menu menu) getMenuInflater().inflate(R.menu.main, menu); return true;
f you need to access Action Bar programatically, you will call getSupportActionBar()
Adding an Action View
Action Views are those helpful views that can be added over the Action Bar when the user presses a menu item. The most typical is the Search View, and that’s the one I’m explaining here. Every Action View has been added to the support library, so you will need to add the reference to it instead of the one in the SDK:
<item android:id="@+id/action_search" android:title="@string/action_search" android:icon="@drawable/ic_action_search" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="ifRoom" />
Recovering this Action View from code is a little bit different. MenuItemCompat will do the hard work this time:
private SearchView mSearchView; @Override public boolean onCreateOptionsMenu(Menu menu) getMenuInflater().inflate(R.menu.main, menu); MenuItem searchItem = menu.findItem(R.id.action_search); mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); return true;
Now it’s time to show the search view when the user presses the magnifying glass icon. No difference with native Action Bar:
@Override public boolean onOptionsItemSelected(MenuItem item) switch(item.getItemId()) ... case R.id.action_search: mSearchView.setIconified(false); return true; ... return false;
You can add an OnQueryTextListener to detect user input on SearchView
public class MainActivity extends ActionBarActivity implements SearchView.OnQueryTextListener ... @Override public boolean onCreateOptionsMenu(Menu menu) ... mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); mSearchView.setOnQueryTextListener(this); return true; ... @Override public boolean onQueryTextSubmit(String s) Toast.makeText(this, s, Toast.LENGTH_LONG).show(); return true; @Override public boolean onQueryTextChange(String s) return false;
Conclusion
Apart from using some new classes and very little coding changes, the process of adding a new action view is similar and straightforward. If you have done this before, it will be easy.