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);
Related
I have a tab host and I want to use the same layout file for all three tabs. But when I try to do this I only see the layout on one of the tabs not all three.
myTabHost =(TabHost) findViewById(R.id.TabHost01);
myTabHost.setup();
TabHost.TabSpec spec1 = myTabHost.newTabSpec("First Tab");
spec1.setIndicator("First Tab", getResources().getDrawable(android.R.drawable.ic_menu_add));
spec1.setContent(R.id.tab1);
myTabHost.addTab(spec1);
myTabHost.addTab(myTabHost.newTabSpec("Tab Two").
setIndicator("Tab Two", getResources().getDrawable(android.R.drawable.ic_menu_edit)).setContent(R.id.tab1));
And then in my tab hosts xml activity I have
<TabHost
android:id="#+id/TabHost01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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>
<!-- container of tabs -->
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
layout="#layout/item_list_view">
But on Tab Two I don't see the item_list_view?
How can I use the same xml for both tabs?
Thanks for the help
myTabHost =(TabHost) findViewById(R.id.TabHost01);
myTabHost.setup();
TabHost.TabSpec spec1 = myTabHost.newTabSpec("First Tab");
spec1.setIndicator("First Tab", getResources().getDrawable(android.R.drawable.ic_menu_add));
spec1.setContent(R.id.tab1);
myTabHost.addTab(spec1);
You are making it porogramatically right use Tabhpost content factory for the content
I want to display a listView on mapView as in below image of Google maps app.
Initially only map is visible. When user clicks on a tab I want this listView to appear on mapView.
But the problem is that using below layout does not display maps(gray screen is visible). However, listView is displayed.
Layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pickMatchesLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
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" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/map_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include layout="#layout/listView" >
</include>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
Method to set up tabs :
// Set up tabs
private void setUpTabs() {
// Get TabHost
tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
// Create tabs
TabSpec sourceOnlyState = tabHost.newTabSpec(SOURCE_ONLY_STATE);
sourceOnlyState.setContent(R.id.map);
sourceOnlyState.setIndicator("Source Only");
TabSpec allState = tabHost.newTabSpec(ALL_STATE);
allState.setContent(R.id.map);
allState.setIndicator("All");
TabSpec mapTypes = tabHost.newTabSpec(MAP_FEATURES);
mapTypes.setContent(R.id.map_layout);
mapTypes.setIndicator("Map Types");
// Add tabs in TabHost
tabHost.addTab(sourceOnlyState);
tabHost.addTab(allState);
tabHost.addTab(mapTypes);
// Set tab change listener
tabHost.setOnTabChangedListener(this);
}
OnClick of tab start new activity and do all the code related to listview in that activity and set theme for activity is
android:theme="#android:style/Theme.Holo.Dialog.NoActionBar"
I have four tabs in tabbar. Whenever I launch my application I need to run all the tabs (four) of a tabbar, because all the tabs contains webviews that should be loaded whenever the app is launched. But the first tab should gets highlighted. I have used
setCurrentTab(0);
to load the first tab,but iam unable to load remaining tabs without clicking.
To do this, first create a xml file. The sample file is shown as below.
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/background">
<TabWidget android:layout_width="fill_parent" android:id="#android:id/tabs"
android:layout_height="wrap_content" />
<FrameLayout android:layout_width="fill_parent" android:id="#android:id/tabcontent"
android:layout_gravity="bottom" android:paddingTop="70dp"
android:layout_height="match_parent">
<ScrollView android:layout_height="fill_parent" android:id="#+id/viewTab1"
android:layout_width="match_parent">
<!-- Your view you want -->
</ScrollView>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/viewTab2">
<!-- Your view you want -->
</LinearLayout>
<!-- Add as many view you want with unique id -->
</FrameLayout>
Each layout will refer to a unique layout.
In main activity use following
TabHost tabHost = getTabHost();
TabSpec spec1 = tabHost.newTabSpec("FirstTabView");
TabSpec spec2 = tabHost.newTabSpec("SecondTabView");
spec1.setContent(R.id.viewTab1);
spec1.setIndicator("First Tab");
spec2.setIndicator("Second Tab");
spec2.setContent(R.id.viewTab2);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.setup();
You can add as many tabs needed. And this all will be initialized at same time.
I hope this helps you :)
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;
}
right now I'm using in my application tabhost with single activity and separate views.
Tab Activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabs = (TabHost) findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec = tabs.newTabSpec(TAB_HOTELS);
spec.setContent(R.id.tab1);
spec.setIndicator("Hotels");
tabs.addTab(spec);
spec = tabs.newTabSpec(TAB_LAST_MINUTE);
spec.setContent(R.id.tab2);
spec.setIndicator("Last Minute");
tabs.addTab(spec);
Layout
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+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:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/tab1"
android:orientation="vertical">
<ListView android:id="#+id/listaLocalita"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/tab2"
android:orientation="vertical" android:paddingTop="60px">
<TextView android:layout_width="fill_parent"
android:layout_height="100px" android:text="This is tab 2"
android:id="#+id/txt2" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
And things are working quite well as long as i'm using one level depth. Unfortunately now I need to make one of the tab to work like following:
Tab loading ListView with the list of cities, User clicks on city name, tab's content is replaced with the list of the Hotels, user choose a hotel and always in the the same tab, hotel's info is loaded.
How can i achieve this scenario? LayoutInflater?
Thanks in advance.
you can use intent to set Activity as your tab content, and this activity can call other activities, either listView or any other.
/** tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec MainTab = tabHost.newTabSpec("tag1");
TabSpec SearchTab = tabHost.newTabSpec("tag2");
TabSpec MessageTab = tabHost.newTabSpec("tag3");
TabSpec ServicesTab = tabHost.newTabSpec("tag4");
TabSpec SettingsTab = tabHost.newTabSpec("tag5");
MainTab.setIndicator("Main", res.getDrawable(R.drawable.anchor_36));
MainTab.setContent(new Intent(this, MainView.class));
SearchTab.setIndicator("Search", res.getDrawable(R.drawable.clock_36));
SearchTab.setContent(new Intent(this, SearchView.class));
MessageTab.setIndicator("Messages", res
.getDrawable(R.drawable.dialog_36));
MessageTab.setContent(new Intent(this, MessagesView.class));
ServicesTab.setIndicator("Services", res
.getDrawable(R.drawable.cloud_36));
ServicesTab.setContent(new Intent(this, ServicesView.class));
SettingsTab.setIndicator("Settings", res
.getDrawable(R.drawable.settings_36));
SettingsTab.setContent(new Intent(this, SettingsView.class));
/** Add tabSpec to the TabHost to display. */
tabHost.addTab(MainTab);
tabHost.addTab(SearchTab);
tabHost.addTab(MessageTab);
tabHost.addTab(ServicesTab);
tabHost.addTab(SettingsTab);
Now in the loaded activity you can use the code like
StartActivity(new intent(currentActivity.this, newActivity.class));
this will work for you.