user988575
user988575

Reputation: 33

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.

Upvotes: 0

Views: 2227

Answers (1)

LouieGeetoo
LouieGeetoo

Reputation: 868

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.)

Upvotes: 2

Related Questions