Adding Icons to Tab while Actionbar is active

85 Views Asked by At

I'm trying to get a "simple" UI done, but I'm stuck at where I want to put icons in my bottom tabs. So far I got the following code:

`public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    FragmentTabHost mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("AddStuff", getResources().getDrawable(R.drawable.ic_add_white_24dp)),
            FragmentTabAdd.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Favorites", getResources().getDrawable(R.drawable.ic_add_white_24dp)),
            FragmentTabSelectFavorites.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Messages", getResources().getDrawable(R.drawable.ic_add_white_24dp)),
            FragmentTabMessages.class, null);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}`

and I got the following as my .xml for each tab:

<?xml version="1.0" encoding="utf-8"?> <selector android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <!-- When selected, use black --> <item android:drawable="@drawable/ic_add_black_24dp" android:state_selected="true" /> <!-- When not selected, use white--> <item android:drawable="@drawable/ic_add_white_24dp" /> </selector>

I worked through several different tutorials, but none had an Actionbar and a Tabbar with icons. I'm happy for any tipps and suggestions.

Thank you in advance!

1

There are 1 best solutions below

0
pgalle On

I found the answer myself after several hours of trying different things.

The way to add Icons to the bottom tab bar is to insert "null" here:

mTabHost.addTab(
        mTabHost.newTabSpec("tab1").setIndicator("AddStuff", getResources().getDrawable(R.drawable.ic_add_white_24dp)),
        FragmentTabAdd.class, null);

null has to be written where "AddStuff" is now. I even colored the background with an image:

mTabHost.setBackground(getResources().getDrawable(R.drawable.tab_background));

Maybe this will help someone else save some time :)