0

enter image description here enter image description here

As shown in the images, I am trying to use Navigation drawer and tab-bar in a same Activity. It compiles without any error, but the content for the tab fragment is not displayed at all.

This is how my MainActivity is coded:

public class UserMainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;

    private SectionsPageAdapter pageAdapter;

    ViewPager viewPager;


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

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container, new MatchListTab());



        mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_layout);

        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        pageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        viewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(viewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager((viewPager));

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(mToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    // Adds fragments to SectionsPageAdapter and gives names for the corresponding tab

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new MatchListTab(), "Search Users");
        adapter.addFragment(new MatchListTab(), "Search dddd");
        viewPager.setAdapter(adapter);
    }
}

And here is how my activity_main.xml is coded:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
    tools:context="ykim164cs242.tournamentor.UserMainActivity"
    android:id="@+id/nav_layout">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="71dp"
            app:tabIndicatorColor="@android:color/holo_green_light" />

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

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#5555"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/navigation_menu">

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

</android.support.v4.widget.DrawerLayout>

When I used the exact same MainActivity.java code without the navigation-drawer functionality, it worked fine and all tab-fragment's contents were displayed in the viewPager which has an id of container in the xml.

What error am I making?

EDIT

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
    tools:context="ykim164cs242.tournamentor.UserMainActivity"
    android:id="@+id/nav_layout">

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#5555"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/navigation_menu">

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

</android.support.v4.widget.DrawerLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="71dp">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="71dp"
        app:tabIndicatorColor="@android:color/holo_orange_dark" />

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

</LinearLayout>
Dawn17
  • 7,825
  • 16
  • 57
  • 118
  • You want to have only one main content `View` inside a `DrawerLayout`. Put both the `ViewPager` and `AppBarLayout` inside another `ViewGroup`; e.g., a vertical `LinearLayout`. – Mike M. Nov 12 '17 at 02:43
  • @MikeM. I added a new edit. I created a new separate LinearLayout and put my ViewPager and AppBarLayout in to it. That gave me an error "Error:(22) Error parsing XML: junk after document element". What did I do wrong? – Dawn17 Nov 12 '17 at 02:53
  • 1
    You want to keep that main content in the same spot; i.e., inside the `` tags, above the ``. Also, you probably want the `` listed first inside the ``. – Mike M. Nov 12 '17 at 02:56
  • Thank you it's working! Only thing that I want to ask more is, if I use a vertical linear layout, how can I adjust the position of the tab-bar to the bottom instead of the top? – Dawn17 Nov 12 '17 at 03:01
  • Oh, sorry, I assumed you wanted those at the top. In that case, put `AppBarLayout` back to being last, and set the `ViewPager`'s `layout_height` to `0dp`, and its `layout_weight` to `1`. – Mike M. Nov 12 '17 at 03:07
  • If I set ViewPager's height to 0, would it still display the contents? – Dawn17 Nov 12 '17 at 03:10
  • Yeah, the `layout_weight`, in this case, tells the `LinearLayout` that the `ViewPager` wants all the vertical space remaining after the other child is measured out. – Mike M. Nov 12 '17 at 03:11
  • 1
    Great! It saved my day. I hope I was able to give you an acceptance. Thanks! – Dawn17 Nov 12 '17 at 03:14
  • There are examples of this setup already on-site here, so I'll just mark this as a duplicate of one. Thanks! Glad you got it working. Cheers! – Mike M. Nov 12 '17 at 03:24

0 Answers0