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
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);
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 want to implement the idea that my every tab would have a separate activity. Here is the code where i am create tabs:
public class MainTabActivity extends SherlockFragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab_activity);
try {
TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
Resources res = getResources();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, stackA.class);
spec = mTabHost
.newTabSpec("tab1")
.setIndicator("Tab 1",
res.getDrawable(R.drawable.ic_launcher))
.setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(this, stackB.class);
spec = mTabHost
.newTabSpec("tab2")
.setIndicator("Tab 2",
res.getDrawable(R.drawable.ic_launcher))
.setContent(intent);
mTabHost.addTab(spec);
mTabHost.setCurrentTab(0);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
I am getting an exception:
java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
So maybe someone know solution if i want to extened FragmentsActivity as TabActivity is depricated. Thanks :)
Extend ActivityGroup instead of Activity and instead of mTabHost.setup() use mTabHost.setup(getLocalActivityManager());
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.