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.)
Related
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.
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.
I have an existing app that is using a Dashboard style pattern where there's a main menu, and clicking icons on the main menu drive start different activities... In order to navigate to a different function, you need to go back to the Dashboard menu and select another icon.
I want to change my application to a tabbed format similar to the one below with Icons:
What type of View is being used below? Is this a FragmentActivity? Also, what is the best approach to go about conversion? I have a bunch of layouts with listviews in linear/relative layouts... Will I be able to reuse any existing code? I want also to be able to use the solution with Android 2.1 and up if possible.
Thanks!
In the image you provided, it looks to be a TabHost that is used (which can be within a normal Activity or a Fragment Activity) and will be available for Android 2.1 and beyond when using the Android Support library. Based upon your description, you most likely have an Activity per each of your items that you will probably want to convert into a different Fragment. This may take a little time, but a Fragment is very similar to a normal activity in many ways, so once you start getting used to it, converting over the old Activities should be a breeze.
If you plan on using these tabs and you follow the Android design UI guidelines, you may want to use the TabHost in conjunction with a ViewPager. There is a great tutorial for this online that also allows for backward compatibility (down to at least 2.1) found here: http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/
Support library for fragments/viewpager: http://developer.android.com/tools/extras/support-library.html
More info about a TabHost and using Tabs with fragments can be found here:
http://developer.android.com/reference/android/widget/TabHost.html
http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
You can use TabLayout and a TabActivity to achieve layout in picture. Bad news is these are deprecated as of Android 3.0. Google advises to use fragments instead of these.
I'm following the android developer tutorials on tab layouts.
(im very very new to android)
I dont have errors, but when I run it I get an activity not found exception.
The ddms asks if ive added the artists activity to the android manifest file, which I have.
in the tutorial - http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
it says :
"Notice that the TabWidget and the FrameLayout elements have the IDs tabs and tabcontent, respectively. These names must be used so that the TabHost can retrieve references to each of them. It expects exactly these names"
Where should I specify those names exactly. I know the id's need to be linked, but I dont know where to do it.
I would actually recommend that you stop following this sample code as it is using an old of way of doing what you want with tabs.
I would instead recommend that you start reading about ViewPager. I would start by reading http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html
You'll want to get the Android Support/Compatibility Library plus the Support Demos and look at the source code inside. In particular, you'll want to pay close attention to http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabsPager.html
With FragmentTabsPager, you'll be using the latest techniques and have the best of both worlds: tabs as well as horizontal swiping. Google recommends using Fragments going forward for UIs like this. You can read more about Fragments at http://developer.android.com/guide/topics/fundamentals/fragments.html
See: http://developer.android.com/reference/android/widget/TabHost.TabSpec.html
The example given uses the third option given, an Intent that launches an Activity. You want #1, I think. So instead of
.setContent(intent)
Use:
.setContent(R.layout.exact_name)
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: