Android Projects

Android Development Books

Monday

Android tab and tab host tutorials

The TabWidget itself does not do much except contain a list of tab 
labels that exist within the parent TabHost.
 Basically it is nothing but a set of labels that the user clicks
 in order to select a specific tab. The actual content of each tab
 is to be held by the 2nd object in the TabHost i.e. a FrameLayout
 
Hence both the TabWidget and FrameLayout need to be present on each tab.
 And in order to align htme one after another,we embed them into a LinearLayout
 
  First you have to create separate activities for each tab. Here I have 3 tabs. 
The 3 activities are AndroidActivity, WindowActivity, and 
IphoneActivity
Each one displays a simple message using TextView.
 
public class AndroidActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.android_layout);
    }
}


public class WindowActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.window_layout);
    }
}



public class IphoneActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.iphone_layout);
    }
}
 
you may defined here there tab:-
 
androidtab.xml 
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  
    <item android:drawable="@drawable/android_selected"
          android:state_selected="true" />
    
    <item android:drawable="@drawable/android" />
</selector>

windowtab.xml 
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  
    <item android:drawable="@drawable/window_selected"
          android:state_selected="true" />
    
    <item android:drawable="@drawable/window" />
</selector>
  

iphonetab.xml 
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  
    <item android:drawable="@drawable/iphone_selected"
          android:state_selected="true" />
    
    <item android:drawable="@drawable/iphone" />
</selector>

 
Here is the main layout file, named main.xml 

<?xml version="1.0" encoding="utf-8"?>
<TabHost 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:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
</TabHost>
 
Now main Java File, looks like this types, you can edit what types
 you want to add your apps
 

public class TabLayoutExample extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources res = getResources(); 
        TabHost tabHost = getTabHost(); 
        TabHost.TabSpec spec;  
        Intent intent;  
       
        intent = new Intent().setClass(this, MusicActivity.class);
      
        spec = tabHost.newTabSpec("Android").setIndicator("Android",
                          res.getDrawable(R.drawable.androidtab))
                      .setContent(intent);
        tabHost.addTab(spec);
      
 
        intent = new Intent().setClass(this, VideoActivity.class);
        spec = tabHost.newTabSpec("Window").setIndicator("Window",
                          res.getDrawable(R.drawable.windowtab))
                      .setContent(intent);
        tabHost.addTab(spec);
        intent = new Intent().setClass(this, FavoriteActivity.class);
        spec = tabHost.newTabSpec("Iphone").setIndicator("Iphone",
                          res.getDrawable(R.drawable.iphonetab))
                      .setContent(intent);
        tabHost.addTab(spec);
        tabHost.setCurrentTab(2);
    }
}
At last but not the least, dont forget to add to mainfeast all tab , like this.

      <activity   android:label="@string/app_name"  android:name=".AndroidActivity"      </activity>
       <activity   android:label="@string/app_name"  android:name=".WindowActivity"      </activity>
      <activity   android:label="@string/app_name"  android:name=".IphoneActivity"   
   </activity>
Hop you guys helpful this short article. source form www.developer.android.com
 
Video Tutorials Here: 


Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0