Sumedh Tambat
Sumedh Tambat

Reputation: 811

Tabhost android

I am creating simple application which have total 3 activities

  1. sign in- having username, password field and sign in and sign up button
  2. sign up- having various form field and submit button
  3. Message- after successful sign in we come to this activity

I use tabhost which have 3 tabs defining above 3 activities. On click of tab i can switch between respective activities but when i use buttons on form to switch i can not able to see tabs. I want to show tabs on screen either i switch from tabs or button. Is there any concept like maintaining tab layout and only change activity in frame... I heard about tabhost group but not able to use that...is it possible with that. Tank you

my tabhost code is

public class TabActivity extends android.app.TabActivity
{
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);

      /** TabHost will have Tabs */ 
    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost); 

    /** TabSpec used to create a new tab.  
     * By using TabSpec only we can able to setContent to the tab.  
     * By using TabSpec setIndicator() we can set name to tab. */

    /** tid1 is firstTabSpec Id. Its used to access outside. */ 
    TabSpec signinTab = tabHost.newTabSpec("tid1");  
    TabSpec signupTab = tabHost.newTabSpec("tid2"); 
    TabSpec messageTab = tabHost.newTabSpec("tid3");

    /** TabSpec setIndicator() is used to set name for the tab. */ 
    /** TabSpec setContent() is used to set content for a particular tab. */ 
    signinTab.setIndicator("Sign In").setContent(new Intent(getApplicationContext(),SignIn.class));  
    signupTab.setIndicator("Sign Up").setContent(new Intent(getApplicationContext(),MainActivity.class));
    messageTab.setIndicator("Message").setContent(new Intent(getApplicationContext(),MessageDisplay.class));

    /** Add tabSpec to the TabHost to display. */ 
    tabHost.addTab(signinTab);  
    tabHost.addTab(signupTab); 
    tabHost.addTab(messageTab);//set Windows tab as default (zero based)

  // tabHost.setCurrentTab(2);
}

}

Upvotes: 1

Views: 984

Answers (1)

Nguyen Kien
Nguyen Kien

Reputation: 1927

Java Code:

public class TabActivity extends android.app.TabActivity
{
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);

      /** TabHost will have Tabs */ 
    TabHost tabHost = getTabHost();

    /** TabSpec used to create a new tab.  
     * By using TabSpec only we can able to setContent to the tab.  
     * By using TabSpec setIndicator() we can set name to tab. */

    /** tid1 is firstTabSpec Id. Its used to access outside. */ 
    TabSpec signinTab = tabHost.newTabSpec("tid1");  
    TabSpec signupTab = tabHost.newTabSpec("tid2"); 
    TabSpec messageTab = tabHost.newTabSpec("tid3");

    /** TabSpec setIndicator() is used to set name for the tab. */ 
    /** TabSpec setContent() is used to set content for a particular tab. */ 
    signinTab.setIndicator("Sign In").setContent(new Intent(getApplicationContext(),SignIn.class));  
    signupTab.setIndicator("Sign Up").setContent(new Intent(getApplicationContext(),MainActivity.class));
    messageTab.setIndicator("Message").setContent(new Intent(getApplicationContext(),MessageDisplay.class));

    /** Add tabSpec to the TabHost to display. */ 
    tabHost.addTab(signinTab);  
    tabHost.addTab(signupTab); 
    tabHost.addTab(messageTab);//set Windows tab as default (zero based)

  // tabHost.setCurrentTab(2);
}

}

XML Layout:

<?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="match_parent"
    android:layout_height="match_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="wrap_content"/>


    </LinearLayout>
</TabHost>

Upvotes: 1

Related Questions