-2

I'm new to android .I am now testing a TabView with some text and Image . Image show but the text don't . android SDK version is 4.4.3 . Help me finding it please . I looked at many other places but none of it work for me .

This is the code :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabHost tabHost =(TabHost) findViewById(R.id.tabHost);
    tabHost.setup();


    TabHost.TabSpec spec=tabHost.newTabSpec("mitab1");
    spec.setContent(R.id.tab1);
    //spec.setIndicator("",getResources().getDrawable(android.R.drawable.ic_menu_rotate));
    spec.setIndicator(prepareTabView("ABCD", R.drawable.talk));


    TabHost.TabSpec spec1=tabHost.newTabSpec("mitab1");
    spec1.setContent(R.id.tab2);
    spec1.setIndicator(prepareTabView("CDEF", R.drawable.ball));



    tabHost.addTab(spec);
    tabHost.addTab(spec1);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


private  View prepareTabView(String text, int resId) {
    View view = LayoutInflater.from(this).inflate(R.layout.tabcustom, null);
    ImageView iv = (ImageView) view.findViewById(R.id.TabImageView);
    TextView tv = (TextView) view.findViewById(R.id.TabTextView);
    iv.setImageResource(resId);
    tv.setText(text);
    return view;

}

This is tabcustom.xml:

 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
 android:id="@+id/TabLayout"
 android:padding="5dip"
 android:gravity="center"
android:orientation="vertical" >


    <ImageView
         android:id="@+id/TabImageView"
         android:src="@drawable/football"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>

    <TextView
         android:id="@+id/TabTextView"
         android:text="Text"
         android:paddingTop="5dip"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textColor="#ffffff"
        />

Naddy
  • 2,664
  • 6
  • 25
  • 38
Chann Lynn
  • 201
  • 1
  • 3
  • 14

1 Answers1

0

layout's orientation is Vertical thats why text will be show at bottom of the tab & change text color if tab background color is same as text color. You can use below code for showing center text with background image.

Change xml:

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:id="@+id/TabLayout"
 android:padding="5dip"
 android:gravity="center"
 android:orientation="vertical" >


    <TextView
         android:id="@+id/TabTextView"
         android:text="Text"
         android:paddingTop="5dip"
         android:layout_width="fill_parent"
         android:gravity="center"
         android:layout_height="wrap_content"
         android:textColor="#ffffff"
        />

Class:

private  View prepareTabView(String text, int resId) {
    View view = LayoutInflater.from(this).inflate(R.layout.tabcustom, null);
    LinearLayout iv = (LinearLayout) view.findViewById(R.id.TabLayout);
    TextView tv = (TextView) view.findViewById(R.id.TabTextView);
    iv.setImageResource(resId);
    tv.setText(text);
    return view;}
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
  • sir Please answer this question [link](http://stackoverflow.com/questions/20988505/open-child-activity-in-tab-host-android) – Bishnu Kumar Jan 08 '14 at 07:19