Changing view inside TabHost (one activity, multiple views) - android

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.

Related

Use same xml layout file for TabHost

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

Tabs in Android not working correctly?

I have created 4 tabs in my application using tabHost ,
it working fine.
below i show my code for adding only 2 tabs.
public class Home_tab extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this, CoalActivity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("", ressources.getDrawable(R.drawable.tab_dis))
.setContent(intentAndroid);
// Apple tab
Intent intentApple = new Intent().setClass(this, EnergyActivity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("", ressources.getDrawable(R.drawable.tab_foc))
.setContent(intentApple);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
}
}
This is my 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"
android:background="#drawable/background"
android:scrollbarAlwaysDrawHorizontalTrack="true"
>
<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>
But my problem is when i add more tabs, it comes in the same window.
for example, currently i added 4 tabs, when i try to add 3 more tabs , all the tabs are coming in the same window? i want to add only 4 tabs in the same window, other tabs should appears only when i scroll the tab bar
How to solve this??
Check following links and let me know if they helps in solving your problem
Link1
Link2
link3

Dynamically adding a whole row in GridView, or splitting the View

I have a GridView with 4 columns, when one is selected, I wanted to dynamically add a whole row underneath the selected cell, and inflate a layout in it, so that I could add some information about the selected cell. Is there anyway to do that? Or maybe, is there a way to split a view in half, then glue it back together when done? Sounds crazy.
Essentially, I'm looking for something you can find on iTunes 11 (see the picture below).
Yeah. You can do that. the xml file where you defined grid view, below that define one linear layout too and make it invisible by adding following code:
android:visibility="invisible"
On click the GridView, inflate that layout and set its visibility and use following java codes:
LinearLayout l2=(LinearLayout)findViewById(R.id.linearLayout);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View detailView = inflater.inflate(R.layout.yourInformationLayoutOfGridItem, l2, false);
for set visibility true/false by java code:
Visibile: l2.setVisibility(0);
Invisible: l2.setVisibility(4);
The Solution to your question is to simple use TabHost widget:
The XML file to that is as below:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<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="match_parent" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
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>
</TabHost>
And The java code for that is as below:
public class AndroidTabLayoutActivity 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();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Songs");
// setting Title and Icon for the Tab
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Videos");
videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
}
Hope this works fine for you.. And hope this is what you asked for.

Tab icon not showing

I'm trying to do a simple tab app in android with two tabs. My problem is that when I put this code, in the tab, only is shown the text, but no the icons.
If I put the text to "" the icon is shown.
Could someone help me? My android version is 4.0.3.
Thanks a lot.
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs" />
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabcontent" >
<LinearLayout android:id="#+id/tab1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/textView1"
android:text="Contenido Tab 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="#+id/tab2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/textView2"
android:text="Contenido Tab 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
and the activity code is
public class TabTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
spec.setContent(R.id.tab1);
spec.setIndicator("sss",
res.getDrawable(android.R.drawable.ic_btn_speak_now));
tabs.addTab(spec);
spec=tabs.newTabSpec("mitab2");
spec.setContent(R.id.tab2);
spec.setIndicator("TAB2",
res.getDrawable(android.R.drawable.ic_dialog_map));
tabs.addTab(spec);
tabs.setCurrentTab(0);
}
as you can see is very simple. But when I write spec.setIndicator("",
res.getDrawable(android.R.drawable.ic_dialog_map));
I can see the icon, bu when I write spec.setIndicator("TAB2",
res.getDrawable(android.R.drawable.ic_dialog_map));
I can only see TAB2, but no both of them.
It seems that there are no enougth space to show both. So I've tried to get increase the tab height with this
tabs.getTabWidget().getChildAt(1).getLayoutParams().height = 150;
but not seems to work.
I replaced the label name with null value.
Now I can see the icon alone..
Could not find out any other solution.
TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
spec.setIndicator("",
res.getDrawable(android.R.drawable.ic_btn_speak_now));
Intent sssIntent = new Intent(this, First.class);
spec.setContent(sssIntent);
tabs.addTab(spec);
//your are over loading the 1st one so you can see only the last added one
TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
spec.setIndicator("sss",
res.getDrawable(android.R.drawable.ic_btn_speak_now));
Intent sssIntent = new Intent(this, First.class);
spec.setContent(sssIntent);
tabs.addTab(spec);
TabHost.TabSpec spec2=tabs.newTabSpec("mitab2");
spec2=tabs.newTabSpec("mitab2");
spec2.setIndicator("TAB2",
res.getDrawable(android.R.drawable.ic_dialog_map));
Intent sssIntent2 = new Intent(this, Second.class);
spec2.setContent(sssIntent2 );
tabs.addTab(spec2);
The visibility of the icon (together with the label) in the tab depend on the target device and the android platform version.
I had a deeper look into this issue and added more details and a solution at your other (quite similiar) question about this problem; It can be found here:
https://stackoverflow.com/a/11379708/414581
Adding this in AndroidManifest.xml solved the issue.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar">
</application>

Android TabHost content not showing

I've created a TabHost and assigned 4 activity intents with tabs and these seems to work just fine. My only problem is that the activity content is not showing within the framelayout #tabcontent in my tabhost view.
I've followed the official reference and scanned the internet for a solution but I can't see what the problem is.
The Log.v("Activity", "Reports") is logged in ant, so it does execute the activity. Therefore I'm guessing its the setContentView() in my ReportsActivity that is causing the problem. But I'm new to this so I can't really tell. (There are no generated errors)
This is my tabhost
<?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="#android:id/tabhost"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
This is how I add tabs in my TabActivity
// Glob
Intent intent;
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Resources res = getResources();
// Reports tab
intent = new Intent().setClass(this, ReportsActivity.class);
spec = tabHost.newTabSpec("reports")
.setIndicator(
res.getText(R.string.reports),
res.getDrawable(R.drawable.reports))
.setContent(intent);
tabHost.addTab(spec);
And this is my content activity (R.layout.reports = linearlayout with a textview)
public class ReportsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reports);
Log.v("Activity", "Reports");
}
}
Try implementing your TabSpec like this:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec = tabHost.newTabSpec("reports")
.setIndicator(
res.getText(R.string.reports),
res.getDrawable(R.drawable.reports))
.setContent(intent);
This is because you have chosen a LinearLayout which defaults to Horizontal layout.
If you set android:orientation="vertical" inside the LinearLayout tag that should fix your problem

Categories

Resources