NRahman
NRahman

Reputation: 2757

Add extra text in Tab in Android Tab

Add Extra Text in This Header of Text On some action in Activity

I want to add some text in the Header inside the red circle in Tab in Android. To make the Tab I used the following code.

MyActivity.Java

public class MainActivity extends TabActivity implements OnTabChangeListener {
    TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabHost.TabSpec spec;

        tabHost = getTabHost();
        // Android tab
        Intent intentAndroid = new Intent().setClass(this, Aclass.class);
        spec = tabHost.newTabSpec("Android").setContent(intentAndroid)
                .setIndicator("My Profile ");
        tabHost.addTab(spec);
        tabHost.setOnTabChangedListener(this);

        // Welcome tab
        Intent intentBus = new Intent().setClass(this, Bclass.class);
        spec = tabHost.newTabSpec("Welcome").setIndicator("Card View")
                .setContent(intentBus);
        tabHost.addTab(spec);

        tabHost.getTabWidget().getChildAt(1)
                .setBackgroundResource(R.drawable.two);

        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0)
                .setBackgroundResource(R.drawable.one);

    }

    @Override
    public void onTabChanged(String tabId) {

        Log.i("tabs", "CurrentTab: " + tabHost.getCurrentTab());

        if (tabHost.getCurrentTab() == 0) {
            tabHost.getTabWidget().getChildAt(0)
                    .setBackgroundResource(R.drawable.one);
            tabHost.getTabWidget().getChildAt(1)
                    .setBackgroundResource(R.drawable.two);
        }

        if (tabHost.getCurrentTab() == 1) {
            tabHost.getTabWidget().getChildAt(0)
                    .setBackgroundResource(R.drawable.one_hover);
            tabHost.getTabWidget().getChildAt(1)
                    .setBackgroundResource(R.drawable.two_hover);
        }

    }
}

The XMl Code is

activity_main.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">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:tabStripEnabled="false"
            android:showDividers="none"
            />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
</TabHost>

Upvotes: 0

Views: 300

Answers (1)

KP_
KP_

Reputation: 1866

Use like this ---------with the help of layoutInflater

 TabSpec passSpec = tabs.newTabSpec("Pass Tab"); 
    passSpec.setIndicator("Passengers", getResources().getDrawable(R.drawable.tab_message));

    passSpec.setContent(new TabHost.TabContentFactory() { 
        public View createTabContent(String tag) { 
            View layout = mInflater.inflate(R.layout.tab_content_passengers, null);                 
            return(layout); 
        } 
    });
    tabs.addTab(passSpec);

Upvotes: 1

Related Questions