Saku
Saku

Reputation: 180

How to Show Second Tab in Android while calling from an Activity

Here is the Code which I'm Trying. How to go to Tab2 from Sample? Please help me to Solve this.

public class DemoTab extends TabActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_and_login_tab);
        setTabs() ;
    }
    private void setTabs()
    {
        addTab("Tab1", R.drawable.tab_search, Tab1.class);
        addTab("Tab2", R.drawable.tab_home, Tab2.class);        
    }

    private void addTab(String labelId, int drawableId, Class<?> c)
    {
        TabHost tabHost = getTabHost();
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }

}

Code for Tab2

public class Tab2 extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.tab2);
    }


}

Code for Tab1

public class Tab1 extends Activity implements OnClickListener {
    Button check;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab1);
        check= (Button) findViewById(R.id.check);
        check.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.check:
            //Do the Task Search Here
            Intent i = new Intent(getApplicationContext(), Sample.class);
            startActivity(i);
            break;

        }
    }
}

Code for Sample

public class Sample extends Activity implements OnClickListener {
    Button redirect;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_result);
        redirect= (Button)findViewById(R.id.redirect);
        redirect.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.redirect:

Here What should i do to get view of Tab2,By default the DemoTab's Current Tab will be Tab1.

Intent i = new Intent(getApplicationContext(), DemoTab.class);
                startActivity(i);
            break;

        default:
            break;
        }
    }

}

Upvotes: 1

Views: 4590

Answers (3)

Android Developer
Android Developer

Reputation: 987

Just try like this...

when call the tabactivity call like this

 Intent go=new Intent(this,TabActivity.class);
 Bundle b=new Bundle();
 b.putString("condition","tab2");
 go.putExtras(b);
 startActivity(go);

then in tabAcitivity class

public class Demo extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Bundle b=getIntent().getExtras();
    String con=b.getString("condition");

    TabHost tab = getTabHost();
    TabSpec tab1 = (TabSpec) tab.newTabSpec("tb1");
    TabSpec tab2 = (TabSpec) tab.newTabSpec("tb2");
    tab1.setIndicator("Tab1").setContent(
            new Intent(this, Tab1.class));
    tab2.setIndicator("Tab2").setContent(
            new Intent(this, Tab2.class));

    tab.addTab(tab1);
    tab.addTab(tab2);
   if(con.equals("tab2")
     {
     tab.setCurrentTab(1);
     }
}
}

Upvotes: 3

Shobhit Puri
Shobhit Puri

Reputation: 26007

setCurrentTab(index) opens the tab to be displayed by default, specified by the index position of the tab. If you set tabHost.setCurrentTab(1); property while creating your TabActivity, it will show the tab on 1st index as you desire( 0 will set it to a tab on 0th index). You might want to check some of the above questions as well:

android setting default tab in tab-activity

Start Android App in a specific tab

setCurrentTab of a tabHost

Edit

Android TabHost setCurrentTab()

public class DemoTab extends TabActivity {
    //Declare this outside the elements
    TabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //...
    }
    private void setTabs()
    {
        // ...
        addTab("Tab1", R.drawable.tab_search, Tab1.class);
        addTab("Tab2", R.drawable.tab_home, Tab2.class);  
        // Here you may try adding
        tabHost.setCurrentTab(1);    
    }

    private void addTab(String labelId, int drawableId, Class<?> c)
    {
        tabHost = getTabHost(); v//edit this line
        //....
    }

Hope this helps

Upvotes: 0

Dhruv
Dhruv

Reputation: 1872

I have two Classes "One.Java" and "Two.Java". My main Activity which is extends 'TabActivity'. In onCreate() method :

Intent one = new Intent(this, One.class);
Intent two = new Intent(this, Two.class);

TabHost t = (TabHost) findViewById(R.id.tabhost);
t.setup();

TabSpec tspecOne = t.newTabSpec("CLICK ONE").setIndicator("CLICK ONE")
            .setContent(one);
t.addTab(tspecOne);

TabSpec tspecTwo = t.newTabSpec("CLICK TWO")
            .setIndicator("CLICK TWO").setContent(two);
t.addTab(tspecTwo);

Upvotes: 0

Related Questions