Hello Guys and probably some Girls, is it possible to stack Layout Objects. So that one Object is over the other one?
My Problem is this, i've got a tabhost and under the tabhost i've got a tablerow with a textview in it.
I made the tabhost tabs invisible, but it hiddes my tablrow with the textview in it,how can i bring it infront?
Here is the Layout.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"
android:weightSum="1">
<RelativeLayout
android:orientation="vertical"
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_weight="1" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom = "#android:id/tabcontent"
android:visibility="invisible"/>
<TableRow
android:id="#+id/ADFilterOverview"
android:layout_height="wrap_content"
android:onClick="onClickADConfig"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="#android:id/tabs"
android:layout_width="match_parent">
<TextView
android:text="#string/newadfilter"
style="#style/NormalFont"
android:layout_gravity="bottom|left">
</TextView>
</TableRow>
</RelativeLayout>
</TabHost>
Thx in Advance
Best Regards
safari
Some more information, for better understanding:
Afterwards my code of my Activity looks something like this:
package de.retowaelchli.filterit;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.content.Intent;
public class StatsActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("whatever").setContent(new Intent(this, SFilterStatsActivity.class)));
tabHost.setCurrentTab(1);
}
/** Verweise auf die anderen Seiten **/
public void onClickADConfig(View view){
final Intent i = new Intent(this, ADFilterConfigActivity.class);
startActivity(i); }
}
The linked class from my Tabhost looks for example like this:
package de.retowaelchli.filterit;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class ADFilterStatsActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListContent));
}
private static String[] mListContent={"Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3"};
}
So you see, i got a TabHost with one tab in it, which is filled from my ListActivity, the tabhost should be hidden (invisible), over this hidden tab should be the tablerow visible. So i can click it to get to my other class.
I hope this helps you guys...
FOUND A SOLUTION THX Egon
And here is the solution
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabcontent"
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">
<RelativeLayout
android:orientation="vertical"
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_weight="1" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom = "#android:id/tabcontent"
android:visibility="invisible"/>
<TableRow android:layout_height="wrap_content" android:id="#+id/ADFilterOverview" android:layout_width="match_parent" android:onClick="onClickADConfig" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true">
<TextView android:layout_gravity="bottom|left" android:text="#string/newadfilter" style="#style/NormalFont"></TextView>
</TableRow>
</RelativeLayout>
</TabHost>
</FrameLayout>
If you want to place one View at the top of the other in Z-axis, you can use FrameLayout. It lets you position a View on the background and other views on top of it. It's really powerful and easy to use, so you must give it a try. Hope this helps.
If you make Parentview invisible then its childview also automatically make invisible. In this scenario you have to define other layout for your new tab.
If this is not correct, please make edit in, or suggest me. Thanx
EDIT: on your xml code I think tabhost is a parentview and tablerow , textview are childview of it.
I believe the problem is that you set the tabwidget to be alignParentBottom and then you set TableRow to have aligntop the tabwidget. Thus it is not visible. Making the tabwidget invisible does hide it, but the place holder is still there.
Related
I have 2 tabs: Report and Profile.
I want to add a different form (Textview, spinner, etc) in each of the tabs. For now im trying with a button and it should be different button in different tab.
But what i get is i have the same button in both tab. It should show button name ButtonReport in Report Tab and ButtonProfile in Profile Tab.
Below is the picture of it.
I am putting the button code in main.xml.
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:orientation="vertical"
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">
</TabWidget>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Start Interface -->
<TableLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/tableLayout1" android:alwaysDrawnWithCache="false">
<TableRow android:layout_height="fill_parent" android:id="#+id/tableRow3">
<TableLayout android:id="#+id/tableLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:text="Button Report" android:id="#+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</TableLayout>
</TableRow>
</TableLayout>
<!-- End Interface -->
</FrameLayout>
</LinearLayout>
</TabHost>
This is my ReportActivity class and ProfileActivity(same code only different name)
package com.tab;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ReportActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
text.setText("Artist");
setContentView(text);
}
}
This is my MainActivity class
package com.tab;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
private TabHost aTab;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
aTab = getTabHost();
TabHost.TabSpec spec;
Intent intent;
//Report Tab
intent = new Intent(this, ReportActivity.class);
spec = aTab.newTabSpec("Report")
.setIndicator("Report", res.getDrawable(R.drawable.tab_icon))
.setContent(intent);
aTab.addTab(spec);
//Profile Tab
intent = new Intent(this, ProfileActivity.class);
spec = aTab.newTabSpec("Profile")
.setIndicator("Profile", res.getDrawable(R.drawable.tab_icon))
.setContent(intent);
aTab.addTab(spec);
aTab.setCurrentTab(1);
}
}
is there a way to have different form in different tab?
The best way (my opinion) is to use Fragments :
http://developer.android.com/guide/components/fragments.html
Fragment are for API Level 11 minimum, but you can work with a compatibility package :
http://android-developers.blogspot.ch/2011/03/fragments-for-all.html
So every tab will become a fragment.
Have a look at TabHost tutorial And, just look how they're using Different Contents in their TabHost For Example -
main.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">
<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>
</TabHost>
Report.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Screen Design for Photos -->
<Button android:text="Button Report"
android:textSize="18dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Profile.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Screen Design for Photos -->
<Button android:text="Button Profile"
android:textSize="18dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
In your main.xml class contain some different thing that is Start Interface This is the problem that to display the same content (Button) is every tab. You've to remove this.
And, in your Profile and Report activities just view this xml file using setContentView This will provide the seperate buttons which you need. You can use MainActivity for this, it seems perfect.
Hope this helps you.
You can set different layout xml's for each activity (report and profile):
Source: http://www.androidhive.info/2011/08/android-tab-layout-tutorial/
First make an activity called TabHostActivity:
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabHostActivity 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();
TabSpec report = tabHost.newTabSpec("Report");
report.setIndicator("Report", getResources().getDrawable(R.drawable.icon_report));
Intent reportIntent = new Intent(this, report.class);
report.setContent(reportIntent);
TabSpec profile = tabHost.newTabSpec("Profile");
profile.setIndicator("Profile", getResources().getDrawable(R.drawable.icon_profile));
Intent profileIntent = new Intent(this, profile.class);
profile.setContent(profileIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(report); // Adding photos tab
tabHost.addTab(profile); // Adding songs tab
}
}
then make Main.xml under res/layouts:
<?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>
</TabHost>
Then two different activities that will do your actions for report and profile:
//report.java
package com.example;
import android.app.Activity;
import android.os.Bundle;
public class ReportActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report_layout);
}
}
//profile.java
package com.example;
import android.app.Activity;
import android.os.Bundle;
public class ProfileActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_layout);
}
}
Now you can make your two XML's, one for the profile layout and one for the report layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Profile"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
And
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Report"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Once that is done you can modify each xml to have whatever buttons and spinners, etc. you like.
Lastly, make sure you modify your AndroidManifest to allow the activities and auto-launch via the intent filters to your tabhostactivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".TabHostActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".profile" />
<activity android:name=".report" />
</application>
I think this piece of code will help you.You can use different intents to call different tab activity..And you can set different content views to different activities..
public class MainActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
//Setting Android TAB
Intent intentAndroid = new Intent().setClass(this, AndroidTab.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("", ressources.getDrawable(R.drawable.android))
.setContent(intentAndroid);
//Setting Apple TAB
Intent intentApple = new Intent().setClass(this, AppleActivity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("", ressources.getDrawable(R.drawable.aapl))
.setContent(intentApple);
//Add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
tabHost.setCurrentTab(0);
}
}
After you should create different classes and make two activities for android and apple.In that particular activities you can set different contentviews and I think you will get what you wanted.This is just a sample you can make it what you want as the same..
I have a little problem using Tabs with Views.
First I just copied the sample code where Tabs are used with activitys:
My LayoutFile looks like this:
<?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>
And this is my Java-code:
public class MyActivity extends TabActivity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState)
setContentView(R.layout.main);
TabHost tH = getTabHost();
Indent intent = new Intent().setClass(this, AnotherActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
//TextView Test = new TextView(this);
//Test.setText("test");
tH.addTab(tH.newTabSpec("t1").setIndicator("Tab1").setContent(intent));
tH.setCurrentTab(0);
}
}
And this works as expected.
But when I uncomment the TextView-lines and call setContent(Test.getId()) instead of setContent(intent), the app crashes.
I also tried to create a textview in the layoutfile, and call setContent(R.id.test),
that also makes it crash.
So this is one problem.
The seccond point is. I do not want to use activitys, because i want to be able to call methods on those classes, which shall represent the Tab-content.
So my original idea is, to derive some classes from view. 1 for each tab, and pass their ids. But therefor the codesample above needs to work first.
greetings Uzaku
I know you said you tried a TextView in the layout file but this should work...
Change the FrameLayout section as follows...
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<TextView
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TEST" />
</FrameLayout>
Then in your code do the following...
tH.addTab(tH.newTabSpec("t1").setIndicator("Tab1").setContent(R.id.test));
Forgive my simple questions, as I'm new to android. :)
Why is the text property of the textview not displaying inside this tab?
Screen: Here
Here's my code..
MapTab2.java
package com.test.maptab2;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
public class MapTab2 extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost.TabSpec spec;
spec = getTabHost().newTabSpec("tab1");
spec.setContent(R.id.mapstub);
spec.setIndicator("Map");
getTabHost().addTab(spec);
spec = getTabHost().newTabSpec("tab2");
spec.setContent(R.id.detailstub);
spec.setIndicator("Detail");
getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
}
}
Main.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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Tab-switch panel -->
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!-- Tab contents -->
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Map here -->
<TextView android:id="#+id/mapstub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="map"/>
<!-- Other stuff -->
<TextView android:id="#+id/detailstub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="detail"/>
</FrameLayout>
</LinearLayout>
</TabHost>
I'm trying to build my way up to displaying a map within a tab. Best to understand the basics first, I thought. :')
I actually have had more luck setting the gravity in the parent view (in this case, your LinearLayout), then setting the layout_gravity attributes in the child views (in your case, your TextViews, etc).
To get this simple example working, I would change all your "fill_parent" attributes to "wrap_content" (or 0.0dp as another alternative) - then you can start playing with the different attributes to get everything positioned exactly how you would like.
Try adding android:gravity="center" in every TextView inside a Tab.
Hey im developing my first proper app for android and have run into 2 small hickups and was hoping for some help basicaly the main problem is that i cant seem to change to a new view from within tabhost. I have 4 tabs 2 just show a simple xml file the 3rd shows a series of pictures which i want to make clickable so that they expand out into player bio's. atm im attempting to do this in the following way but it just causes my application to crash
package com.BPRUFC;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
public class BPRUFCAppActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Fixtures").setContent(R.id.fixtures));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Results").setContent(R.id.results));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Players").setContent(R.id.bio));
mTabHost.addTab(mTabHost.newTabSpec("tab_test4").setIndicator("Tour").setContent(R.id.tour));
mTabHost.setCurrentTab(0);
int days = 184;
String tour2 = getString(R.string.tour, days);
}
public void clicked(View view)
{
Intent myIntent = new Intent(BPRUFCAppActivity.this, PlayerBio.class);
BPRUFCAppActivity.this.startActivity(myIntent);
}
}
main Xml stopped after first pic as its quite long
<?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">
<ScrollView
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/fixtures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/fixtures" />
</ScrollView>
<TextView
android:id="#+id/results"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Results go here" />
<ScrollView
android:id="#+id/ScrollView02"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/bio"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="90dip"
android:layout_height="90dip"
android:id="#+id/imageView1"
android:src="#drawable/forrest"
android:onClick="clicked"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp">
</ImageView>
Thanks in advance and im sure its a noob mistake
I fixed this myself. I was going about it the wrong way nesting too many layouts. I changed it all so that each tab has 1 frame layout and this is then modified accordingly it now behaves properly.
can anybody tell how to reduce the height of tab bar and display tab bar in bottom
Thanks
use the following line of code to change the height, this is the last line in my onCreate method
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height =35;
Add the following method to your class that extends TabActivity
public void addNewTab(Context context, String title, int height){
TabHost tabHost = getTabHost(); // The activity TabHost
Intent intent = new Intent().setClass(context, HelloTabsActivity.class);
TabHost.TabSpec spec = tabHost.newTabSpec(title.toLowerCase()).setIndicator(title).setContent(intent);
tabHost.addTab(spec);
int totalTabs = tabHost.getTabWidget().getChildCount();
((RelativeLayout)tabHost.getTabWidget().getChildTabViewAt(totalTabs-1)).removeViewAt(0);
((TextView)((RelativeLayout)tabHost.getTabWidget().getChildTabViewAt(totalTabs-1)).getChildAt(0)).setHeight(30);
tabHost.getTabWidget().getChildAt(totalTabs-1).getLayoutParams().height = height;
}
then call it like this addNewTab(this, "tab title", 30);
Note that you have to change the height of each tab. For two tabs:
#Override
public void onCreate(Bundle savedInstanceState) {
... // other code
final int height = 45;
mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height = height;
mTabHost.getTabWidget().getChildAt(1).getLayoutParams().height = height;
}
You could either
Build your own tab using a TableLayout at the bottom of the screen - which gives you quite a lot of flexibility
or
Modify use the existing TabHost/TabWidget - works in principle but I don't know how to reduce the tab bar height. Works like that:
Layout file main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost android:id="#+id/tab_host"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#android:id/tabs"
android:layout_gravity="bottom" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<LinearLayout android:id="#+id/first_tab"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="First Tab" />
<!-- Replace TextView with your layout content for this tab -->
</LinearLayout>
<LinearLayout android:id="#+id/second_tab"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="Second Tab" />
<!-- Replace TextView with your layout content for this tab -->
</LinearLayout>
<LinearLayout android:id="#+id/third_tab"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="One More Tab" />
<!-- Replace TextView with your layout content for this tab -->
</LinearLayout>
<LinearLayout android:id="#+id/edit_item_text_tab"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</TabHost>
</LinearLayout>
Source code of your activity, in this case StartActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class StartActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tab_host = (TabHost) findViewById(R.id.tab_host);
tab_host.setup();
TabSpec tabspec1 = tab_host.newTabSpec("TAB_1");
tabspec1.setIndicator("Tab 1");
tabspec1.setContent(R.id.first_tab);
tab_host.addTab(tabspec1);
TabSpec tabspec2 = tab_host.newTabSpec("TAB_2");
tabspec2.setIndicator("Tab 2");
tabspec2.setContent(R.id.second_tab);
tab_host.addTab(tabspec2);
TabSpec tabspec3 = tab_host.newTabSpec("TAB_3");
tabspec3.setIndicator("Tab 3");
tabspec3.setContent(R.id.third_tab);
tab_host.addTab(tabspec3);
tab_host.setCurrentTab(0);
}
}
Turns out to look like:
<?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="wrap_content" android:padding="1dp">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="60dp"
android:layout_alignParentBottom="true" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="1dp">
</FrameLayout>
</RelativeLayout>
Most likely you'll have to implement tabs by your own. As far as I know that's impossible with regular android tabs.
<TabWidget android:id="#android:id/tabs"
android:tabStripEnabled="false"
android:padding="0dip"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
Working with tabs in the Eclipse "graphical layout" mode is not quite intuitive.
NOTE - Currently I am not able to attach pictures to this answer as my reputation is
below 10. Maybe in the future I will edit it and add them.
1) To change tab bar height:- Select "tabs (TabWidget)" in the Outline view. A selection/resize box should appear around the tab bar and its height can now be modified. I have made it thinner in picture1.
2) To move the tab bar to bottom:- Select "tabs (TabWidget)" in the Outline view and drop it into the LinearLayout above it (as marked in the picture 1). This will send the "tabs (TabWidget)" to the bottom of the list (see picture2). Tabs Bar might disappear at this stage. So adjust the weights of "tabcontent" & "tabs (TabWidget)" till it looks ok. I have used 10 & 1 respectively.
3) To bring tab2 to the top:- After design/layout of tab1 is complete, we want to work on the next tab. But Eclipse does not allow selecting it. To bring tab2 to the top select tab1 and drop it into tabcontent. This will send tab1 to the bottom of the list and tab2 to the top.
Later when work on tab2 is complete, tab3 can be brought up.
This pretty roundabout way of working in graphical layout mode but maybe better than typing xml code.
Cheers