This is a problem that has troubled me for a few days now. I’ve done my best searching for a solution, but everywhere I go it says the approach I’m currently using is the right one.
The situation is simple: I have five tabs, which are to contain the same layout with slightly different content (for example by using setText to change one of the labels). As such, I am inflating the layout five times, and adding all of them as seperate tabs.
This is my code:
package com.test;
import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
public class TestActivity extends TabActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost host = getTabHost();
Resources res = getResources();
String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
for (int i = 0; i < days.length; i++)
{
View contents = getLayoutInflater().inflate(R.layout.test, host.getTabContentView(), true);
((TextView)contents.findViewById(R.id.title)).setText(days[i]);
host.addTab
(
host.newTabSpec(days[i]).
setIndicator(days[i]).
setContent(contents.getId())
);
host.getTabWidget().getChildAt(i).getLayoutParams().height = 55;
}
}
}
And my layout file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="6dip">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
The problem is that, instead of the text being on seperate tabs, all the text overlaps eachother and is the same for every tab. It might be something really simple that I’m overlooking. Any help is appreciated.
They ain’t overlapping each other from my discovery. If you remove
android:text="Test"from your layout file, you will be seeing only Friday in all tab.It looks like you will need to use
TabContentFactoryto achieve what you want. Here is my solution to your problem. It will give you what you needed.Here is the link where I get started https://github.com/commonsguy/cw-android/tree/master/Fancy/DynamicTab
Credit goes to Mark Murphy(Commonsguy).