Tabs in Android not working correctly? - android

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

Related

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.

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

Tabview to follow all activities

Im new to android and i am trying to develop a simple application with tabhost. And i want the tabhost to be in all of my activites. I have 5 tabs and 6-7 activites, and i want them all to be shown in those five tabs.
Here i create my tabactivity:
<?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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</TabHost>
And here i add tabs:
TabSpec tabSpec = mTabHost.newTabSpec("tab_test1");
tabSpec.setIndicator("Map",res.getDrawable(R.drawable.world));
Context ctx = this.getApplicationContext();
Intent i = new Intent(ctx, DealFinderActivity.class);
tabSpec.setContent(i);
mTabHost.addTab(tabSpec);
mTabHost.getTabWidget().setBackgroundColor(Color.GRAY);
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator
("List Deals",res.getDrawable(R.drawable.database))
.setContent(new Intent(this,ListDeals.class)));
// TextView textView = (TextView) findViewById(R.id.textview3);
// textView.setText("hello");
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Favorites",
res.getDrawable(R.drawable.heart)).setContent(
new Intent(this, ListDeals.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test4").setIndicator("Settings",
res.getDrawable(R.drawable.preferences)).setContent(
new Intent(this, DealDescription.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test5").setIndicator("About",
res.getDrawable(R.drawable.newspaper)).setContent(
new Intent(this, DealDescription.class)));
And in the ListDeals.class i use this to change to a new activity:
Intent myIntent = new Intent(getApplicationContext(),
DealMyAss.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(myIntent);
But here the tabview disappears??
Any ideas how i can keep the tabview??
Thanks in advance
ActivityGroup is what you are looking for .
as name suggest it ambeds multiple activities within a tab .
go through examples . one is here .
Create one ActivityGroup for every tab .

ListActivity is not showing up when added as content of a TabActivity

I'm trying to present a ListActivity in a TabActivity, and for some reason the ListActivities simply will not show up. All I get is a blank space beneath the tabs.
TabActivity: https://picasaweb.google.com/FlyingYellow/Misc#5629459368100202146
ListActivity: https://picasaweb.google.com/FlyingYellow/Misc#5629459406832281026
I have absolutely no idea why this is happening. I've searched around and have only found posts about input issues. I can't even get my content to display!
TabActivity.onCreate()
super.onCreate(savedInstanceState);
setContentView(R.layout.browser);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent(this, ArtistBrowser.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_artists));
spec.setContent(intent);
tabHost.addTab(spec);
intent = new Intent(this, AlbumBrowser.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums", res.getDrawable(R.drawable.ic_tab_artists));
spec.setContent(intent);
tabHost.addTab(spec);
intent = new Intent(this, TrackBrowser.class);
spec = tabHost.newTabSpec("tracks").setIndicator("Tracks", res.getDrawable(R.drawable.ic_tab_artists));
spec.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
(I followed the Android docs tutorial)
/res/layout/browser.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="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <--- this was the error
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
Once again, I've discovered I'm an idiot. Just in case anyone else runs into this error, my problem was I set the TabWidget's height to match_parent without thinking. I figured this out by making the TabWidget and FrameLayout different colors and saw that the entire screen was red. Christ, I need to be more careful.

Changing view inside TabHost (one activity, multiple views)

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.

Categories

Resources