Skip to main content

ANDROID - Adding ActionBar Navigation Tabs




Note: if you are develop App < 3.0 Android OS then use ActionBarSherlock  support library.


ActionBarSherlock is an extension of the support library designed to facilitate the use of the action bar design pattern across all versions of Android with a single API.


Create new Android Project : in Main Activity



 package com.AlarmManager;  
 import android.os.Bundle;  
 import android.view.View;  
 import com.actionbarsherlock.app.ActionBar;  
 import com.actionbarsherlock.app.ActionBar.Tab;  
 import com.actionbarsherlock.app.SherlockFragmentActivity;  
 public class AlarmManagerActivity extends SherlockFragmentActivity {  
      public static String ACTIVE_TAB = "activeTab";  
      /** Called when the activity is first created. */  
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
           setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);  
           super.onCreate(savedInstanceState);  
           // setContentView(R.layout.main);  
           final ActionBar actionBar = getSupportActionBar();  
           actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  
           // add tabs  
           Tab tab1 = actionBar  
                     .newTab()  
                     .setText("Tab1")  
                     .setTabListener(  
                               new TabListener<TabFragment>(this, "tab1",  
                                         TabFragment.class));  
           actionBar.addTab(tab1);  
           Tab tab2 = actionBar  
                     .newTab()  
                     .setText("Tab2")  
                     .setTabListener(  
                               new TabListener<TabFragment1>(this, "tab2",  
                                         TabFragment1.class));  
           actionBar.addTab(tab2);  
           // check if there is a saved state to select active tab  
           if (savedInstanceState != null) {  
                getSupportActionBar().setSelectedNavigationItem(  
                          savedInstanceState.getInt(ACTIVE_TAB));  
           }  
      }  
      @Override  
      protected void onSaveInstanceState(Bundle outState) {  
           // save active tab  
           outState.putInt(ACTIVE_TAB, getSupportActionBar()  
                     .getSelectedNavigationIndex());  
           super.onSaveInstanceState(outState);  
      }  
 }  

Create TabListener Class:

 package com.AlarmManager;  
 import android.app.Activity;  
 import android.support.v4.app.Fragment;  
 import android.support.v4.app.FragmentTransaction;  
 import com.actionbarsherlock.app.ActionBar;  
 import com.actionbarsherlock.app.ActionBar.Tab;  
 public class TabListener<T extends Fragment> implements ActionBar.TabListener {  
   private Fragment mFragment;  
   private final Activity mActivity;  
   private final String mTag;  
   private final Class<T> mClass;  
   /** Constructor used each time a new tab is created.  
    * @param activity The host Activity, used to instantiate the fragment  
    * @param tag The identifier tag for the fragment  
    * @param clz The fragment's Class, used to instantiate the fragment  
    */  
   public TabListener(Activity activity, String tag, Class<T> clz) {  
     mActivity = activity;  
     mTag = tag;  
     mClass = clz;  
   }  
   /* The following are each of the ActionBar.TabListener callbacks */  
   public void onTabSelected(Tab tab, FragmentTransaction ft) {  
     // Check if the fragment is already initialized  
     if (mFragment == null) {  
       // If not, instantiate and add it to the activity  
       mFragment = Fragment.instantiate(mActivity, mClass.getName());  
       ft.add(android.R.id.content, mFragment, mTag);  
     } else {  
       // If it exists, simply attach it in order to show it  
       ft.attach(mFragment);  
     }  
   }  
   public void onTabUnselected(Tab tab, FragmentTransaction ft) {  
     if (mFragment != null) {  
       // Detach the fragment, because another one is being attached  
       ft.detach(mFragment);  
     }  
   }  
   public void onTabReselected(Tab tab, FragmentTransaction ft) {  
     // User selected the already selected tab. Usually do nothing.  
   }  
 }  

Create TabFragment class:

 package com.AlarmManager;  
 import android.os.Bundle;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import com.actionbarsherlock.app.SherlockFragment;  
 public class TabFragment extends SherlockFragment {  
      @Override  
      public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                Bundle savedInstanceState) {  
           // Inflate the layout for this fragment  
           View view = inflater  
                     .inflate(R.layout.main, container, false);  
           // do your view initialization here  
           return view;  
      }  
 }  

main.xml layout file:

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:gravity="center"  
   android:orientation="vertical" >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:gravity="center"  
     android:text="Tab1"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Button" >  
   </Button>  
 </LinearLayout>  

Same as TabFragment class i have used TabFragment1 class with two button.

Enjoy Coding..

Comments

  1. Hello sir

    can u tel me how to add libraries

    ReplyDelete
  2. I already have the support libraries installed and i have the "libs" folder in the project

    But stil i'm getting errors...

    ReplyDelete
  3. Please tell me what error you get..

    Please do this:

    Select Java Build Path, and click the Libraries tab.
    Click Add External JARs, browse to /extras/android/support/v4/android-support-v4.jar. Click OK
    On the Order and Export tab, select android-support-v4.jar.
    Click OK.

    if you already done this then check if your sharelock library have android-support-v4.jar then delete it from your project not from sharelock library.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why Domain-Specific AI Agents Beat One Big Agent

Everyone is building agents right now. Real estate firms. Independent insurance brokers. Fortune 500 companies with budgets big enough to hire an army of consultants. Ask around and you'll hear the same story everywhere: "we're building our own agent." And yet almost nobody is asking the obvious question: why does the default approach keep failing? One large, general-purpose agent gets wired up to every tool the business owns. It impresses in the demo. Then it quietly stalls before production. There's a gap between what businesses want and what they're actually getting. They want AI woven into their data, their workflows, their day-to-day operations. What they get instead is one oversized agent trying to be a sales rep, a compliance officer, and a customer support line, all at once. That gap is an architecture problem, not a model problem. Key Takeaways The default "one big agent" pattern breaks down on context bloat, cost, fragility, and portability...

ANDROID - update status Linked-IN

jars: 1. linkedin-j-android.jar 2. scribe-1.3.1.jar MainActivity: package com.testshare; import java.util.EnumSet; import org.scribe.oauth.OAuthService; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import com.google.code.linkedinapi.client.LinkedInApiClient; import com.google.code.linkedinapi.client.LinkedInApiClientFactory; import com.google.code.linkedinapi.client.enumeration.NetworkUpdateType; import com.google.code.linkedinapi.schema.Network; public class TestshareActivity extends Activity { private int LINKEDIN_OAUTH_RESULT_CODE = 4000; private OAuthService service; private static final String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(...

Connecting mysql Database in ANDROID using PHP & JSON

 To implement this tutorial you should have basic knowledge of how to run PHP script and start server.  If we talk about client-server architecture, client is Android device and in server side there is a combination of PHP Script and MySQL. In short, PHP Script sits in middle as shown in image. Lets suppose that we have a MySQL database named Employee, and a table int created, with the following SQL: CREATE TABLE `employee` (   `emp_id` int(11) NOT NULL auto_increment,   `emp_name` varchar(100) NOT NULL,   PRIMARY KEY  (`emp_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; The PHP code will be very simple: Now Create Android Project :  The Android part is only a bit more complicated: -use a HttpPost to get the data -convert response to string -parse JSON data in to List In Your First Activity : O/P :