Darko Petkovski
Darko Petkovski

Reputation: 3912

Styling actionbar tabs - the easy way

I'm trying to apply a custom style to the actionbar tabs, but for now without any success. I know there are many tutorials out there, but can someone explain how it should be done or provide a link to a good tutorial or documentation on how can I implement custom styling.

Here is the code I have:

ActionBar actionBar = getActionBar();
Tab1 = actionBar.newTab().setText("TAB1");
        Tab2 = actionBar.newTab().setText("Tab2");
        // Set Tab Listeners
        Tab1.setTabListener(new TabListener(fragmentTab1));
        Tab2.setTabListener(new TabListener(fragmentTab2));
        // Add tabs to actionbar
        actionBar.addTab(Tab1);
        actionBar.addTab(Tab2);

What Im trying to achieve:

enter image description here

Upvotes: 0

Views: 85

Answers (1)

Daiwik Daarun
Daiwik Daarun

Reputation: 3974

Here is a snippet of code I wrote for one of my projects, you should be able to easily adjust it for your own needs!

//@param string - the R.string.name_of_string, I use it for convenience
private ActionBar.Tab createCustomTab(ActionBar actionBar, int string){
    View tabView = getLayoutInflater().inflate(R.layout.tab, null);

    ((TextView) tabView.findViewById(R.id.txt_tab)).setText(string);

    //in my case I wanted to center the layout - for some reason certain layout params do not work in the XML,
    //  (such as centering), so I do that here
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.CENTER;
    tabView.setLayoutParams(lp);

    return actionBar.newTab().setCustomView(tabView);
}

Let me know if you have any questions!

Upvotes: 1

Related Questions