Search code examples
androidandroid-tabhost

Android compound view not showing on Fragment Tab Host


I want a FragmentTabHost which contains an ImageView and TextView in a LinearLayout.The reason for to have both ImageView and TextView,is that I need to change the selected state of both the Views on Tab Selected. This is my tab_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
  <ImageView
    android:id="@+id/img_tab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_tab_alert" />
  <TextView
    android:id="@+id/txt_tab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:singleLine="true"
    android:text="Alert" />
</LinearLayout>

And my Compound View class is TabView

public class TabView extends FrameLayout {

public ImageView imageView;
public TextView textView;

private String textValue;
private int normalDrawableId, selectedDrawableId;

public TabView(Context context, String textValue, int normalDrawableId,
        int selectedDrawableId) {
    super(context);
    this.textValue = textValue;
    this.normalDrawableId = normalDrawableId;
    this.selectedDrawableId = selectedDrawableId;

    View view = LayoutInflater.from(context).inflate(R.layout.tab_view,
            this, false);
    textView = (TextView) view.findViewById(R.id.txt_tab);
    imageView = (ImageView) view.findViewById(R.id.img_tab);
    textView.setText(textValue);
    imageView.setImageResource(normalDrawableId);
    }
  }

And from Activity class,I am doing the following

public class MainActivity extends ActionBarActivity {

private FragmentTabHost fragmentTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_layout);
    fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    fragmentTabHost.setup(this, getSupportFragmentManager(),
            R.id.realtabcontent);

    Bundle params = new Bundle();

    TabSpec tabSpec = fragmentTabHost.newTabSpec("Alert").setIndicator(
            createTabView(fragmentTabHost.getContext(), "Alert",
                    R.drawable.ic_tab_alert, R.drawable.ic_tab_alert_red));
    fragmentTabHost.addTab(tabSpec, TabAlertFragment.class, params);

    tabSpec = fragmentTabHost.newTabSpec("Community").setIndicator(
            createTabView(fragmentTabHost.getContext(), "Community",
                    R.drawable.ic_tab_community,
                    R.drawable.ic_tab_community_red));
    fragmentTabHost.addTab(tabSpec, TabCommunityFragment.class, params);


}

public TabView createTabView(Context context, String textValue,
        int normalDrawableId, int selectedDrawableId) {

    TabView tabView =new TabView(context,textValue,normalDrawableId,selectedDrawableId);
    return tabView;
}
}

But I am not being able to see any Tabs. When I simply Inflate the Layout from Activity class ,the Tab shows,but I am not being able to use Tabs with Compound Views. What am I doing wrong?


Solution

  • Ok finally I found the culprit, in my code

      View view = LayoutInflater.from(context).inflate(R.layout.tab_view,
            this, false);
    

    should be

      View view = LayoutInflater.from(context).inflate(R.layout.tab_view,
            this);
    

    By doing this the problem is solved