Reputation: 954
In my activity I have a TabHost
.
I have 3 tabs and 3 activities for them. How can I start the corresponding activity when I click on a tab?
At the moment all three activities starts...
If I run this code, every activity (connected_upload
, connected_download
, connected_search
)
runs the "onCreate
" method.
How can I start those activities manually? I mean I like to start the activity only when I click on the corresponding tab...
public class connected extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connected);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, connected_upload.class);
spec = tabHost.newTabSpec("Hoch").setIndicator("Hoch",res.getDrawable(R.drawable.freeftp)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, connected_download.class);
String str_path = getIntent().getStringExtra("path");
String str_profil = getIntent().getStringExtra("profil");
String str_server = getIntent().getStringExtra("server");
String str_port = getIntent().getStringExtra("port");
String str_user = getIntent().getStringExtra("user");
String str_password = getIntent().getStringExtra("pw");
intent.putExtra("path", str_path);
intent.putExtra("profil", str_profil);
intent.putExtra("server", str_server);
intent.putExtra("port", str_port);
intent.putExtra("user", str_user);
intent.putExtra("pw", str_password);
spec = tabHost.newTabSpec("Herunter").setIndicator("Herunter",res.getDrawable(R.drawable.freeftp)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, connected_search.class);
spec = tabHost.newTabSpec("Search").setIndicator("Search",res.getDrawable(R.drawable.freeftp)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
//Button Connect Server
Button cmd_mainsite = (Button)findViewById(R.id.but_connected_mainsite);
cmd_mainsite.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
finish();
Intent Intent_mainsite = new Intent(connected.this, Login.class);
startActivity(Intent_mainsite);
}
});
}
}
Upvotes: 0
Views: 1809
Reputation: 1271
Use This Code As per your requirement
MainActivity.java
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost th = getTabHost();
th.addTab(th.newTabSpec("").setIndicator("tab1",
res.getDrawable(R.drawable.icon)).
setContent(new Intent(this, firsttab.class)));
th.addTab(th.newTabSpec("").setIndicator("tab2")
.setContent(new Intent(this, secondtab.class)));
th.addTab(th.newTabSpec("").setIndicator("tab3")
.setContent(new Intent(this, thirdtab.class)));
th.setCurrentTab(1);
}
}
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="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/linearLayout1"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent"
android:layout_weight="1">
</FrameLayout>
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs">
</TabWidget>
</LinearLayout>
</TabHost>
firsttab.java
public class firsttab extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
TextView tv = new TextView(this);
tv.setText("HI");
tv.setTextSize(25);
setContentView(tv);
}
}
secondtab.java
public class secondtab extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
TextView tv = new TextView(this);
tv.setText("Hello");
tv.setTextSize(25);
setContentView(tv);
}
}
thirdtab.java
public class thirdtab extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
TextView tv = new TextView(this);
tv.setText("How Are U?");
tv.setTextSize(25);
setContentView(tv);
}
}
Upvotes: 2
Reputation: 1271
public class connected extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connected);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabSpec spec1= tabHost.newTabSpec("")
.setIndicator("Hoch",res.getDrawable(R.drawable.freeftp))
.setContent(new Intent(this, connected_upload.class));
tabHost.addTab(spec1);
TabSpec spec2 = tabHost.newTabSpec("")
.setIndicator("Herunter",res.getDrawable(R.drawable.freeftp))
.setContent(new Intent(this, connected_download.class));
String str_path = getIntent().getStringExtra("path");
String str_profil = getIntent().getStringExtra("profil");
String str_server = getIntent().getStringExtra("server");
String str_port = getIntent().getStringExtra("port");
String str_user = getIntent().getStringExtra("user");
String str_password = getIntent().getStringExtra("pw");
intent.putExtra("path", str_path);
intent.putExtra("profil", str_profil);
intent.putExtra("server", str_server);
intent.putExtra("port", str_port);
intent.putExtra("user", str_user);
intent.putExtra("pw", str_password);
tabHost.addTab(spec2);
TabSpec spec3= tabHost.newTabSpec("")
.setIndicator("Search",res.getDrawable(R.drawable.freeftp))
.setContent(new Intent(this, connected_search.class));
tabHost.addTab(spec3);
tabHost.setCurrentTab(1);
//Button Connect Server
Button cmd_mainsite = (Button)findViewById(R.id.but_connected_mainsite);
cmd_mainsite.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
finish();
Intent Intent_mainsite = new Intent(connected.this, Login.class);
startActivity(Intent_mainsite);
}
});
}
}
Upvotes: 0
Reputation: 22291
Write below code for open activity on click of particular tab and if you have any query regarding that then tell me.
TabActivity.java:-
public class TabActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_screen);
TabHost tabHost = getTabHost();
Intent intent = new Intent().setClass(this, ActivityStack.class);
TabHost.TabSpec spec = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.home));
spec.setContent(intent);
tabHost.addTab(spec);
Intent intent1 = new Intent().setClass(this, ActivityStack.class);
TabHost.TabSpec spec1 = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.invoice));
spec1.setContent(intent1);
tabHost.addTab(spec1);
tabHost.setCurrentTab(0);
}
}
Upvotes: 0