How to get tab click in Android? - android

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 :)

Related

Change Tab color depending on which tab is selected

I have an app with a MainActivity that extends TabActivity (i know it's deprecated but too much to do to change whole app).
So in my app I use tabhost to create 3 tabs like this:
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
firstTabSpec.setIndicator("tab1").setContent(
new Intent(this, tab1.class));
secondTabSpec.setIndicator("tab2").setContent(
new Intent(this, tab2.class));
thirdTabSpec.setIndicator("tab3").setContent(
new Intent(this, tab3.class));
/* Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);
//Changing the tabs text color on the tabs
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#ffffff"));
}
// remove divider
tabHost.getTabWidget().setDividerDrawable(null);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#90a4ae"));
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#607d8b"));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#607d8b"));
So my code creates the 3 tabs that link to 3 different activities and sets the color of the tabs. First tab has at first load a different color than the other two.
I want the color of the tabs to change depending on which one is selected.
So when I press the second tab i want the first to get #607d8b color and the second to get #90a4ae.
Same for the third one.
Tried to implement a OnTabChangeListener but couldn't get it to work.
Tried to to this:
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#90a4ae"));
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#607d8b"));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#607d8b"));
with changed colors inside each loaded tab activity but I get error that it can't resolve tabhost (as it should, since it's defined in MainActivity.
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#54C4C6")); // unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#114C5A")); // selected
}
});

Opening OptionsMenu on the touch of a tab of the TabActivity

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;
}
});
}

android tab activity go to previous tab

I have an activity with five tabs. Everything looks okay when I go from tab 1 to tab 2 or tab 3. How can I go back programatically from tab 2 to tab 1?
Intent myIntent = new Intent(this, Tab1.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
This is not working properly because it starts activity 1 without any tab.
When going from tab 1 to tab 2 I can both see tab 1 and tab 2 (current tab activated). But when going from tab 2 to tab 1, both tab 1 and tab 2 disappear from the activity. What could cause this?
This will surely help you.
TabHost tabHost = (TabHost) getParent().findViewById(android.R.id.tabhost);
tabHost.setCurrentTab(1);
OR you can refer to this link
How to programmatically switch tabs using buttonclick in Android
Thanks :)
just use finish() method
public void onClick(View v)
{
finish();
startActivity(new Intent(Activity2.this, Activity1.class));
}
I don't know about the Intent.FLAG_ACTIVITY_CLEAR_TOP, never needed that, but the mentioned effect of loosing your tabs is produced by calling startActivity() from your TabHost, not one of your tabs. If that's the case, move the call there and your tabs should stay.
I have a similar situation but seems none of the answers help. so, I post my solution here:
// tab selection history, each tab has a tag which is a string
private List<String> tabIdHistory = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstanceState);
// this layout contains TabHost and TabWidget
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
tabIdHistory.remove(tabId); // ensure uniqueness
tabIdHistory.add(tabId);
}
});
// continue your tab initialisation, such as
// tabHost.addTab(tabHost.newTabSpec(TAG)
// .setContent(...).setIndicator(...));
}
#Override
public void onBackPressed() {
if (tabIdHistory.size() > 1) {
// pop the current last item, we want the second last
tabIdHistory.remove(tabIdHistory.size() - 1);
tabHost.setCurrentTabByTag(tabIdHistory.get(tabIdHistory.size() - 1));
} else {
super.onBackPressed();
}
}
If use select tab#1, tab#3, tab#2, tab#1, then the back stack is "3, 2, 1" and app will exit to main screen if user press back button three times. If you want to keep full history, comment out this line:
tabIdHistory.remove(tabId);

TabHost , TabSpec - onclick implementation

i am working with TabHost , TabSpec .i want to do some actions(like onClick) when TabSpec is clicked. has anyone tried this i am able to go to another activity with "home.setIndicator("Home")...." is it possible
Use http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html
Example
mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if(TAB_1.equals(tabId)) {
//Change first image
}
if(TAB_2.equals(tabId)) {
//chnage second image ...so on
}
}});
If you follow this tutorial, you'll see that it's possible to change the tab image (to show that the tab is being selected or not) by defining XML file in res/drawable/

Get index of selected tab in tabHost

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() ;

Categories

Resources