1

I am trying to create an app with the Navigation Drawer using the sample Navigation Drawer app from within Android Studio. My first fragment that I am trying to create from the navigation drawer has 3 tabs and I'm trying to use FragmentTabHost to switch between the tabs and load a slightly different fragment below each tab.I want the user to navigate the main sections of the app from the Navigation Drawer and on some screens I want to have sub-level navigation using TabHost. I basically want to use something like the Two Level navigation that is recommended on the Android Developers site here: https://www.google.com/design/spec/patterns/navigation.html#navigation-two-levels

I found some examples of how to use FragmentTabHost here: http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html

The first example doesn't work because the FragmentTabHost is extending FragmentActivity but the Navigation Drawer code appears to only invoke Fragments

Trying to implement the second example using nested fragments is giving me an error the following error:

Error:(106, 17) error: no suitable method found for setup(FragmentActivity,FragmentManager,int) method TabHost.setup(LocalActivityManager) is not applicable (actual and formal argument lists differ in length) method TabHost.setup() is not applicable (actual and formal argument lists differ in length)

at:

mTabHost.setup(getActivity(), getChildFragmentManager(), 
            R.layout.fragment_tab_host);

Here is a snippet from my MainActivity.java:

public class MainActivity extends ActionBarActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks{

    private NavigationDrawerFragment mNavigationDrawerFragment;

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new fragment_tabHost();
                break;
            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                .replace(R.id.container, fragment).commit();
            // update selected item and title, then close the drawer

        } else {
            Log.e("MainActivity", "Error in creating fragment");
        }
    }
}

Here a snippet of the code for the fragment_tabhost.java which is called when you click the first item in the navigation drawer:

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;

public class fragment_tabHost extends Fragment {

    private TabHost mTabHost;    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager(),  
            R.layout.fragment_tab_host);

        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
            tab1.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
            tab2.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
            tab3.class, null);

        return mTabHost;

    }
}

I am only targeting a minimum SDK of API level 15 so I am open to a better way to do this if one exists.

JoeDaddy7105
  • 31
  • 1
  • 6

1 Answers1

0

This sample app which ships with the android Studio seems to be aged. I would recommend that you use the DrawerLayout. You will find an official sample from google here.

If you want a full example with the newest API take look at this github sample.

For your case the classes, DrawerActivity, TabHolderFragment and TabFragment will be intressting.

swissonid
  • 1,025
  • 1
  • 10
  • 22
  • I am trying to work the github sample methods into my code but I am having some issues. I see a lot of references to a library called ButterKnife. I would prefer to avoid 3rd party libraries so I am trying to work around the various Butterknife references but I don't see how I can replace the @InjectView references with conventional Android code because it is unclear what these items do. – JoeDaddy7105 Jul 13 '15 at 06:32
  • I am also getting a NullPointerException when I try to run my modified code with your samples implemented at the following lines: setNewRootFragment(StandardAppBarFragment.newInstance()); in onCreate of the DrawerActivity.java class – JoeDaddy7105 Jul 13 '15 at 06:33
  • Can you plz show me your modified code so, it is easier for me to help you. And if you don't like butterKnife, remove it and every View which get injected you have to find theme by id, on the onCreate Method. Take a look here https://gist.github.com/swissonid/9f8ada12c7caee6fb4e7 - I didn't test it. – swissonid Jul 13 '15 at 21:22