I'm trying to store the index of the currently selected tab in onSaveInstanceState so I can restore it. However the getCurrentTab apparantely gives me back the String I used in the etTabHost().newTabSpec, which I find a bit weird since the documentation says it returns an int and setCurrentTab also taking an int.
Does anyone know how I can get the index of my currently selected tab so I can restore it?
you are on the right way, use setOnTabChangedListener to get your selected tab.
public class MainActivity extends TabActivity {
static TabHost mytabs;
mytabs = getTabHost();
mytabs.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
Log.i("***Selected Tab", "Im currently in tab with index::" + mytabs.getCurrentTab());
}
});
...
...
...
You can use getCurrentTab() that returns index of tab start from 0.
Use tabHost.getCurrentTab() to get Tab ...
tabHost= getTabHost();
tabHost.addTab(tab0); // TabSpec tab0=tabHost.newTabSpec(...
tabHost.addTab(tab1); // TabSpec tab1=tabHost.newTabSpec
int current = tabHost.getTabHost() ;
Related
I have a tabActivity where I am adding tabs on runtime. So I think this is the usual code to do that:
_tabSpec = TabHost.newTabSpec("More");
_tabSpec.setIndicator("", Resources.GetDrawable(Resources.Drawable.myIcon).SetContent(intent);
TabHost.AddTab(_tabSpec);
Now the thing is, I have defined an options menu and I want to pop that up when the user clicks on the 'More' tab. I don't know how to do that. I tried not setting a content on that tab and simply use the OpenOptionsMenu() to pop it, but it doesn't seem to work.
Any clue how to achieve that?
P.S.: This is a C# code written in Xamarin. It might not look like the native java-android code, but its almost the same.
Alright, here's an answer to what I was trying to do.
Motive: Add a 'More' tab to the existing TabActivity. When user clicks it, open some kind of a PopUpWindow or ContextMenu, etc.
Steps to do that:
1) Create a tabSpec and add that tab to the TabHost as shown in the question.
2) Now you need to take in this last added tab as a View type variable. Do this by..
View v = TabWidget.GetChildAt(index)
Remember index of tabs starts from 0
3) Now on the onCreate() method of your main activity (one that holds the TabActivity) add an onTouchListener() (I use C#, so I added v.Click+=myFunction()) and write your code of the PopupWindow or ContextMenu or whatever you want to do there.
private String lastTab = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TabHost tabHost = getTabHost();
TabHost.TabSpec tabSpec;
tabSpec = tabHost.newTabSpec("tab1");
tabSpec.setIndicator("Tab 1");
tabSpec.setContent(new Intent(this, OneActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("tab2");
tabSpec.setIndicator("Tab 2");
tabSpec.setContent(new Intent(this, TwoActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("more");
tabSpec.setIndicator("More");
tabSpec.setContent(new Intent(this, OneActivity.class));
tabHost.addTab(tabSpec);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
if (tabId.equalsIgnoreCase("more")){
openOptionsMenu();
tabHost.setCurrentTabByTag(lastTab);
}
else lastTab = tabId;
}
});
}
I am Having the small doubt on tab-widget.
How i can get the position of tab widget dynamically?
Assume that there are of 20 tabs,when i clicked on the 10 tab i need to display the position of that tab.
use OnTabChangeListener interface to TabHost
http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html
onTabChanged(String tabId)
this will give you String id of selected tab
and use setCurrentTabByTag(String tag) of TabHost to get current Tab
or
http://developer.android.com/reference/android/widget/TabHost.html
String getCurrentTabTag() will give currentTab Tag string
int getCurrentTab() will give you currentTab int position
MainTabHost tabparent;
tabparent = (MainTabHost) getParent();
tabparent.getTabHost().getCurrentTab()
Assuming that you want to access it from a child activity of the tab.
In your TabActivity implement OnTabChangeListener
then set the listener for the TabHost mTabHost.setOnTabChangedListener(this);
#Override
public void onTabChanged(String tabId) {
Log.i("selected tab index", "Current index - "+mTabHost.getCurrentTab());
}
The app I am building has a tabhost with three tabs. The goal is to update the display in the new tab when the switch takes place. I have the following tab change listener set up in the main activity that built the tab host.
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
//Now what?
}
});
The question is, How do I take the tabId given and use it to call a method in that tab?
Edit
To clarify a bit: when you create an Intent for a new tab you specify an Activity Class, an object of which, presumably, is created to handle setup and management of that tab.
intent = new Intent().setClass(this, Setup.class);
spec = tabHost.newTabSpec("setup").setIndicator("",
res.getDrawable(R.drawable.tab_setup))
.setContent(intent);
tabHost.addTab(spec);
What I am looking for with this question is how to get a reference to that object? In the example above, the Setup class is instantiated to handle the “Setup Tab”.
To restate my question now: How do I, from the OnTabChangeListener, call a method in the Setup class?
Try to use
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
//Now what?
Object.refreshDrawableState();
}
});
Where Object = any display Object.
As it turns out the solution to the problem is closer to home. Rather than attaching code to the TabHost, the solution involves getting into the Life-Cycle of the activity by overriding the onPause and onResume methods.
#Override
public void onResume() {
super.onResume();
// Update your UI here.
}
I have created two tabs in my Activity using the below code. This is working perfectly fine. When I click on the first tab I see its contents and the other one shows its content when clicked on it.
How ever I want to set a variable value to true or false based on the Tab selected. But I dont know how to get the tab click for this tab. Can you please help me on this.
The code :
tabHost.setup();
TabSpec ts = tabHost.newTabSpec("Tab1");
ts.setIndicator("", getResources().getDrawable(R.drawable.tab1_content));
ts.setContent(R.id.tab1Layout);
tabHost.addTab(ts);
TabSpec ts1 = tabHost.newTabSpec("Tab2");
ts1.setIndicator("", getResources().getDrawable(R.drawable.tab2_content));
ts1.setContent(R.id.tab2Layout);
tabHost.addTab(ts1);
Add onTabchangedListener to the tabhost and using selectedTab Value manage whatsoever you want to manage.
selectedTab value will be = 0 for first tab and rest so on
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
int selectedTab = tabHost.getCurrentTab() // selected
}
});
Hope it helps :)
I have a TabHost with 4 tabs. I need only the selected tab activity to be available on the stack. When user changes the tab, how to finish the activity under previous tab. I tried the following code. Here showing code for first tab. It similar for remaining tabs:
spec = tabHost.newTabSpec("tab1").setIndicator("Tab1",
res.getDrawable(R.drawable.ic_tab_tab1))
.setContent(new Intent(this, Tab1.class)
.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP));
But the above code is deleting the Tab1 Activity on stack/heap only when the user comes again to that tab but not when user changes to new tab.
I've had a look at this, what is your reason for this?
How do you know what's on the stack? Are you depending on onDestroy() or something?
I haven't got a full answer but you can see which tab is active:
Let your class implement OnTabChangeListener
public class YourClass extends TabActivity implements OnTabChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Load all your normal objects as well as TabHost
// make your tabhost listen for tab changes
mTabHost.setOnTabChangedListener(this);
}
#Override
public void onTabChanged(String tabId) {
int currentTabNumber = mTabHost.getCurrentTab();
for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
{
if(i != currentTabNumber){
System.out.println("I'm not a currently active tab");
}
}
}
}