I'm trying to get the navigation drawer down,  i've followed a few 
tutorials around the internet however they all end up not working.  Now I 
know it's something simple but I can't figure it out.  Menu opens, main 
fragment loads, but if I click a new fragment, nothing works.  ideas?

activity_main.xml
<?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";
    xmlns:ads="http://schemas.android.com/apk/res-auto";
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />


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



MainFragment java

    package com.autotools.autotools;


    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;






    public class MainFragment extends Fragment {


    public MainFragment() {




    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {


        View rootView = inflater.inflate(R.layout.fragment_main, container, 
false);


        // Inflate the layout for this fragment
        return rootView;
    }




    }


Second Fragment

    
package com.autotools.autotools;


    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;




    public class autoLoanFragment extends Fragment {


    public autoLoanFragment(){


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) {


       return inflater.inflate(R.layout.fragment_autoloan,container,false);




    }


    }


MainActivity java


    package com.autotools.autotools;


    import android.support.v4.app.FragmentManager;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.view.View;
    import android.support.design.widget.NavigationView;
    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;
    import android.view.Menu;
    import android.view.MenuItem;


    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;


    public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


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


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout
);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.
string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();


        NavigationView navigationView = (NavigationView) findViewById(R.id.
nav_view);
        navigationView.setNavigationItemSelectedListener(this);


        MainFragment fragment =  new MainFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = 
getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, fragment);
        fragmentTransaction.commit();
    }


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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is 
present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();


        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }


        return super.onOptionsItemSelected(item);
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.


        int id = item.getItemId();


        if (id == R.id.nav_home) {
            MainFragment fragment =  new MainFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = 
  getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.content_frame, fragment);
            fragmentTransaction.commit();
        } else if (id == R.id.nav_autoLoan) {
            autoLoanFragment fragment =  new autoLoanFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = 
getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.content_frame, fragment);
            fragmentTransaction.commit();
        } else if (id == R.id.nav_gas) {


        } else if (id == R.id.nav_settings) {


        } else if (id == R.id.nav_share) {


        } else if (id == R.id.nav_send) {


        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout
);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    }


Fragment_main xml

   
 <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android";
    android:layout_width="match_parent"
    android:layout_height="match_parent">




    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView2"
        android:layout_gravity="center_horizontal|top" />
    </FrameLayout>


activity_main_drawer xml

  
  <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android";>


    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_info_black_48dp"
            android:title="Home" />
        <item
            android:id="@+id/nav_autoLoan"
            android:icon="@drawable/ic_attach_money_black_48dp"
            android:title="Auto Loan Calculator" />
        <item
            android:id="@+id/nav_gas"
            android:icon="@drawable/ic_local_gas_station_black_48dp"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_settings"
            android:icon="@drawable/ic_settings_black_48dp"
            android:title="Settings" />
    </group>


    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>


    </menu>


-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/3bfe5656-8cf2-457a-acd8-438275c9b6f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to