TabHost do not match is parent width in HoneyComb Android 3.0 - android

I'm trying to make a TabHost on HoneyComb as if I was on 1.6+ but the fact is when I copy the code (which is the same for both HoneyComb and previous version), the TabHost do not match the screen width. I don't get it?
Using Halo theme on API 3.0 with the emulator.
Here's the xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</TabHost>
And here is my Activity :
package org.agetac;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class TabsActivity extends TabActivity {
private static final String TAB_SITAC = "tab_sitac";
private static final String TAB_SOEI = "tab_soei";
private static final String TAB_MOYEN = "tab_moyen";
private static final String TAB_TAB4 = "tab_tab4";
private static final String TAB_TAB5 = "tab_tab5";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
TabHost tHost = getTabHost();
TabHost.TabSpec spec;
Intent tabIntent;
// Initialize a TabSpec for each tab and add it to the TabHost
tabIntent = new Intent().setClass(this, SITACActivity.class);
spec = tHost.newTabSpec(TAB_SITAC).setIndicator(getString(R.string.sitac),
getResources().getDrawable(R.drawable.ic_tab_sitac))
.setContent(tabIntent);
tHost.addTab(spec);
tabIntent = new Intent().setClass(this, SOEIActivity.class);
spec = tHost.newTabSpec(TAB_SOEI).setIndicator(getString(R.string.soei),
getResources().getDrawable(R.drawable.ic_tab_soei))
.setContent(tabIntent);
tHost.addTab(spec);
tabIntent = new Intent().setClass(this, MoyenActivity.class);
spec = tHost.newTabSpec(TAB_MOYEN).setIndicator(getString(R.string.moyen),
getResources().getDrawable(R.drawable.ic_tab_moyen))
.setContent(tabIntent);
tHost.addTab(spec);
tabIntent = new Intent().setClass(this, Tab4Activity.class);
spec = tHost.newTabSpec(TAB_TAB4).setIndicator(getString(R.string.tab4),
getResources().getDrawable(R.drawable.ic_tab_tab4))
.setContent(tabIntent);
tHost.addTab(spec);
tabIntent = new Intent().setClass(this, Tab5Activity.class);
spec = tHost.newTabSpec(TAB_TAB5).setIndicator(getString(R.string.tab5),
getResources().getDrawable(R.drawable.ic_tab_tab5))
.setContent(tabIntent);
tHost.addTab(spec);
tHost.setCurrentTab(0);
}
}
Does anyone know why does TabHost behave like that with HoneyComb?

Thats normal behaviour and how it's planned by Google. The Tabs won't fill the whole Activity unless you write your own TabHost.

Related

Tab bar not showing in activity

I have a tab bar that I want to add to multiple activities. I have a TabController.java which looks like this
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabController extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabcontroller);
TabHost tabHost = getTabHost();
Intent intent;
intent = new Intent().setClass(this, Help.class);
TabSpec specHelp = tabHost.newTabSpec("Help").setIndicator("Help")
.setContent(intent);
intent = new Intent().setClass(this, Services.class);
TabSpec specServices = tabHost.newTabSpec("Services").setIndicator("Services")
.setContent(intent);
intent = new Intent().setClass(this, Inbox.class);
TabSpec specInbox = tabHost.newTabSpec("Inbox").setIndicator("Inbox")
.setContent(intent);
intent = new Intent().setClass(this, About.class);
TabSpec specAbout = tabHost.newTabSpec("About").setIndicator("About")
.setContent(intent);
intent = new Intent().setClass(this, More.class);
TabSpec specMore = tabHost.newTabSpec("More").setIndicator("More")
.setContent(intent);
tabHost.addTab(specHelp);
tabHost.addTab(specServices);
tabHost.addTab(specInbox);
tabHost.addTab(specAbout);
tabHost.addTab(specMore);
}
and a tabcontroller.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabhost" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_alignParentBottom = "true"
android:layout_height="wrap_content" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</RelativeLayout>
</TabHost>
</merge>
What I want to do with that is to add that layout to multiple activities. When I try and add the layout I use <include layout="#layout/tabcontroller"/>. When I run the project my tab bar does not appear on the screen.
How can I add this tab bar to my activities? PS. TabController.java is not my main activity
Try taking a look at this tutorial over at : http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android
One Thing i know ,maybe i am wrong, is that the Root Activity class must inherit TAbActivity and for you your TabController.java is not the main activity.
EDIT: THis is how i am getting Tabs which is not my Root Activity :
public class ListFragment extends Fragment implements OnTabChangeListener{
View v;
TabHost tabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.main_menu_libraries, container, false);
tabHost=(TabHost) v.findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("About",getResources().getDrawable(R.drawable.android));
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Blog",getResources().getDrawable(R.drawable.android));
TabSpec spec3=tabHost.newTabSpec("Tab 3");
spec3.setIndicator("Donate",getResources().getDrawable(R.drawable.android));
spec3.setContent(R.id.tab3);
TabSpec spec4=tabHost.newTabSpec("Tab 4");
spec4.setIndicator("Explore",getResources().getDrawable(R.drawable.android));
spec4.setContent(R.id.tab4);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
tabHost.addTab(spec4);
return v;
}
As in the code above i am using fragments . So i inflate my VIew with the menu_libraries.xml which contains the tabs and the tabwidgets.
Inside the OncreateView function i am defining and inflating the tabs as well.
Not the best of answers but i guess you could try that.

How to locate drawable folder in Android

According to this http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
developer page says, create a XML file which should holds the icons to indicate state of resource. But by default a new project is created, this Drawable folder does not exist but i will be having 4 diff folder indicate different resolution images.
I forcefully created a folder named Drawable and place the XML file and used in my application, but unfortunately my application throws exception saying resource is NULL. How to overcome this issue?
Where do we have to place the XML file?, should we make individual XML file and place in different resource folders? kindly advice me
Here is the image of folder structure.
If i use the ic_tab_artist, my app crashes. here is the full source
package simple.tab.proj;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TextView;
public class SimpleTabActivity extends TabActivity {
/** 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
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ArtistsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ArtistsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
public static class ArtistsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Artists tab");
setContentView(textview);
//setContentView(R.layout.main);
}
}
}
This is my Main.xml file
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
Here is my ic_artist_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- When selected, use grey -->
<item android:drawable="#drawable/ic_tab_artists_grey"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/ic_tab_artists_white" />
</animation-list>
I forcefully created a folder named Drawable and place the XML file and used in my application, but unfortunately my application throws exception saying resource is NULL. How to overcome this issue?
Android is case-sensitive. The directory needs to be res/drawable/, not Drawable.
should we make individual XML file and place in different resource folders?
One XML file in res/drawable/ should suffice, though the individual drawable resources pointed to by your StateListDrawable may need different versions for different densities.

Custom TabWidget Android Tab Indicator

Okay, so i am making an android app that has tabs, now my problem is that the tab widget isn't uniform across the diffrent android versions or devices.
I want to make it to be the same on any android this is my tab activity
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Cook extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cook_layout);
TabHost tabHost = getTabHost();
// Tab for Snacks
TabSpec snackspec = tabHost.newTabSpec("Snacks");
// setting Title and Icon for the Tab
snackspec.setIndicator("Snacks", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
Intent snacksIntent = new Intent(this, Cook_tab_snacks.class);
snackspec.setContent(snacksIntent);
// Tab for Mains
TabSpec mainspec = tabHost.newTabSpec("Mains");
mainspec.setIndicator("Mains", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
Intent mainsIntent = new Intent(this, Cook_tab_mains.class);
mainspec.setContent(mainsIntent);
// Tab for Desserts
TabSpec dessertspec = tabHost.newTabSpec("Desserts");
dessertspec.setIndicator("Desserts", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
Intent dessertsIntent = new Intent(this, Cook_tab_desserts.class);
dessertspec.setContent(dessertsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(snackspec); // Adding snacks tab
tabHost.addTab(mainspec); // Adding mains tab
tabHost.addTab(dessertspec); // Adding desserts tab
}
}
I also have my XML layout :
<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:background="#drawable/gradient_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
I made a new indicator xml which is like the main android tab indicator v4
I followed and searched alot of blogs , i couldn't find my answer ...
I really want to make the android tabs uniform across all android versions and to make the colors nice , since orange and yellow dont really fit with the color theme in my app
Help please!!!!
I cant seem to find a way to fix it...
Cheers
okay i found a solution.
here is the code:
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Cook extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cook_layout);
TabHost tabHost = getTabHost();
// Tab for Snacks
TabSpec snackspec = tabHost.newTabSpec("Snacks");
// setting Title and Icon for the Tab
snackspec.setIndicator(makeTabIndicator(getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
Intent snacksIntent = new Intent(this, Cook_tab_snacks.class);
snackspec.setContent(snacksIntent);
// Tab for Mains
TabSpec mainspec = tabHost.newTabSpec("Mains");
mainspec.setIndicator(makeTabIndicator( getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
Intent mainsIntent = new Intent(this, Cook_tab_mains.class);
mainspec.setContent(mainsIntent);
// Tab for Desserts
TabSpec dessertspec = tabHost.newTabSpec("Desserts");
dessertspec.setIndicator(makeTabIndicator( getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
Intent dessertsIntent = new Intent(this, Cook_tab_desserts.class);
dessertspec.setContent(dessertsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(snackspec); // Adding snacks tab
tabHost.addTab(mainspec); // Adding mains tab
tabHost.addTab(dessertspec); // Adding desserts tab
}
//making the tab view:
private View makeTabIndicator(Drawable drawable){
ImageView Tabimage = new ImageView(this);
LayoutParams LP = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);
LP.setMargins(1, 0, 1, 0);
Tabimage.setLayoutParams(LP);
Tabimage.setImageDrawable(drawable);
Tabimage.setBackgroundResource(R.drawable.tabview);
return Tabimage;
}}
I dont know weather or not i need the cook_layout anymore i'll see if i can remove it or leave it later... right now i just want to get it all working and later i'll come round by for a clean and tiding up
hope that helps you guys out there that stumble upon this question! cheers

Android: Moving tabs to bottom causes the app to crash

i use this tutorial http://developer.android.com/resources/tutorials/views/hello-tabwidget.html to create tabs which opens new intents. however, the tabs in the tutorials are located on the top, so I followed this solution here https://stackoverflow.com/a/2710404/301584 to move the tabs to bottom (basically i just move the TabWidget to be after FrameLayout in the xml file, modify the neccessary layout_height and add layout_weight, as suggested by the solution on the link). the problem is, my apps will always forced close when I did this. here's the error reported by logcat
01-09 04:30:09.838: ERROR/AndroidRuntime(336): FATAL EXCEPTION: main
01-09 04:30:09.838: ERROR/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geoflex.trymasak/com.geoflex.trymasak.TryMasaktTabActivity}: java.lang.ClassCastException: android.widget.FrameLayout
01-09 04:30:09.838: ERROR/AndroidRuntime(336): Caused by: java.lang.ClassCastException: android.widget.FrameLayout
01-09 04:30:09.838: ERROR/AndroidRuntime(336): at com.geoflex.trymasak.TryMasaktTabActivity.onCreate(TryMasaktTabActivity.java:34)
this is my full code
package com.geoflex.trymasak;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
public class TryMasaktTabActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable 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, Tab1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tabOne");
spec.setContent(intent);
spec.setIndicator("Tab One");
tabHost.addTab(spec);
// Squish the tab a little bit horizontally
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 40;
// Bump the text size up
LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
android.widget.TabWidget tw = (android.widget.TabWidget) ll.getChildAt(0);
RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
TextView lf = (TextView) rllf.getChildAt(1);
lf.setTextSize(20);
// Do the same for the other tabs
intent = new Intent().setClass(this, Tab2.class);
spec = tabHost.newTabSpec("tabTwo");
spec.setContent(intent);
spec.setIndicator("Tab Two");
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 40;
RelativeLayout rlrf = (RelativeLayout) tw.getChildAt(1);
TextView rf = (TextView) rlrf.getChildAt(1);
rf.setTextSize(20);
tabHost.setCurrentTab(0);
}
}
and my main.xml file
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TabWidget
android:id="#android:id/tabs"
android:layout_weight="0"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
and my tab1.java (tab2.java is the same)
package com.geoflex.trymasak;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Tab1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("This is tab 1");
setContentView(tv);
}
}
Try this to set Tabs in the bottom :
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_main"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs"
/>
</RelativeLayout>
</TabHost>
And use the following code for your main activity (thats extends the TabActivity):
public class TabTestActivity extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this,Tab1.class);
spec = tabHost.newTabSpec("projects").setIndicator("Projects",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab2.class);
spec = tabHost.newTabSpec("news").setIndicator("News",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
Try this
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:paddingBottom="#dimen/tab_space_top"
android:layout_width="fill_parent" android:paddingLeft="#dimen/tab_space_gap"
android:layout_height="fill_parent" android:paddingRight="#dimen/tab_space_gap" >
<LinearLayout android:id="#+id/tab_relative_layout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#drawable/background">
<FrameLayout android:id="#android:id/tabcontent" android:layout_weight="1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_below="#android:id/tabs"></FrameLayout>
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0"></TabWidget>
</LinearLayout>
RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
I think this is supposed to be casted to Framelayout according to your logs.
set in fragment
if(getActivity() != null && isAdded) {
//do your operation
}

Selection problem in tabs

I am making an application where i need tabs placed at the bottom of the screen .
I have managed to get to implement the code of tabs but i can't get them to be placed in the bottom of the screen.
Also i don't want any of the tabs to be selected i.e. i just want four tabs to be present at the bottom of the screen and above the tabs there are some image views and buttons.
I have linked the tabs to respective activites but i don't want the tab bar to be functional until the user wants to go and click it.
Till then i just want a normal gui screen with tabs at the bottom with none of them pre selected.
package com.tabs;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class HelloTabWidget extends TabActivity {
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, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums", res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
---------- To place tab at the bottom add the below code
<TabHost android:id="#android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"><RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"><FrameLayout android:id="#android:id/tabcontent" android:background="#drawable/bg" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="#android:id/tabs"/><TabWidget android:id="#android:id/tabs" android:background="#drawable/tab_bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#fff" android:layout_alignParentBottom="true" android:textStyle="bold"/></RelativeLayout></TabHost>
##
and if yu dont want any tabs to be selected remove
tabHost.setCurrentTab(2);

Categories

Resources