Question

I have 3 Activities I'd like to link together using fragments because TabActivity is no longer used.

I've looked around a bit and understand the general idea but am not sure how to implement the entire group of activities together with fragments without messing up the original look and function...

Here's how it works... 1 have 2 pages, recipe_ingredients.xml and recipe_new.xml. I have a recipe_tabs.xml linked to them through RecipeNew.java. The recipe_tabs.xml uses android:tabhost and android:tabcontent to create tabs and give the tabs a style ect... The tabs sit side-by-side on top of recipe_ingredients.xml and recipe_new.xml. The tab with the page your on is highlighted and you can easily flip back and forth through the pages and the tabs highlight corresponding to what page your on.

Here is the java activity in android manifest: <activity android:name=".RecipeNew"></activity>

To create the Tabs and tab layout I have a file called recipe_tabs

Here is the code for recipe_tabs: (I already changed the layout in this to FragmentTabHost, just not sure how to connect it all using the fragment method.

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout 
android:id="@+id/tablinearlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabWidget 
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
</TabWidget>
<FrameLayout 
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>

Here is the java using tabactivity which I want to change to fragment, but I'd still like it to look and function the same, is this possible? Here is the code for RecipeNew.java

public class RecipeNew extends TabActivity implements OnTabChangeListener {            

TabHost _tabHost;
Resources _res;

@Override
public void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.recipe_tabs );

    tabHost =getTabHost;
    res = getResources();
    tabHost.setOnTabChangedListener( this );

    TabHost.TabSpec _tabSpec;

    tabSpec = _tabHost.newTabSpec("recipes").setIndicator("Recipe", res.getDrawable( R.drawable.recipes_tab ) ).setContent( new Intent( this,RecipeEntry.class ) );
    tabHost.addTab( _tabSpec );

    tabSpec = _tabHost.newTabSpec("ingredients").setIndicator("Ingredients", res.getDrawable( R.drawable.ingredients_tab) ).setContent( new Intent( this.RecipeIngredients.class ) );
    tabHost.addTab( _tabSpec );

    for(int i=0; i<_tabHost.getTabWidget().getChildCount(); i++)
    {
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor( Color.LTGRAY );
    }

    tabHost.getTabWidget(.setCurrentTab(1);

    tabHost.getTabWidget().getChildAt(1).setBackgroundColor( Color.DKGRAY );

}

public void onTabChanged(String tabI(d) {
    for(int i=0; i<_tabHost.getTabWidget().getChildCount(); i++)
    {
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor ( Color.LTGRAY );
    }
    tabHost.getTabWidget().getChildAt( _tabHost.getCurrentTab()).setBackgroundColor( Color.GRAY );
    }

Any advice or help would be appreciated. In the end, the layout is supposed to render 2 tabs at the top of the page, whichever page your on is highlighted in the tab, to move between the two pages u can simply hit the tabs.. The tabs look like 2 boxes at the top of the page...The looks It's defined in recipe_tabs

Was it helpful?

Solution

Looking at your code it seems your tab contents are Activities (RecipeEntry.class and RecipeIngredients.class).

Just create Fragments with the same functionality and follow the example code shown for FragmentTabHost.

In other words, use a FragmentActivity instead of a TabActivity, use Fragments instead of Activities for the tab content and then add call addTab(...) in the same way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top