I'm trying to use ActionBarCompat on my own project. I've already opened the sample project (http://developer.android.com/resources/samples/ActionBarCompat/index.html), But I don't know how to implement it on my own.
I can't find any tutorial of some kind. Should I make this project as a library? Can someone give me some directions, please.
As for implementation, just stick to the sample code provided under the MainActivity.java
class. You can find it here or under <your local android-sdks folder>/samples/android-15/ActionBarCompat/src/com/example/android/actionbarcompat/MainActivity.java
. In general, all you need to do is the following:
ActionBarActivity
onCreateOptionsMenu()
so that it inflates the menu you coded in step #1onOptionsItemSelected()
so that you handle the event when the user taps any of the ActionBar items you defined in step #1.I think it makes sense to build an Android Library project out of the ActionBarCompat code; then you can just reference it from your custom Android project. Remember it's licensed under the Apache License, Version 2.0.
This answer describes how to use the new ActionBarCompat library (July 2013).
In Android Studio, open build.gradle
and add this:
dependencies {
compile 'com.android.support:appcompat-v7:18.0.+'
}
In Eclipse, create a library project based on the code in sdk\extras\android\support\v7\appcompat
and add it to your project.
Activities have to extend ActionBarActivity
.
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
ActionBar ab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ab = getSupportActionBar();
ab.setTitle("Test");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView =
(SearchView) MenuItemCompat.getActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
}
Themes have to be (or extend) one of the Theme.AppCompat
themes, for example:
<activity android:theme="@style/Theme.AppCompat.Light" ... />
Source: How to add ActionBarCompat to your project, blog post by Gabriele Mariotti
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With