Search code examples
androidandroid-actionbarandroid-tabs

Tabs below action bar


I'm developing for Android 4.3 and been having a problem with my code that I can't seem to figure out. I've been looking for answers for a while and all I could find was people that want my current situation while I want their bugged program.

I have 3 tabs which I've placed in the action bar by Android's tutorial for tabs in ActionBar.

What is supposed to happen: The tabs should appear on the action bar

What happens instead: The tabs appear below the action bar

My questions are:
1. How can I set those tabs to show on the ActionBar and not below
2. If succeeding 1, how can I set the size of the tabs? for example making the 3 tabs together take one third of the ActionBar (by width)

My code:

mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        final ActionBar actionBar = getActionBar();
        actionBar.show();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                 // show the given tab
                mPager.setCurrentItem(tab.getPosition());
            }

            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
                // hide the given tab
            }

            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
                // probably ignore this event
            }
        };

        mPager.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        // When swiping between pages, select the
                        // corresponding tab.
                        actionBar.setSelectedNavigationItem(position);
                    }
                });
        // Add 3 tabs, specifying the tab's text and TabListener
        actionBar.addTab(
                    actionBar.newTab()
                            .setText("A")
                            .setTabListener(tabListener));
        actionBar.addTab(
                actionBar.newTab()
                        .setText("B")
                        .setTabListener(tabListener));
        actionBar.addTab(
                actionBar.newTab()
                        .setText("C")
                        .setTabListener(tabListener));

Solution

  • private ViewPager mPager;
    private PagerAdapter mPagerAdapter;
    private static final int NUM_PAGES = 3;
    

    in onCreate()

    final ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });
    
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
    
            @Override
            public void onTabReselected(Tab tab,
                    android.app.FragmentTransaction ft) {
    
            }
    
            @Override
            public void onTabSelected(Tab tab,
                    android.app.FragmentTransaction ft) {
                mPager.setCurrentItem(tab.getPosition());
            }
    
            @Override
            public void onTabUnselected(Tab tab,
                    android.app.FragmentTransaction ft) {
            }
    
        };
    
        for (int i = 0; i < mPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar
                    .newTab()
                    .setText(mPagerAdapter.getPageTitle(i))
                    .setIcon(
                            ((ScreenSlidePagerAdapter) mPagerAdapter)
                                    .getPageIcon(i))
                    .setTabListener(tabListener));
        }
    
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return Fragment.instantiate(MainActivity.this,
                        Fragment_sports.class.getName());
            case 1:
                return Fragment.instantiate(MainActivity.this,
                        Fragment_casino.class.getName());
            case 2:
                return Fragment.instantiate(MainActivity.this,
                        Fragment_live_betting.class.getName());
            default:
                break;
            }
            return null;
        }
    
        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            String tabLabel = null;
            switch (position) {
            case 0:
                tabLabel = " Sports";
                break;
            case 1:
                tabLabel = "Casino";
                break;
            case 2:
                tabLabel = "Live Betting";
                break;
            }
    
            return tabLabel;
        }
    
        public int getPageIcon(int position) {
            int id = 0;
            switch (position) {
            case 0:
                id = R.drawable.icon_all_sports_d;
                break;
            case 1:
                id = R.drawable.icon_favourites_d;
                break;
            case 2:
                id = R.drawable.icon_live_d;
                break;
            default:
                break;
            }
            return id;
    
        }
    }
    

    and your main_activity.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>
    

    ADD:

    You should replace this line:

    actionBar.setDisplayHomeAsUpEnabled(true);
    

    to this:

    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);