Skip to content Skip to sidebar Skip to footer

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 tha

Solution 1:

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);

Solution 2:

Edit: For tabs, use this part of the API guides.

For adding elements to your ActionBar, you must Override onCreateOptionsMenu()

For example:

@Override

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

And you should have an xml for the layout of those items:

res/menu/main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"/>
    <item android:id="@+id/action_compose"
      android:icon="@drawable/ic_action_compose"
      android:title="@string/action_compose" />
</menu>

Source


Post a Comment for "Tabs Below Action Bar"