I have an app that has 2 tabs. Each tab loads an xml file (fairly large, maybe 400 item rss file).
By default the tab doesn't get the xml until it's clicked on. I simply wanted a way to load it all when the app is first opened.
Here is the main view:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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
// Audio Feed
intent = new Intent().setClass(this, AudioFeed.class);
spec = tabHost.newTabSpec("audio").setIndicator("",
res.getDrawable(R.drawable.ic_tab_audio))
.setContent(intent);
tabHost.addTab(spec);
// Video Feed
intent = new Intent().setClass(this, VideoFeed.class);
spec = tabHost.newTabSpec("video").setIndicator("",
res.getDrawable(R.drawable.ic_tab_video))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0); //todo: remember what tab user was last on
}
I use TabHost.setCurrentTab(x) or TabHost.setCurrentTabByTag(x).
mTabHost.setCurrentTab(1);
mTabHost.setCurrentTab(0);
For Initialization I use TabHost.OnTabChangeListener.
private OnTabChangeListener mOnTabChangeListener = new OnTabChangeListener() {
#Override
public void onTabChanged(String tag) {
if (FBIntent.EXTRA_XX.equals(tag)) {
// Current tab is xx.
... if xx not init -> ...
} else if (FBIntent.EXTRA_YY.equals(tag)) {
// Current tab is yy.
...
}
}
};
In this same method do whats the other tabs do. I mean what the other do when you click them. So you have the same thing for all tabs. Don't forget to include this same tab in the OnTabChanged() method and do the same operations again, otherwise you'll get nothing when this tab is clicked again.
Related
What i am trying to do::
BreakfastLunchDinnerIndividualListOfItems.java
public class BreakfastLunchDinnerIndividualListOfItems extends TabActivity implements OnTabChangeListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.breakfast_lunch_dinner_individual_list_of_items);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
String REST = getTabHost().toString();
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, BLD_IndividualListOfItems_Starters.class);
//intent.putExtra("Starters", REST);
spec = tabHost.newTabSpec("Starters").setIndicator("Starters").setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, BLD_IndividualListOfItems_MainCourse.class);
//intent.putExtra("MainCourse", REST);
spec = tabHost.newTabSpec("MAIN_COURSE").setIndicator("Main Course").setContent(intent);
tabHost.addTab(spec);
}
public void onTabChanged(String arg0)
{
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(),arg0, Toast.LENGTH_LONG).show();
}
}
I tried with String REST = getTabHost().toString(); problem is that
on click of tab i am not able to send perticular tabtext to the
activity that the tab launches
I know tab activity is depricated, i am just learning
How to resolve this, hope i am clear
spec = tabHost.newTabSpec("Starters").setIndicator("Starters").setContent(intent);
spec = tabHost.newTabSpec("MAIN_COURSE").setIndicator("Main Course").setContent(intent);
here Starters and Main Course are the titles of tabs. The easiest way to send this strings to bounded activities is to send them throught intents which are bounded to corresponding
tabSpecs.
String TAB_TITLE="Starters";
Intent intent = new Intent().setClass(this, BLD_IndividualListOfItems_Starters.class);
Bundle bundle =new Bundle();
bundle.putString("key_title", TAB_TITLE);
intent.putExtras(bundle);
spec = tabHost.newTabSpec("Starters").setIndicator(TAB_TITLE).setContent(intent);
tabHost.addTab(spec);
And here is how you can get this string in BLD_IndividualListOfItems_Starters activity :
protected void onCreate(Bundle savedInstanceState) {
...
String title=getIntent().getExtras().getString("key_title");
}
But as Raghunandan says, it's better to use fragments instead of depracated TabActivity.
EDITED:
if you want to send text to corresponding activity dinamically, i mean exactly after tab is changed - you can:
create and broadcast custom intent with your string. (CUSTOM_INTENT_EXAMPLE)
in BLD_IndividualListOfItems_Starters activity register BroadcastReciever which will catch your custom intent and take text from it. (BROADCAST_RECIEVER_FROM_ACTIVITY_EXAMPLE)
You can't directly access tab text from BLD_IndividualListOfItems_Starters, because TabActivity and BLD_IndividualListOfItems_Starters are two different activities.
But you can send data between activities via bundles, static fields, singletons etc. Here is link to docs
http://developer.android.com/guide/faq/framework.html#3
I'm trying to do a simple tab app in android with four tabs tabs. My problem is that when I want to show icon and indicator, it just display only text indicator, i want to display drawable and indicator text on tabhost, this is my code :
#Override
protected void onCreate(Bundle savedInstanceState) {
//hide title bar
BasicDisplaySettings.toggleTaskBar(SimasCardMainActivity.this, false);
//show status bar
BasicDisplaySettings.toggleStatusBar(SimasCardMainActivity.this, true);
super.onCreate(savedInstanceState);
setContentView(R.layout.simascard);
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
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, TerbaruSimasCard.class);
spec = tabHost.newTabSpec("Terbaru").setIndicator("Terbaru",
res.getDrawable(R.drawable.menu_terbaru))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MerchantSimasCard.class);
spec = tabHost.newTabSpec("Merchant").setIndicator("Merchant",
res.getDrawable(R.drawable.menu_merchant))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TentangSimasCard.class);
spec = tabHost.newTabSpec("Tentang").setIndicator("Tentang",
res.getDrawable(R.drawable.menu_tentang))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, FaqSimasCard.class);
spec = tabHost.newTabSpec("FAQ").setIndicator("FAQ",
res.getDrawable(R.drawable.menu_faq))
.setContent(intent);
tabHost.addTab(spec);
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++){
tabHost.getTabWidget().getChildAt(i).setPadding(0,0,0,0);
tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundDrawable(null);
}
tabHost.setCurrentTab(0);
}
// #Override
public void onBackPressed() {
finish();
}
when i use
spec = tabHost.newTabSpec("Tentang").setIndicator("",
res.getDrawable(R.drawable.menu_tentang))
.setContent(intent);
it will show an icon, but when i add text on setIndicator such as setIndicator("Tentang") it show only indicator text on tabhost, i don't know where is something wrong with my code,I've tried to get increase the tab height, but it doesn't work, i hope someone can help me to solve my problem. thank you
Remove this tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundDrawable(null);
and replace it with
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.menu_tentang);
i have done it like this and it is working fine
Resources ressources = getResources();
TabHost tabHost = getTabHost();
Intent addfriend = new Intent().setClass(this, yourclass1.class);
Intent mailfriend = new Intent().setClass(this, yourclass2.class);
tabHost.addTab(tabHost.newTabSpec("child1").setIndicator("your tag",getResources().getDrawable(R.drawable.youpng)).setContent(addfriend));
tabHost.addTab(tabHost.newTabSpec("child1").setIndicator("your tag",getResources().getDrawable(R.drawable.yourpng)).setContent(mailfriend));
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(getResources().getColor(R.color.anycolor));
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.yourpng);
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(getResources().getColor(R.color.anycolor));
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.youpng);
The question is: I have a TabHost with 4 tabs (see code below) and I got a Button in MainMenuActivity class. The Button is set up with a OnClickListener and if it is clicked I want it to go to the second tab. I have tried with setCurrentTab(1) but that just messed the project up. What can I do?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTabs() ;
}
private void setTabs()
{
addTab("Home", R.drawable.tab_home, MainMenuActivity.class);
addTab("Calculate", R.drawable.tab_search, SpinnerClass.class);
addTab("Search", R.drawable.tab_home, ScrollView1.class);
addTab("Premium", R.drawable.tab_search, ScrollView2.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
tabHost.setCurrentTab(index) is the right way to go. What's the problem when you use it?
"setCurrentTab(int) opens the tab to be displayed by default, specified by the index position of the tab."
I have tabhost in my application(2 tabs),when i select tab2 the content has been dislayed has a list view ,again i have list click event here to move further another list view,here the 2tab already in selected mode and it's focus not been changed,when i click the 2tab again i need it to reload its content has loaded initially with first list view content.how can i get it.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("OPT")
.setContent(new Intent(this, TabGroup1Activity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("EDIT")
.setContent(new Intent(this, TabGroup2Activity.class)));
tabHost.setCurrentTab(1);
}
You could also re-issue another intent to refresh the tab
try public void onResume() {} method
I have a TabHost with two tabs. I want to know when the user clicks on either tab. How can I achieve this?
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
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Main.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("watchlist").setIndicator("Watchlist",res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ARActivity.class);
spec = tabHost.newTabSpec("trending").setIndicator("Trending",res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
I think you want to use an onTabChangeListener (http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html) and pass that to the TabHost. See http://developer.android.com/reference/android/widget/TabHost.html#setOnTabChangedListener%28android.widget.TabHost.OnTabChangeListener%29
You can effectively get notified every time someone switches a tab.