subashprasanna
Lurker
I am new to android field. I am sorry to post my problem if this problem have been posted . I searched solution for my problem but i could not find. if already post is existing for my problem kindly refer me.
My problem is, i have 4 tabs in android tab application namely Day,Week,Month and Year. i am using different layout for each tab. It shows content correctly in all the tabs, But i need to get current tab index or reference name which i have given for each tab in tab widget. i have tried this code. but could not find solution..help me out please. Thanks in advance.
my code is
[
package com.android.tabwidget;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
public class HelloTabWidgetActivity extends TabActivity {
TabHost tabhost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabhost = getTabHost(); //The activity tab host
TabHost.TabSpec tapspec; //Reusable tab host for each activity
Intent intent; //Reusable intent for each tab
// Create an intent to launch an activity for the tab for Day
intent = new Intent().setClass(this, DayActivity.class);
tapspec = tabhost.newTabSpec("day").setIndicator("Day").setContent(intent);
tabhost.addTab(tapspec);
// Create an intent to launch an activity for the tab for Week
intent = new Intent().setClass(this, WeekActivity.class);
tapspec = tabhost.newTabSpec("week").setIndicator("Week").setContent(intent);
tabhost.addTab(tapspec);
// Create an intent to launch an activity for the tab for Month
intent = new Intent().setClass(this, MonthActivity.class);
tapspec = tabhost.newTabSpec("month").setIndicator("Month").setContent(intent);
tabhost.addTab(tapspec);
// Create an intent to launch an activity for the tab for Year
intent = new Intent().setClass(this, YearActivity.class);
tapspec = tabhost.newTabSpec("year").setIndicator("Year").setContent(intent);
tabhost.addTab(tapspec);
tabhost.setCurrentTab(2);
int noOfTabs = tabhost.getTabWidget().getChildCount();
for (int i = 0; i < noOfTabs; i++) {
tabhost.getTabWidget().getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
String currentSelectedTag = getTabHost().getCurrentTabTag();
String currentTag = (String)v.getTag();
if(currentSelectedTag.equalsIgnoreCase(currentTag)){
getTabHost().setCurrentTabByTag(currentTag);
String newSelectedTabTag = getTabHost().getCurrentTabTag();
if(newSelectedTabTag.toLowerCase().indexOf("tab1")!=-1){
Toast.makeText(getApplicationContext(), " tab1 : currentSelectedTag: " + currentSelectedTag + " currentTag: " + currentTag, Toast.LENGTH_SHORT);
}else if(newSelectedTabTag.toLowerCase().indexOf("tab1")!=-1){
Toast.makeText(getApplicationContext(), " tab2 : currentSelectedTag: " + currentSelectedTag + " currentTag: " + currentTag, Toast.LENGTH_SHORT);
}else if(newSelectedTabTag.toLowerCase().indexOf("tab3")!=-1){
Toast.makeText(getApplicationContext(), " tab3 : currentSelectedTag: " + currentSelectedTag + " currentTag: " + currentTag, Toast.LENGTH_SHORT);
}
return true;
}
}
return false;
}
});
}
//another way
/* tabhost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
//Toast.makeText(getApplicationContext(), tabhost.getCurrentTab(), Toast.LENGTH_SHORT);
Log.i("selected tab", "index "+tabhost.getCurrentTab());
}
});*/
//another way
/*getTabWidget().getChildAt(getTabHost().getCurrentTab()).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), (CharSequence)getTabWidget().getChildAt(getTabHost().getCurrentTab()), Toast.LENGTH_SHORT);
}
});*/
}
}
]
My problem is, i have 4 tabs in android tab application namely Day,Week,Month and Year. i am using different layout for each tab. It shows content correctly in all the tabs, But i need to get current tab index or reference name which i have given for each tab in tab widget. i have tried this code. but could not find solution..help me out please. Thanks in advance.
my code is
[
package com.android.tabwidget;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
public class HelloTabWidgetActivity extends TabActivity {
TabHost tabhost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabhost = getTabHost(); //The activity tab host
TabHost.TabSpec tapspec; //Reusable tab host for each activity
Intent intent; //Reusable intent for each tab
// Create an intent to launch an activity for the tab for Day
intent = new Intent().setClass(this, DayActivity.class);
tapspec = tabhost.newTabSpec("day").setIndicator("Day").setContent(intent);
tabhost.addTab(tapspec);
// Create an intent to launch an activity for the tab for Week
intent = new Intent().setClass(this, WeekActivity.class);
tapspec = tabhost.newTabSpec("week").setIndicator("Week").setContent(intent);
tabhost.addTab(tapspec);
// Create an intent to launch an activity for the tab for Month
intent = new Intent().setClass(this, MonthActivity.class);
tapspec = tabhost.newTabSpec("month").setIndicator("Month").setContent(intent);
tabhost.addTab(tapspec);
// Create an intent to launch an activity for the tab for Year
intent = new Intent().setClass(this, YearActivity.class);
tapspec = tabhost.newTabSpec("year").setIndicator("Year").setContent(intent);
tabhost.addTab(tapspec);
tabhost.setCurrentTab(2);
int noOfTabs = tabhost.getTabWidget().getChildCount();
for (int i = 0; i < noOfTabs; i++) {
tabhost.getTabWidget().getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
String currentSelectedTag = getTabHost().getCurrentTabTag();
String currentTag = (String)v.getTag();
if(currentSelectedTag.equalsIgnoreCase(currentTag)){
getTabHost().setCurrentTabByTag(currentTag);
String newSelectedTabTag = getTabHost().getCurrentTabTag();
if(newSelectedTabTag.toLowerCase().indexOf("tab1")!=-1){
Toast.makeText(getApplicationContext(), " tab1 : currentSelectedTag: " + currentSelectedTag + " currentTag: " + currentTag, Toast.LENGTH_SHORT);
}else if(newSelectedTabTag.toLowerCase().indexOf("tab1")!=-1){
Toast.makeText(getApplicationContext(), " tab2 : currentSelectedTag: " + currentSelectedTag + " currentTag: " + currentTag, Toast.LENGTH_SHORT);
}else if(newSelectedTabTag.toLowerCase().indexOf("tab3")!=-1){
Toast.makeText(getApplicationContext(), " tab3 : currentSelectedTag: " + currentSelectedTag + " currentTag: " + currentTag, Toast.LENGTH_SHORT);
}
return true;
}
}
return false;
}
});
}
//another way
/* tabhost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
//Toast.makeText(getApplicationContext(), tabhost.getCurrentTab(), Toast.LENGTH_SHORT);
Log.i("selected tab", "index "+tabhost.getCurrentTab());
}
});*/
//another way
/*getTabWidget().getChildAt(getTabHost().getCurrentTab()).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), (CharSequence)getTabWidget().getChildAt(getTabHost().getCurrentTab()), Toast.LENGTH_SHORT);
}
});*/
}
}
]