Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI all,

I have just started programming on android using eclipse.

I have used tab layout control to design my form.

Now i have 3 tabs and 3 different activity.

I have already design my layout XML for this. Which displays everything pretty good.

My java code is as below.

Java
TabHost th = (TabHost)findViewById(R.id.tabhost);
        th.setup();
        
        TabSpec specs = th.newTabSpec("Enquire");
        specs.setContent(R.id.tbenquire);
        specs.setIndicator("Enquire");
        
        th.addTab(specs);


I have 3 different tabs like this, but the problem is how can i assign them their own activity class?

I found on google to use something like this


Java
Intent intent = new Intent(this,Enquire.class);
th.setContent(intent);


but then how can I use "R.id.tbenquire"?

Thanks in advance..
Posted

1 solution

If you want this, you can set the layout in your activitys onCreate() using setContentView(), just as you would with any other activity.

This does not seem to be the recommended way of using a tab view though, for two reasons.
1. The overhead of creating multiple activities.
2. Problems when passing data between the tabs if they have dependencies.
The recommended approach is to use a single activity with multiple views. Note, you can still design your code to have one class for each view, if that's what you want, to separate behavior.

If the two points above is not presenting a problem for you, then go ahead with multiple activities if you feel that is easier. I did that in the past, but realized the limitations of passing state around. Now I alway use multiple views and a single activity.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900