1

What I have been Trying to Do

What I have been aiming to do for several days is, using an Activity that implements NavigationView.OnNavigationItemSelectedListener. I want this to create two Tabs inside a android.support.v4.app.Fragment in the FrameLayout provided by the template for the Navigation Drawer on Android Studio 1.5.1.
- My Current Activity uses a toolbar not an ActionBar.
- I was hoping to use a ViewPager; hence needing to use Support Fragments.
- I have followed several code examples online and have looked at Android Developers Reference.

Other Linked Posts:

Unable to add Tabs inside Fragment of Navigation Drawer Android | This uses Fragments and the ActionBar

Android TabHost inside Fragment | Chosen Answer -> using FragmentActivity | Unfortunatly uses a new FragmentActivity but I need to keep my NavigationDrawer so am using one activity

Android TabHost only inside one Fragment (NavigationDrawer) | This didnt really provide me much help as I am very new to using tabs

Android Adding Tab inside Fragment | Chosen Answer -> using FragmentTabHost | I have taken FragmentTabHost XML and Java code from this example but It has not worked

Tabs in a class that extends fragment | The andswer refers to googles documentation which is not working for me.

Adding Tab inside Fragment In Android? | Uses android.app.Fragment and the suggested answer didnt work.

Using TabLayout inside a Fragment; tab text invisible | I am using Design Library v23.1.1 | Bugs reported in v22.2.1. The code here gave me no tabs either


Any Suggestion would be appreciated.

MY CODE

NavActivity Class

import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;


public class NavigationMenuActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    Toolbar toolbar;
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle toggle;
    NavigationView navigationView;
    FragmentManager fragmentManager;

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

    //Setting up the Toolbar
    toolbar = (Toolbar) findViewById(R.id.navmenu_appbar_tbar);
    setSupportActionBar(toolbar);

    //Set up the action bar toggle
    drawerLayout = (DrawerLayout) findViewById(R.id.navmenu_act_drawerlay);
    toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.setDrawerListener(toggle);
    toggle.syncState();

    //Set up the navigation View
    navigationView = (NavigationView) findViewById(R.id.navmenu_nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //SET THE FRAGMENT
    fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.navmenu_appbar_fl);

    if(fragment==null) { //If no fragment exists relate the fragment
        fragment = new TabLayoutFragmentExample();
        fragmentManager.beginTransaction()
                .add(R.id.navmenu_appbar_fl, fragment)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .commit();
    }
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.navmenu_act_drawerlay);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}



@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    if (id == R.id.nav_profile) {
        fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.navmenu_appbar_fl);
        if(fragment!=null) { //If a fragment exists replace the fragment
            fragment = new TestFragmentNoTabs();
            fragmentManager.beginTransaction()
                    .replace(R.id.navmenu_appbar_fl, fragment)
                    .commit();
        }
    } else if (id == R.id.nav_near_me) {
        // Handle the near me action here

    } else if (id == R.id.nav_proximity) {
        // Handle the proximity action
        fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.navmenu_appbar_fl);
        if(fragment!=null) {//If A fragment exists replace the fragment
            fragment = new TestFragmentNoTabsTWO();
            fragmentManager.beginTransaction()
                    .replace(R.id.navmenu_appbar_fl, fragment)
                    .commit();
        }
    }
    //Close the drawerLayout after it is clicked?
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.navmenu_act_drawerlay);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    //sync the toggle
    toggle.syncState();
}
}

navmenu_appbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".NavigationMenuActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/navmenu_appbar_tbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/JethrosTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/navmenu_appbar_fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</android.support.design.widget.CoordinatorLayout>

TablayoutFragmentExample Class

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;


public class TabLayoutFragmentExample extends Fragment {


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

    TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tab_layout_frag_tl);
    tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.tab_layout_frag_vp);

    viewPager.setAdapter(new PagerAdapter //ChildFragManager; Tnx @Luksprog
            (getChildFragmentManager(), tabLayout.getTabCount())); 

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setupWithViewPager(viewPager); //Set Viewpager; Tnx @Luksprog

    return inflatedView;
}

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                TestFragmentNoTabs tab1 = new TestFragmentNoTabs();
                return tab1;
            case 1:
                TestFragmentNoTabsTWO tab2 = new TestFragmentNoTabsTWO();
                return tab2;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}
}

tab_layout_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout_frag_tl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/tab_layout_frag_vp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"/>

</android.support.design.widget.AppBarLayout>

TestFragmentNoTabs & TestFragmentNoTabsTWO (Fragment 1 and fragment 2)

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

public class TestFragmentNoTabs extends Fragment {
    public final String TAG = "TestFragmentNoTabs";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //Inflate the fragment view
        View view = inflater.inflate(R.layout.testfragnotabs, container, false);
        return view; //Return the view to the activity
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}

testfragnotabs.xml (similar to testfragnotabstwo.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/nearme_frag_rlay"
    android:clickable="false">

    <TextView
        android:id="@+id/nearme_frag_tv_seclab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" TestFragmentNoTabsOne -> testfragnotabs.xml" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView1"
        android:layout_above="@+id/nearme_frag_tv_seclab" />

</RelativeLayout>

Current Situation with Above Code

The project builds fine and the navigation drawer is working fine as well. However the frame layout where the fragment and the TabLayout should appear is empty with a white background (Just like the Design preview in tab_layout_frag.xml; this could mean there is a problem with the ViewPager or the TabLayout?).

Community
  • 1
  • 1
Jethro
  • 928
  • 1
  • 10
  • 20
  • You didn't explain very well what is happening now or where did you get stuck. – user Feb 16 '16 at 19:55
  • 1
    Instead of getFragmentManager() use getChildFragmentManager() in TabLayoutFragmentExample because you have nested fragments. Also, instead of manually setting those listeners you could use the setupWithViewPager() of the TabLayout instead. – user Feb 16 '16 at 22:04
  • @Luksprog Thank you for the suggestions. Much better implementing them with the one line of code. I thought i had used Child manager but no i hadnt :D. It doesn't fix it but is a step in the right direction. However, the output is unchanged. – Jethro Feb 17 '16 at 14:28
  • EDIT: I have worked out that the frame layout is being partially hidden behind the Appbar at the top. Now need to work out how to get it to start from the end of the Appbar and fill down – Jethro Feb 18 '16 at 16:11
  • Follow this [link](https://stackoverflow.com/a/52374982/1675469). This link might help you. – Zeeshan Sep 17 '18 at 20:09

0 Answers0