0

I want to set a background image for each tab in my application. I tried applying a icon but it wont fill the tabs. Then I tried putting a layout. It covers the all height but width isn't enough.

Below is my code Please help to fill a tab with a background image.

viewpager = (ViewPager) findViewById(R.id.pager);
    FragmentPageAdapter ft = new FragmentPageAdapter(getSupportFragmentManager(), getApplicationContext());

    actionBar = getActionBar();

    viewpager.setAdapter(ft);

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.addTab(actionBar.newTab().setCustomView(R.layout.a_selected).setTabListener(this)); // trying to apply a layout as background
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon_1).setTabListener(this));//applying a icon as background
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon_2).setTabListener(this));//applying a icon as background
tenten
  • 1,276
  • 2
  • 26
  • 54

2 Answers2

0

Try to use

 actionBar = getSupportActionBar(); 

instead of

 actionBar = getActionBar();
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
  • getSupportActionBar(); only works when you are using sharlockactionbar other then that simply getActionBar(); works always. – Samarth Sevak May 30 '16 at 04:48
0

ActionBar

final ActionBar actionBar = getActionBar(); 
 BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.raw.actionbar_background)); 
 background.setTileModeX(android.graphics.Shader.TileMode.REPEAT); 
 actionBar.setBackgroundDrawable(background);

For setting background color in Tab Host Actionbar

public static void setTabColor(TabHost tabhost) {
    for(int index=0;index<tabhost.getTabWidget().getChildCount();index++) {
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#477a47")); //unselected
    }
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}

For setting image Tab Host Actionbar

But if you register for TabHost.OnTabChanged events and call mTabHost.getCurrentTabView() to get the View, then use view.setBackgroundResource().

tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
Samarth Sevak
  • 521
  • 4
  • 16
  • Oh sorry dear but i think TabHost is the easy way to do this. kindly follow this url : http://www.viralandroid.com/2015/09/simple-android-tabhost-and-tabwidget-example.html – Samarth Sevak May 30 '16 at 05:15
  • Is there any way to set action bar tab background(above code applies a background for action bar) – tenten May 30 '16 at 05:46
  • No I can't think so.. but you can do it with TabHost that for sure. – Samarth Sevak May 30 '16 at 05:54