1

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));
Mohammad Olfatmiri
  • 1,605
  • 5
  • 31
  • 57
user2558461
  • 281
  • 1
  • 6
  • 21
  • Are you testing on a tablet or phone? ActionBar tabs show up below the ActionBar on smaller screens. Check out figure 7 & 8 here: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs – blackcj Aug 20 '13 at 19:04
  • I'm testing on phone, when I rotate my device the tabs show in the AcitonBar. I wish this to happen always. – user2558461 Aug 20 '13 at 19:44

2 Answers2

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);
WOLVERINE
  • 769
  • 3
  • 12
  • 28
0

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

Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
  • I want the ActionBar to be filled with those tabs only (A,B,C) but I don't want each to take one third of the ActionBar -> taking the entire ActionBar space causing it to display another line. I want the entire width of all tabs to take one third of the ActionBar – user2558461 Aug 20 '13 at 21:05