3

image

I don't know why action bar and title is two-line. Please help me make that on the same 1-line.

Source code is below:

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

    mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());

    final ActionBar actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    for (int i = 0; i < mPagerAdapter.getCount(); i++)
        actionBar.addTab(actionBar.newTab().setText(mPagerAdapter.getPageTitle(i)).setTabListener(this));
}
tckmn
  • 57,719
  • 27
  • 114
  • 156
NHK
  • 769
  • 1
  • 7
  • 16

2 Answers2

0

The line under your title is the TAB navigation, although it is an ActionBar feature, from what I understand, you think (and I agree) it's not part of the "main" ActionBar.

If you want some sort of navigation on the main ActionBar, you can do it by adding action items on your res/menu/main.xml, for example with IDs action_next and action_previous. If you're doing this, you can remove

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

And then you can add this to your Activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.action_previous:
        mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
        return true;

    case R.id.action_next:
        mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
        return true;
    }

    return super.onOptionsItemSelected(item);
}
bytehala
  • 665
  • 1
  • 10
  • 25
  • Thanks reply. But why [Android Effective Navigation Sample](http://developer.android.com/training/implementing-navigation/ancestral.html) title and action bar is 1-line? (I tested Nexus 7 tablet) It has no menu. Maybe other way can fix this. – NHK Sep 14 '13 at 16:38
  • I found the reason. **android:targetSdkVersion** may affect this. When I targetSdk is 18 or 17, then 2-line menu displayed. But when not type what targetSdk is, it displays 1-line menu. – NHK Sep 14 '13 at 17:00
  • 1
    I tested EffectiveNavigation on my emulator, and on a tablet configuration, it's one line. On a phone configuration, it's two lines. It might be related to "Split action bar", where the system adjusts the number of "lines" depending on how much space is available. http://developer.android.com/guide/topics/ui/actionbar.html – bytehala Sep 14 '13 at 17:51
0

If you want the tabs on the main ActionBar, you can use ActionBar.setCustomView! to supply a custom navigation view which has title and the custom tab navigation.

LT21
  • 1