0

I'm implementing ActionBar tabs according to official guide.

I have a fragment that is added to activity. In that fragment I'm creating tabs:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(false);

    Tab tab = actionBar.newTab()
                       .setText("One")
                       .setTabListener(new TabListener<DemoFragment>(
                               this, "1", DemoFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
                   .setText("Two")
                   .setTabListener(new TabListener<DemoFragment>(
                           this, "2", DemoFragment.class));
    actionBar.addTab(tab);
}

But these tabs are never displayed. Nor content of DemoFragment. Even though at runtime getActionBar().getTabCount() returns correct count of tabs. And I see that DemoFragment is initialized. All I see is ActionBar.

What am I doing wrong?

I'm not using any support libraries as I'm developing for minSdk=14.

Martynas Jurkus
  • 9,231
  • 13
  • 59
  • 101

1 Answers1

1

Update

IF YOUR NOT USING ANY SUPPORT LIBRARY THIS SHOULD WORK otherwise you will have to use the getSupportActionBar() and above mentioned dependencies.

Try something more like this:

// Get the Instance of the Action Bar, set Navigation Mode, remove title
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);

// One tab
actionBar.addTab(actionBar.newTab() .setText("One")
                   .setTabListener(new TabListener<DemoFragment>(
                           this, "1", DemoFragment.class)));
// Two tab
actionBar.addTab(actionBar.newTab()
               .setText("Two")
               .setTabListener(new TabListener<DemoFragment>(
                       this, "2", DemoFragment.class)));

Edit to Post - Working Code that I have used for a demo

Here is code from a demo Application That i wrote using Android sdk 14 Just like you are.

public class MainActivity extends Activity {

 // String Titles
static String [] titles = {"Fragment 1", "Fragment 2"};
static String [] fragmentClasses = {Fragment1.class.getName(), Fragment2.class.getName()};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SimpleTabListener.SetUpTabNavigation(this, fragmentClasses, titles);
}

And here is the SimpleTabListener Class

public class SimpleTabListener implements TabListener {


Context m_context;
String m_fragmentClassName ;
Fragment m_fragment = null;


public  SimpleTabListener(Context context, String tabFragmentClassName)
{
    m_context = context;
    m_fragmentClassName = tabFragmentClassName;
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) 
{
    // TODO Auto-generated method stub


}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
    if(m_fragment == null)
    {
        m_fragment = Fragment.instantiate(m_context, m_fragmentClassName);
        ft.add(android.R.id.content, m_fragment);
    }else
    {
        ft.attach(m_fragment);
    }

}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) 
{
    // TODO Auto-generated method stub
    if(m_fragment != null){
        // TODO Auto-generated method stub
        ft.detach(m_fragment);
    }
}


public static void SetUpTabNavigation(Activity activity, String [] classNames, String [] tabTitles){

    ActionBar actionBar = activity.getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.TabListener listener;

    for(int i = 0; i <  tabTitles.length; i++)
    {
        listener = new SimpleTabListener(activity, classNames[i]);
        actionBar.addTab(actionBar.newTab().setText(tabTitles[i]).setTabListener(listener));

    }

}

}

Frament1.class

public class Fragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment_1, container, false);
}

}

Fragment 2.class

public class Fragment2 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment2, container, false);

}

}
kandroidj
  • 13,784
  • 5
  • 64
  • 76
  • I'm developing for Android 4.0 and up, so `ActionBarActivity` is not needed. And suggested solution changes nothing. I've updated my question. – Martynas Jurkus Feb 20 '14 at 14:54
  • okay, then if thats the case. Remove the getSupportActionBar() and use getActionBar(); Your problem looks like you are not getting an actionbar object back. Edit above ^ If you are not using any compatibility library the getSupportActionBar() call will return null. Also, you do not need to declare the ActionBarActivity for your activity then. – kandroidj Feb 20 '14 at 14:58
  • Please, read the question. If I woudn't get the ActionBar tabs would not be initiated and I would get NPE's. – Martynas Jurkus Feb 20 '14 at 15:00
  • Can you please post your Activity Class then ? I need to see more than were you set up the tabs. – kandroidj Feb 20 '14 at 15:01
  • That was my mistake. My nested fragments overridden ActionBar navigation mode and Tabs were never shown. – Martynas Jurkus Feb 21 '14 at 10:37
  • Nice, good job fixing it. Tabs are super useful, and implementing them is a little tricky. If you ever need help with ViewPager as well, let me know. Good luck. – kandroidj Feb 21 '14 at 13:40
  • Actually I do - http://stackoverflow.com/questions/21932541/fragment-view-in-viewpager-is-not-restored-when-resuming – Martynas Jurkus Feb 21 '14 at 14:44