ListActivity with ActionBar support v7 - android

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?

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.

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.

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.

Related

Is there a way to have a function update a layout when it's in an <include> (Android)

Using Android Studio's built in Navigation Drawer Activity several files are created automatically. content_main is included automatically in the hello World application and I've changed content main to have some menu item buttons and another include. This include goes to the list_of_items layout.
My question is, how do I update the list_of_items layout with new information from a seperate class?
include merges the code at runtime so all it's views and id's will be available to you. you can do whatever needed.
for second point pass value to be updated in new class constructor then implement logic in it's method and return the value everything from mainactivity

Can't add any items to activity_main.xml in Android Studio

In Android Studio, I can't add any items from Palette to activity_main.xml in Design view. It just won't let me drag and drop them. Any idea why this happens?
Here is the PrintScreen:
According to new design method followed in android studio for android development you cannot add any elements to the activity_main.xml. Rather you should add them to content_main.xml.
You can learn more about it from this answer.
You can stop it from happening.
Answered by BNK:
If you create a new project, you can choose Empty Activity template
instead of Blank Activity. If you create a new activity, you can
choose Empty Activity instead of Blank Activity too.
how do I create an Empty Activity with a Fragment?
You start by creating an activity using the "Empty Activity" template.
Then, add a fragment class yourself or possibly via New > Fragment.
Please understand that all of the "New >" options, for activities,
fragments, etc., are all driven by templates. Those templates do what
they do. If you do not want what they are generating, don't use them,
and add the classes, resources, manifest entries, and such yourself.
Personally, I almost never use those templates, because it's simpler
IMHO just to create it all yourself than to rip out all the stuff you
don't need that gets generated.
I had same problem. In the design page, I saw error message "Render Error..."
I click Android Studio->check for update->update and restart
After android studio update, the drag&drop from Palette to design page works.
Check ConstraintLayout properties:
layout width and layout height should be "match parent", not "wrap content".
It works for me.
While you have marked a witget the editor don't let you drag.

Eclipse creates FrameLayout when creating new project

I've just updated some things on eclipse ADT and I created a new project.
I've seen that in the place where you can set the Main Activity's and main layout's names, now there is another place to set a fragment's name.
When finishing creating the project, in the layout folder now appears the main layout and a framgent layout.
Also, the mainActivity extends ActionBarActivity, makes a fragment transaction in the onCreate() method, and implements the PlaceholderFragment class.
Is there a place where these changes are explained or could someone explain how these modifications affect to coding?
Sorry If this is stupid question, but I'm learning about this and I would like to know how theses changes affect.
Those files are template file for an android application using fragments. Like mentioned before you can delete them or use them as you see fit. Fragments are a little different than regular activities and I would recommend reading the android developer guide on fragments if you want to work with these files:
http://developer.android.com/guide/components/fragments.html
http://developer.android.com/training/basics/fragments/creating.html
These will give you an idea of how fragments differ from regular activities.

How to show activities inside of a running activity?

I want to show my activities inside of a running activity. I need something like frame in html language that is used for showing other pages inside a page. I know Tabhost has this ability. Which one of other controls has this ability?
Thanks,
Google introduced fragments in Android 3.0 and upper to create a portion of user interface in an Activity. But it is not two Activity, becuase activity <> window. For lower version you can manually load xml layout in your activity.
The Fragment API is really your best choice, it's quite easy to use and you can dynamicly add them to your Activity Layout (take a look to Framgent For All - google Article) by code using the FragmentManager, this feature it's since 3.0 altough Google also released a pretty nice Compatibility Package that you can download out of the SDK Manager and added to your project like this Fragment For All:

My first ever Android code with Fragment

As an exercise, I am trying to rewrite the following google tutorial with Fragment class. The original tutorial implements tabs by using the old TabActivity class and TabHost/TabWidget annotation.
Tab Layout Google Tutorial
I have converted all Activity class with Fragment. I couldn't make my new code to work. I think I am stuck.I could not find any 'complete' Tab sample code using Fragment class.
Here are my questions
1. Should I define in the res/layout/main.xml or calling Actionbar.addTab(...) in my entry class, or both?
2. What would be complete res/layout/main.xml looks like? What would be the root element (i.e. LinearLayout, FrameLayout...etc)?
3. Any additional info would be greatly appreciated.
Check out this example from the compatibility library demos: FragmentTabs.java
and the corresponding layout: fragment_tabs.xml
Really, though, I wouldn't start with Tabs if you're trying out Fragments for the first time. Tabs in Android are a little bit of a mess. The above example (from Google itself) uses a hack just to get things working. Tabs just add a layer of unnecessary confusion when you're just learning.
Here's a more straightforward starting-out Fragments example/tutorial: http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html
(Just make sure to replace things like getFragmentManager() with getSupportFragmentManager() if you're using the compatibility library.)

Categories

Resources