TabHost/TabWidget views not centering in parent - java

Have a TabHost that doesn't center the views even though I set the views layout_gravity to center_horizontal. Here is my XML, the button is all I've added to the tab1 layout so far.
<?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/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent" android:id="#+id/linearLayout1" android:layout_height="fill_parent" android:orientation="vertical">
<TabWidget android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#android:id/tabcontent">
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/tab1">
<Button android:layout_width="wrap_content" android:id="#+id/button1" android:layout_gravity="center_horizontal" android:text="Button" android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/tab2"></LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/tab3"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>

you have to set gravity at the parent!

Try setting the android:gravity attribute on the parent of the button instead.
Like this:
<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/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent" android:id="#+id/linearLayout1" android:layout_height="fill_parent" android:orientation="vertical">
<TabWidget android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#android:id/tabcontent">
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/tab1" android:gravity="center_horizontal">
<Button android:layout_width="wrap_content" android:id="#+id/button1" android:text="Button" android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/tab2"></LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/tab3"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost></LinearLayout>

Related

Changing color of TabHost/TabWidget

I'm having a really hard time changing the color of my TabWidget. I've looked at several posts and tried them out, but none of them worked for me. I'm trying to change the default (blue) color scene to green.
Image of my app
My xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/relativeLayout">
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabHost"
android:layout_alignParentTop="true"
android:layout_alignParentStart="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"
android:clickable="true"
android:tabStripEnabled="false">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="#+id/listFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
The way to change the color of the tabs programmatically is by using
for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
{
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#00304E"));
}
To change the color of the currently selected tab,
mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab())
.setBackgroundColor(Color.parseColor("#008EE8"));

Adding containers and action bar to an Android Activity.

I am trying to add action bar on top, and two containers below the action bar, each of the containers I want to have 80% of width and 20% of width.
Here is my XML file which is not working:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hany_action_bar.MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:id="#+id/menu_container"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:weightSum="20"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/main_container"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:weightSum="80"
android:orientation="horizontal" >
</LinearLayout>
</FrameLayout>
Can try like this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
tools:context="com.example.hany_action_bar.MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:id="#+id/menu_container"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.6"
android:background="#000"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/main_container"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.4"
android:background="#fff"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>

Arrange Screen in 3x3 pattern

I like to arrange the screen in 3 colomns and 3 rows in my android application.
all nine squares should always be the same size. Each square should be able to contain a picture or video which should not get cut in size.
I already have a fully working solution, but this one just works on a specific screen size, because I hardcoded the layout_withd and layout_heigth attributes. I have worked with a Tablelayout containing three TableRows and each containing three LinearLayouts.
Can you please give me some advice how to start this in a proper way? It should work on all devices.
Thanks for help
Try this
<LinearLayout
android:orientation="horizontal"
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:id="#+id/linearLayout2" android:layout_weight="1">
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"/>
</LinearLayout>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/linearLayout" android:layout_weight="1">
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"/>
</LinearLayout>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/linearLayout2" android:layout_weight="1">
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

adview on every tab (tabhost, admob)

Im trying to get the Google Ads adview on every tab of my tabhost, but its not working.
If I place the adview in the main.xml, I can see the adview on every tab.
But the problem then is that the views (buttons) are showing above the adview, so I cant see the ad anymore?
Main.xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
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: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" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include layout="#layout/tab1" />
<include layout="#layout/tab2" />
<include layout="#layout/tab3" />
<include layout="#layout/tab4" />
</FrameLayout>
</LinearLayout>
</TabHost>
</TableLayout>
</LinearLayout>
tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res/com.this.app"
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="xxx" />
<ScrollView
android:id="#+id/scrollview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/adView" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableRow>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Lets try this layout.It is always below the frame layout.Hope it helps.
<?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="#fff" >
<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="#+id/tv"
android:layout_below="#android:id/tabs" >
</FrameLayout>
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Ads" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#android:id/tabcontent"
android:layout_alignParentTop="true"
android:paddingTop="-10dip" >
</TabWidget>
</RelativeLayout>
</TabHost>

Error during post inflation process for TabHost

I have a FrameLayout inside of a TabHost. I am getting the following error. How can I get it to render in Eclipse?
Error during post inflation process:
The FrameLayout for the TabHost has no content. Rendering failed.
Here is my 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">
<!--
<RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/actionbarRelativeLayout">
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/icon" android:id="#+id/stocktwitsImageButton"></ImageButton>
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/icon" android:id="#+id/composeImageButton" android:layout_alignParentRight="true"></ImageButton>
</RelativeLayout>
-->
<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>
This is extract from Android's source setupTabHost method:
final int count = content.getChildCount();
if (count == 0) {
throw new PostInflateException(
"The FrameLayout for the TabHost has no content. Rendering failed.\n");
}
As the exception says, you need to have content. Refactor the XML to:
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tab1"></LinearLayout>
</FrameLayout>

Categories