Custom Android tabs

Source: Internet
Author: User

Due to limitation of Android tab component I created a custom tabwidget that I am using in couple different projects already. the widget allows us to add custom background and use custom icons, tabs can be top/bottom aligned.

Currently tabs can launch new activity and dialog, when starting new activity we can use "startactivityforresult" so our tab will get a notification when other activity have finished.

This is not a perfect solution as there are some drawbacks to it but it suits me well so I will stick to it till something better comes along; hint Android gurus we need to be able to start new activities inside existing tab with more mizmization.

Creating a tab

There a two thing that a tab needs

  • Icon: either R. drawable. ID or drawable object
  • An activity set via intent or dialog
Tab homeTab=new Tab(context, "HOME");homeTab.setIcon(R.drawable.home);homeTab.setIconSelected(R.drawable.home_selected);homeTab.setIntent(new Intent(context, CarmeLauncher.class));
What is a tabhost?

Tabhost host tabs and specify how they will be rendered, tabviewconfig is used to configure the tabhost

TabHost tabHost=new TabHost(new TabViewConfig().context(context).headerResourceId(R.drawable.tab_background_55).separatorId(R.drawable.separator).orientation(TabHost.Orientation.BOTTOM));

I use fluent interfaces or as some call it builder pattern for processing ing tabhost I think its straight forward and intuitive.

Binding tabs to activity
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//Our Tab providerTabHostProvider tabProvider=new CarmeTabProvider(this);TabHost tabHost=tabProvider.getTabHost("main");//This is the content of the tabtabHost.setCurrentView(R.layout.main);setContentView(tabHost.render());}

To make Tab selected we need to retrieve it using tab assigned tag name in this case "home" beforesetContentView(tabHost.render())Is called.

Tab home=tabHost.getTab("HOME");home.setSelected(true);setContentView(tabHost.render());
Download/view Demo project

Tabwidgetdemo

Carme project using tabs bottom aligned [view with piclens]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.