-1

I am Building an android application where I am using 4 tablayout.

Now I wanna to set an image in place of text in handing of tablayout.

I am using code of this link - http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

Here is my code -

MainActivity.java
package info.androidhive.tabsswipe; 

    public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
   private String[] tabs = { "Top Rated", "Games", "Movies" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}

Here is my fragment code -

package info.androidhive.tabsswipe;

public class MoviesFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_movies, container, false);

    return rootView;
}

}
Hitesh Matnani
  • 533
  • 2
  • 8
  • 26

1 Answers1

0

change this:

for (String tab_name : tabs) {
    actionBar.addTab(actionBar.newTab().setText(tab_name)
            .setTabListener(this));
}

to this:

for (String tab_name : tabs) {
    actionBar.addTab(actionBar.newTab().setText("")
            .setTabListener(this).setIcon(R.drawable.YOUR_ICON));
}
Rami
  • 7,879
  • 12
  • 36
  • 66
  • I had try this but when I setIcon an image it is set for my all tabhost layout icon can you please tell me what to do to solve this problem !! – Hitesh Matnani Nov 11 '14 at 17:12
  • you have 3 tabs, so instead of looping you can do: actionBar.addTab(actionBar.newTab().setText("") .setTabListener(this).setIcon(R.drawable.TOPRATED_ICON)); actionBar.addTab(actionBar.newTab().setText("") .setTabListener(this).setIcon(R.drawable.GAMES_ICON)); actionBar.addTab(actionBar.newTab().setText("") .setTabListener(this).setIcon(R.drawable.MOVIES_ICON)); – Rami Nov 11 '14 at 17:19
  • @thanks it is done but still the images are coming in center I need my total space of tabhost should be fill by my icon image – Hitesh Matnani Nov 11 '14 at 17:48
  • Yes! I will accept the answer after removal of this error can you please tell me or any other solution so that I can the background color of tabhost – Hitesh Matnani Nov 11 '14 at 17:50
  • if you want the image to take total space, you should create a style for the tabview that clears the background and the padding and use it in your theme. for the actionBar background color, see this solution: http://stackoverflow.com/a/12487457/4224337 – Rami Nov 11 '14 at 17:53
  • can you please tell me how to create style for tabview – Hitesh Matnani Nov 11 '14 at 17:59