Search code examples
javaandroidandroid-layoutactionbarsherlock

ActionBar's Navigation Mode Changes during ConfigChanges


I am using actionbarsherlock's ActionBar, the first time you run the application in landscape or horizontal orientation the tabs is displayed in Tabs Mode. After changing the orientation to portrait the nav mode is still in Tabs. The second time changing the orientation to horizontal the tabs are displayed as a List and only the parent item is displayed and the rest are hidden. I heard that this is a bug to Android, the system automatically changes the nav mode to list if the tabs are too many to fit the screen. But the thing is the first time I load the application is it displayed as tabs mode even horizontally. By the way I created 100 tabs.

This image is in horizontal view. The first image from the top is just the result after I change the phone's orientation to portrait then go back to landscape.
enter image description here


I have this code from my Manifest File,

android:configChanges="orientation|screenSize"

Main Activity

 public void onConfigurationChanged(Configuration newConfig) {
         ActionBar ab = getSupportActionBar();
             ab.setDisplayShowTitleEnabled(false);

             if(ab.getNavigationMode() != ActionBar.NAVIGATION_MODE_TABS){
                 ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

             }
        super.onConfigurationChanged(newConfig);
}

Just to make sure I also put the code, somewhere near the setContentView

        final ActionBar ab = getSupportActionBar();
        ab.setDisplayShowTitleEnabled(false);
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

UPDATE:

The reason why I am handling the onConfigurationChanged because I got a Null Pointer Exception.

Logcat

08-05 15:36:18.486: E/AndroidRuntime(30044): FATAL EXCEPTION: main
08-05 15:36:18.486: E/AndroidRuntime(30044): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.actionbarsherlock.sample.styled/com.actionbarsherlock.sample.styled.MainActivity}: java.lang.NullPointerException
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3512)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.access$700(ActivityThread.java:130)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.os.Looper.loop(Looper.java:137)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.main(ActivityThread.java:4745)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at java.lang.reflect.Method.invokeNative(Native Method)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at java.lang.reflect.Method.invoke(Method.java:511)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at dalvik.system.NativeStart.main(Native Method)
08-05 15:36:18.486: E/AndroidRuntime(30044): Caused by: java.lang.NullPointerException
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:504)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:410)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:538)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.sample.styled.MainActivity.rotateLeftFrag(MainActivity.java:218)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.sample.styled.MainActivity.onTabSelected(MainActivity.java:256)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabSelected(ActionBarWrapper.java:356)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.android.internal.app.ActionBarImpl.setSelectedNavigationItem(ActionBarImpl.java:356)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.android.internal.app.ActionBarImpl.setNavigationMode(ActionBarImpl.java:1158)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.internal.app.ActionBarWrapper.setNavigationMode(ActionBarWrapper.java:216)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.sample.styled.MainActivity.showTabsNav(MainActivity.java:244)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at com.actionbarsherlock.sample.styled.MainActivity.onCreate(MainActivity.java:82)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.Activity.performCreate(Activity.java:5008)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-05 15:36:18.486: E/AndroidRuntime(30044):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-05 15:36:18.486: E/AndroidRuntime(30044):    ... 12 more

Solution

  • Its a default behavior of the tabs in android. If there is no more space for tabs in horizontal/portrait orientation then it will converted to list.

    Now, if you still want to have all the tabs not in list then what i suggest is to look at this sample of ViewPager after that you should have to add that FragmentActivity in to your SherlockFragmentActivity. Just check below Example Code what i am doing which will help you to have all tabs with any orientation.

    public class TestSampleTabDefault extends SherlockActivity{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        }
        @SuppressLint("NewApi")
        public static class SampleTabsDefault extends FragmentActivity implements ServiceConnection {
            private static final String[] CONTENT = new String[] { "Albums", "Artists", "Songs", "Playlist","Genre","Recently Played","Easy Transfer" };
            Handler mHandler;
            //Runnable mRunnable;
            private ServiceToken mToken;
            private IRemoteService  mIRemoteService;
            public static ViewPager pager;
            public static int index = 0;
            public static android.app.ActionBar mActionBar;
    
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.simple_tabs);
    
                //getSupportActionBar().setHomeButtonEnabled(true);
                //ActionBar bar = getSupportActionBar();
                //bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                //bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
    
                FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager());
    
                pager = (ViewPager)findViewById(R.id.pager);
                pager.setAdapter(adapter);
    
                /*getSupportActionBar().setTitle(name);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);*/
    
                TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
                indicator.setViewPager(pager);
    
                index = getIntent().getExtras().getInt("index");
                if(index >0){
                    indicator.setCurrentItem(index);
                }
    
            }
    
            class GoogleMusicAdapter extends FragmentPagerAdapter {
                 private final FragmentManager mFragmentManager;
                 private Fragment mFragmentAtPos0;
                public GoogleMusicAdapter(FragmentManager fm) {
                    super(fm);
                    mFragmentManager = fm;
                }
                @Override
                public Fragment getItem(int position) {
                   // return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
                    Fragment fm = null;
    
                    if(position==0){// LOAD ALBUMS
                        fm = ArtistList.ArtistListFragment.newInstance();
                        return fm;
    
                    }else if(position==1){// LOAD ARTISTS
                        //Toast.makeText(getApplicationContext(), "Select Album", Toast.LENGTH_SHORT).show();
                        fm = ArtistList.ArtistListFragment.newInstance();
                        return fm;
                    }else if(position==2){// LOAD SONGS
                        fm = SongsList.SongsListFragment.newInstance(); 
                        return fm;
                    }else if(position==3){   // PLaylist
                        fm = PlaylistList.PlayListFragment.newInstance();
                        return fm;
                    }else if(position==4){ // Genre List
                        fm = GenreList.GenreListFragment.newInstance();
                        return fm;
                    }else if(position==5){ // Recent List
                        fm = RecentAddedList.RecentListFragment.newInstance();
                        return fm;
                    }else{
                        fm = TestFragment.newInstance(CONTENT[position]);
                    }
                    return fm;
    
    
                }
    
                @Override
                public CharSequence getPageTitle(int position) {
                    return CONTENT[position % CONTENT.length].toUpperCase();
                }
    
                @Override
                public int getCount() {
                  return CONTENT.length;
                }
    
                @Override
                public int getItemPosition(Object object)
                {
                    if (object instanceof AlbumListFragment && mFragmentAtPos0 instanceof TestFragment)
                        return POSITION_NONE;
                    return POSITION_UNCHANGED;
                }
    
            }
    
        }
    }
    

    Hope this will help you. All The Best. Let me know if you get any difficulty in that.