TabHost inside fragment in HoenyComb - android

I have started currently an portfolio application for HoneyComb tablet. I have used ActionBar.Tab to implement three tab on the ActionBar and Fragment for each Tab. The three tab name About, Gallery, Settings. In the Settings ActionBar.Tab, I want to have TabHost.
That means How to have TabHost inside Fragment. Thanks in advance for your ideas!!!

Have a layout with a tabhost like (assuming you define this as my_fragment_tabhost):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<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:visibility="gone"
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"/>
</LinearLayout>
</TabHost>
</LinearLayout>
In your fragment, have a TabHost member variable and get it in the onCreateView like:
private TabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment_tabhost, container, false);
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup();//very important to call this
TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content");
tab.setIndicator("my tab content")
//... add your content by one of the tab.setContent() methods
mTabHost.addTab(tab);
return view;
}

Related

Conditional use of tabbed activity

I am trying to create an app that starts with an activity A with a "custom" layout. From that activity it is possible to enter activity B with 3 tabs.
I succeed at creating activity with tabs. The problem is that tabs shows up only if that activity is also launcher activity. If it is not, the tabs do not shows up.
At first time i tried to create tabs with TabLayout. Now i tried to create tabs with TabHost.
Can someone pleas help?
onCreate method in main activity where tabs are "created".
public class GlavnaStran extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glavna_stran);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabHost.TabSpec tab1 = tabHost.newTabSpec("Search");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Lyric");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Playlist");
//tab1.setIndicator("Tab1");
tab1.setIndicator("Search");
tab1.setContent(new Intent(this, SearchTab.class));
//tab2.setIndicator("Tab2");
tab2.setIndicator("Lyric");
tab2.setContent(new Intent(this, LyricTab.class));
//tab3.setIndicator("Tab3");
tab3.setIndicator("Playlist");
tab3.setContent(new Intent(this, PlaylistTab.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
}
For each tab i was created separate activity which looks like that:
public class SearchTab extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_tab);
}
}
The xml part of the project is created by importing TabHost container in design view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.admin.besedilatakt_v5.GlavnaStran">
<TabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabhost">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
In the XML files of tab's activity is nothing special except TextView widget to display some random text.

Tabs at the bottom of a fragment

I'm having a Fragment from a mother activity and I want inside it to display two tabs using FragmentTabHost. But I would like the tabs to be located at the bottom of the fragment not at the top, as it is the default.
MainFragment.java:
public class MainFragment extends Fragment {
private FragmentTabHost tabHost;
private View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//tabHost = new FragmentTabHost(getActivity());
rootView = inflater.inflate(R.layout.fragment_main, container, false);
tabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);
tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);;
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab 1"), ConnectFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab 2"), ManageFragment.class, null);
tabHost.setCurrentTab(0);
return tabHost;
}
}
The layout which I am using is pretty basic and when I run this version everything works but the tabs are displayed at the top of the fragment.
fragment_main.xml:
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
When I am running a slightly different version which is supposed to put the tabs at the bottom of the fragment-according to other threads of this site- i get an error.
fragment_main_v2.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Thrown error:
android.view.InflateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I can imagine that something is going wrong with the
android.view.InflateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Get instance of child/sub TabHost from the parent TabHost

I've created an activity which contains TabHost. In one of the tabspec there is another TabHost (like a sub-TabHost).
By default the visibility of this subTabHost is gone, & is only visible when the second parent tabspec is selected.
Now when the second tab is selected, I want to get the instance of the subTabHost inside java code in TabSpec variable.
Thank You
Layout:
Parent TabHost Layout
<TabHost
android:id="#android:id/tabhost"
android:visibility="gone" >
...
<TabWidget
android:id="#android:id/tabs" />
<FrameLayout
android:id="#android:id/tabcontent" >
<include
android:id="#+id/abc"
layout="#layout/abc"
android:visibility="gone" />
<include
android:id="#+id/subtab2"
layout="#layout/subtab2" <--! sub tab -->
android:visibility="gone" />
...
Inside layout of subtab2
<!-- want to get this's tabhost instance in code -->
<TabHost
android:id="#android:id/tabhost"
android:visibility="visible" >
<LinearLayout
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs" />
<FrameLayout
android:id="#android:id/tabcontent" >
<include
android:id="#+id/xyz"
layout="#layout/xyz"
android:visibility="gone" />
.....
Java Code
TabHost parent = mTabHost = (TabHost) findViewById(android.R.id.tabhost);
// How to code below
(if subtab2 is visible)
Tabhost subTabHost = ??
As you used the same ids for the TabHosts you can't use findViewById(android.R.id.tabhost) in the main layout but you can use it in the content panel:
// when the second tab is selected
FrameLayout content = (FrameLayout) findViewById(android.R.id.tabcontent); // main tabs content
TabHost subtabs = (TabHost)content.findViewById(android.R.id.tabhost);

ActionBarActivity with TabHost

I am developing an app, with minimum sdk level 7 and that's why I use support library and ActionBarActivity to have a proper ActionBar, but also the layout of my page contains four tabs on the bottom of the activity, and as i already extend ActionBarActivity, I cannot extend TabActivity.
The problem is when I write
public class HomePageActivity extends ActionBarActivity {
private TabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_homepage);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabHost.TabSpec tab1 = mTabHost.newTabSpec("TAB1");
tab1.setIndicator("TAB 1");
Intent popularContentIntent = new Intent().setClass(this, GuideActivity.class);
tab1.setContent(popularContentIntent);
mTabHost.addTab(tab1);
I get the error "Did you forget to call 'public void setup(LocalActivityManager activityGroup)?'" at the line when I trying to add tab to the tabhost.
If I try to set the id of the view in the tab.setContent(..) like tab.setContent(R.id.tab1) and have a view in the xml with the id tab1, I get the error that the view with such a number cannot be found.
My layout:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs"
android:layout_alignParentTop="true" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</Button>
</TabWidget>
</RelativeLayout>
Is it possible somehow to have layout with tabs and actionbar at the same time? What I do wrong? What could be other alternatives?
Thanks a lot!
TabActivity was deprecated in API level 13. The updated documentation recommends using android.support.v4.app.FragmentTabHost instead.
An even better method would be to use the ViewPager sample provided here.

tabHost in android gives Unfortunately,app has stopped error

I want two tabs in my application which are in other activity which I access from my main activity from a button.
My Activity which will control the tabs has the code as below
public class Myclass extends TabActivity
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myclass);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstSpec = tabHost.newTabSpec("First");
firstSpec.setIndicator("first Tab",null);
Intent firstintent = new Intent(this,Tab1.class);
firstSpec.setContent(firstintent);
TabSpec secondSpec = tabHost.newTabSpec("Second");
secondSpec.setIndicator("first Tab",null);
Intent secondintent = new Intent(this,Tab2.class);
secondSpec.setContent(secondintent);
tabHost.addTab(firstSpec);
tabHost.addTab(secondSpec);
}
}
And the XML which has the tabHost content has the code
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabHost"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
So the app is compiling and loading up fine in my device and able to access other activites. But when I try to access the button which should display the activity above mentioned "Myclass" activity. The application exits and I get a screen displaying "Unfortunately,app has stopped".
I have entered the activities in the AndroidManifest.xml also.
The TabActivity shows a cancel mark on the word and says 'android.app.TabActivity' is deprecated.
What does this mean exactly?
Can any one suggest me what is going wrong with the program.
I changed the code as given in the link http://developer.android.com/reference/android/app/TabActivity.html to the code below Code
public class MyClass extends FragmentActivity
{
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myclass);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
Tab1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
Tab2.class, null);
}
}
And the XML CODE to
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
But still no effect, the same problem of application exiting upon clicking of the button to start the activity Myclass continues.
Can anyone suggest a solution?
you should change
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
with
TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
try this:
make changes in your XML
Change Tab host id
android:id="#+id/tabHost" to android:id="#android:id/tabhost"
it should be like this :
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabhost
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">

Categories

Resources