Custom tabs in android

Falmarri picture Falmarri · Oct 11, 2010 · Viewed 22.2k times · Source

I'm having a really hard time understanding how to use custom tabs in android. I don't want to just be able to set the text and stuff. How can I change the size, and the image, and all that.

I've googled and I can't find anything that makes sense

Answer

Sergey Glotov picture Sergey Glotov · Oct 11, 2010

You can create XML layout file in /res/layout. Then you need inflate layout in View and set indicator. I use this code in my projects:

private static View prepareTabView(Context context, int textId, int drawable) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
    // setting text and image
    // ...
    // Write your own code here
    return view;
}

public static void addTab(TabHost host, int title, String tag, int drawable, int layout) {
    TabHost.TabSpec spec = host.newTabSpec(tag);
    spec.setContent(layout);
    View view = prepareTabView(host.getContext(), title, drawable);
    spec.setIndicator(view);
    host.addTab(spec);
}