Changing tabs in android
- by Pavel
Hi guys. Can someone please tell me how to change tab by clicking on element INSIDE the tab? I already tried it with global data. The code looks like this:
public class Tabs extends TabActivity {
int tabNumber = 0;
private TabHost tabHost;
int returnedTabNumber = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Tribocracy.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Areas.class);
spec = tabHost.newTabSpec("areas").setIndicator("Areas",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Settings.class);
spec = tabHost.newTabSpec("settings").setIndicator("Settings",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(tabNumber);
}
protected void onResume() {
super.onResume();
GlobalData globalData = ((GlobalData)getApplicationContext());
returnedTabNumber = globalData.getTabNumber();
tabHost.setCurrentTab(returnedTabNumber);
}
}
The global adapter looks like this:
public class GlobalData extends Application {
//----------------------------------------------------
private int Point1; //define the vars here
private int Point2; //define the vars here
private int Point3; //define the vars here
private int Point4; //define the vars here
private int Point5; //define the vars here
private int Point6; //define the vars here
private int tabNumber;
public int getTabNumber() //getter of the value
{
return tabNumber;
}
public int setTabNumber(int number) //setter of the value
{
tabNumber = number;
return tabNumber;
}
}
Now when I'm trying to change tab in my ListActivity tab by clicking on one of the items it doesn't do anything and stays on the ListActivity tab. Perhaps I shouldn't use onResume() here. Basically I want to go to first tab when I click on one of the items in the list. Please help!