Search code examples
androidandroid-tabhost

How to change TabHost Title?


I'm trying change the text of Tabs of TabHost to text make it on singleLine=true. To do it I'm trying create TextView and add this attribute singleLine. The problem is I can`t insert this TextView in TabHost.

How could I do this ?

I'm trying this.

XML TabHost

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight="0"
            android:layout_gravity="bottom">
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

</android.support.v4.app.FragmentTabHost>

TextView

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:id="@+id/perfil"
        android:text="teste de aba para tab host"
        />

</LinearLayout>

Activity

public class AreaAlunoMainActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.areadoaluno_main);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(
                mTabHost.newTabSpec("perfil").setIndicator("Perfil", null).setContent(R.id.perfil),
                PerfilFrag.class, null);

    }
}

Solution

  •     TabHost.TabSpec spec = mTabHost.newTabSpec(TAB_A);
        spec.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
                return findViewById(android.R.id.tabcontent);
            }
        });
         spec.setIndicator(createTabView(TAB_A, R.drawable.salonsphp));
        mTabHost.addTab(spec);
    
    
        private View createTabView(final String text, final int id) {
             View view =LayoutInflater.from(this).inflate(R.layout.yourcustomlayout,null);
            ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon);
            imageView.setImageDrawable(getResources().getDrawable(id));
            ((TextView) view.findViewById(R.id.tab_text)).setText(text);
            return view;
        }
    
    you can set specifi. of tab like this.....