We provide complete mobile and web apps development solutions

Sunday, February 3, 2013

Tabs like with ActionBar


 TabActivity is depreciated  in Api 13. Tabs are presented in latest versions with ActionBar.
ActionBar.newTab().

So, prefer to not to use depreciated classes and methods. So use latest classes and methods.

Here is the simple example of creating Tab like activity using fragments.



 MobEngineersActivity.java

package com.srinivas.mobengineers;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.os.Bundle;
import android.widget.Toast;


public class MobEngineersActivity extends Activity {
    public static Context appContext;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appContext = getApplicationContext();

 
        ActionBar actionbar = getActionBar();       // initilize actionBar
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);     // set navigation mode
       
        ActionBar.Tab Category1Tab = actionbar.newTab().setText("FOOD");
        ActionBar.Tab Category2Tab = actionbar.newTab().setText("BEVERAGES");
       
        Fragment FoodFragment = new FoodFragment();
        Fragment BeveragesFragment = new BeveragesFragment();
       CategoryTab.setTabListener(new MyTabsListener(FoodFragment));
      BeveragesTab.setTabListener(new MyTabsListener(BeveragesFragment));

        actionbar.addTab(FoodTab);
        actionbar.addTab(BeveragesTab);
    }
Food.java

public class FoodFragment extends Fragment {
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.afragment, container, false);
    }
  }

Beverages.java
public class FoodFragment extends Fragment {
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.afragment, container, false);
    }
  }

 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

 </LinearLayout>
</LinearLayout>



0 coment�rios:

Post a Comment

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts