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);
Related
I set tab without TabActivity,
so my tabHost always has an error.
please tell me how to do.
thanks.
private void setupTab(Class<?> ccls, String name, String label,
Integer iconId) {
Intent intent = new Intent().setClass(this, ccls);
View tab = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
ImageView image = (ImageView) tab.findViewById(R.id.icon);
TextView text = (TextView) tab.findViewById(R.id.text);
if (iconId != null) {
image.setImageResource(iconId);
}
text.setText(label);
TabSpec spec = tabHost.newTabSpec(name).setIndicator(tab)
.setContent(intent);
tabHost.addTab(spec);
}
i read Android Tab-Host
to earn
It can't build.
i'm run error by tabHost cannot be resolved.
I want to create an app consisting of two tabs each of which operates their own webview. I can create tabs but can't manage their webviews.
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
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, fbActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("fb").setIndicator("fb",
res.getDrawable(R.drawable.ic_tab_fb))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, google.class);
spec = tabHost.newTabSpec("google").setIndicator("google",
res.getDrawable(R.drawable.ic_tab_google))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
first create tabactivity class
public class tabviews extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
TabHost tab = getTabHost();
TabSpec tab1 = (TabSpec) tab.newTabSpec("tb1");
TabSpec tab2 = (TabSpec) tab.newTabSpec("tb2");
tab1.setIndicator("Customer", null).setContent(
new Intent(this, webview1.class));
tab2.setIndicator("Item", null).setContent(
new Intent(this, webview2.class));
tab.addTab(tab1);
tab.addTab(tab2);
}
}
then create two activity class say webview1.class and webview2.class
public class webview1 extends Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
WebView v = (WebView) findViewById(R.id.webView1);
v.loadUrl("http://www.google.com");
then create another class with same code. enter all three classes in manifest.xml
I am making tabs in android. I am using TabHost to achieve this. I am getting red line under getTabHost() method on this line TabHost tabHost = getTabHost();. I am not sure why am I getting this. How can I make this example work?
Here is my complete code
public class TabActivity extends Activity {
//TabHost mTabHost;
/** 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 tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
//Artist Tab
intent = new Intent(this, Artist.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
//Songs
intent = new Intent(this, Songs.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
//Albums
intent = new Intent(this, Album.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}
}
you need to extend MyTabActivity not simple activity
try
public class MyTabActivity extends TabActivity
intead of
public class MyTabActivity extends Activity
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.
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.