My main Activity extends ListActivity, and now I have to implement an ActionBar with support for older versions.
I have readed about the ways to do this, and I decided to try this way:
I downloaded the source code of ListActivity, I modified few things, and now I need to implement here the ActionBar, so I'm trying to extend this Activity to ActionbarActivity. This way, I would have a custom ActionBarListActivity, and I could extend my Activity to this custom class, have then the functionality of the ListActivity and the ActionBar.
These are the steps I've done:
- Add the support v7 library to my project
- Set as aplications theme the @Style/Theme.AppCompat
But when I'm trying to extend the activity to ActionBarActivity, doesn't let me do this, it says "ActionbarActivity cannot be resolved to a type" and suggest to change it for 'ActionBar'(android.app). I know I have to import the "import android.support.v7.app.ActionBarActivity" but it doesn't let me do this.
So, what is going wrong here?
It does not sound like you imported the library right especially.
in eclipse you need to right click on the project, go to Properties, select Android in the list then Add to add the library
follow this tutorial in the docs
http://developer.android.com/tools/support-library/setup.html
Update:
Try this:
Import support library as a project from "sdk/extras/android/support/v7/appcompat"
.
Reference library in your project (for Eclipse, "Properties - Android - Add"
).
Build projects (for Eclipse,"Projects - Build All"
). Make sure, you have "android.support.v7.appcompat"
in your main project gen folder.
If it still doesn't solve your problem, restart eclipse.
then clean and rebuild project
If the problem persists, remove the support library from you computer and redownload it and follow above mentioned steps.
A simpler approach (to downloading source, etc.) is to break out the list into a Fragment and just include the Fragment in the Activity. I'll outline the steps to get there:-
- Split out the list into it's own layout XML. So you will end up with 2 XML's - one for the Activity 'container' which will be pretty simple - (see step 2), and one for the new Fragment layout which will be your overall layout for the list, but most importantly will include a
<ListView>
element).
- Include a
<FrameLayout>
placeholder in your activity's layout XML (to be the container for the fragment). It will be nested inside the top level layout (a <RelativeLayout>
would do as the top level). Give the placeholder an id like myFragmentHolder
.
- Create a new Fragment class (extend
ListFragment
) called say, MyListFragment
.
- Move all of your list code (such as ArrayAdapter code etc.) into the new fragment class
- Your Activity code is now really just a shell to contain the fragment and to manage the ActionBar. To support the list fragment all it needs in
onCreate()
is the call to setContentView()
and to replace the fragment:- getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_ENTER_MASK).replace(R.id.myFragmentHolder, new MyListFragment(), "MY_LIST").commit();
. ("MY_LIST" is an arbitary tag name you use to identify the fragment. It can be anything -read the docs for more info).
- Your Activity should still extend
ActionBarActivity
.
Now you have a nice separation of concerns. The Activity manages the ActionBar and the Fragment manages the List.
Thank you Nebu for your great explanation.
Theo, for simplicity, you might check out this tutorial on how to change a ListActivity to an ActionBarActivity by using a ListView object. (http://www.opensourcealternative.org/tutorials/android-tutorials/simple-android-listview-without-listactivity/)
However, Theo, I'll try to clarify Nebu's instructions with a little more details in order to try and answer your questions about his instructions.
Theo, as far as I understand, this is what Nebu is saying needs to be done.
In your original activity's XML for the listactivity, you had a FrameLayout.
You're going to copy the FrameLayout element to your new XML for your ListFragment, and the FrameLayout will be the root element in that new XML file.
In the original activity's XML file, replace the original FrameLayout element with a simple declaration of a FrameLayout element and an ID, just the start and end tags of the XML element without any child elements. This will be used to populate the FrameLayout with the one in your ListFragment at runtime.
The R.id.myFragmentHolder is the ID of the FragmentLayout placeholder in your original activity's XML file.
The "new MyListFragment()" call is calling the constructor of your new ListFragment class.
"MyList" can be any string, I would personally make it more descriptive of the list I was using, but it doesn't matter. Search Google for android fragment Tags to learn more about Tags.
When you extend ActionBarActivity, you will get errors unless you import android.support.v7.app.actionbaractivity in your actionbaractivity class that is replacing your original ListActivity class.
if I missed anything or if anything is not clear, please let me know.