Vikas
Vikas

Reputation: 11

custom tabview in android

I want to design a page in which i just want to show only one tab. But when i add only one tab, it takes the full width available but i want that tab only takes small space and remaining space is left empty. No other tabs i want to add. Only one tab is displayed all time. Code is following

public class TabViewActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, FirstTab.class);
        // Initialize a TabSpec for each tab and add it to the TabHost

        spec = tabHost.newTabSpec("firstTab").setIndicator("First Tab")
                      .setContent(intent);


        tabHost.addTab(spec);
        tabHost.setCurrentTab(1);

Upvotes: 1

Views: 1096

Answers (2)

Harinder
Harinder

Reputation: 11944

Use tabHost.getTabWidget().getChildAt(0).getLayoutParams().width =(int) 30; also set the layout_width="wrap_content"

Upvotes: 1

blessanm86
blessanm86

Reputation: 31789

In your xml hide the tab widget by setting the visibility to gone. and instead use a button. And in the button handler you can use code like

public void tabHandler(View target){
    artistButton.setSelected(false);
    albumButton.setSelected(false);
    if(target.getId() == R.id.artist_id){
        tabHost.setCurrentTab(0);
        artistButton.setSelected(true);
    } else if(target.getId() == R.id.album_id){
        tabHost.setCurrentTab(1);
        albumButton.setSelected(true);
    }
}

Upvotes: 0

Related Questions