1. Are you ready for the Galaxy S20? Here is everything we know so far!

Navigation Drawer not changing fragment.

Discussion in 'Android Development' started by RivaCom, Feb 11, 2016.

  1. RivaCom

    RivaCom Lurker
    Thread Starter

    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
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:app="http://schemas.android.com/apk/res-auto"
    5.     xmlns:tools="http://schemas.android.com/tools"
    6.     xmlns:ads="http://schemas.android.com/apk/res-auto"
    7.     android:id="@+id/drawer_layout"
    8.     android:layout_width="match_parent"
    9.     android:layout_height="match_parent"
    10.     android:fitsSystemWindows="true"
    11.     tools:openDrawer="start">
    12.  
    13.     <include
    14.         layout="@layout/app_bar_main"
    15.         android:layout_width="match_parent"
    16.         android:layout_height="match_parent" />
    17.  
    18.     <android.support.design.widget.NavigationView
    19.         android:id="@+id/nav_view"
    20.         android:layout_width="wrap_content"
    21.         android:layout_height="match_parent"
    22.         android:layout_gravity="start"
    23.         android:fitsSystemWindows="true"
    24.         app:headerLayout="@layout/nav_header_main"
    25.         app:menu="@menu/activity_main_drawer" />
    26.  
    27. </android.support.v4.widget.DrawerLayout>
    28.  
    MainFragment java
    Code (Text):
    1.  
    2.     package com.autotools.autotools;
    3.  
    4.     import android.app.Activity;
    5.     import android.os.Bundle;
    6.     import android.support.v4.app.Fragment;
    7.     import android.view.LayoutInflater;
    8.     import android.view.View;
    9.     import android.view.ViewGroup;
    10.  
    11.  
    12.  
    13.     public class MainFragment extends Fragment {
    14.  
    15.     public MainFragment() {
    16.  
    17.  
    18.     }
    19.  
    20.     @Override
    21.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    22.  
    23.         View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    24.  
    25.         // Inflate the layout for this fragment
    26.         return rootView;
    27.     }
    28.  
    29.  
    30.     }
    31.  
    Second Fragment
    Code (Text):
    1.  
    2.     package com.autotools.autotools;
    3.  
    4.     import android.os.Bundle;
    5.     import android.support.v4.app.Fragment;
    6.     import android.view.LayoutInflater;
    7.     import android.view.View;
    8.     import android.view.ViewGroup;
    9.  
    10.  
    11.     public class autoLoanFragment extends Fragment {
    12.  
    13.     public autoLoanFragment(){
    14.  
    15.     }
    16.  
    17.     @Override
    18.     public View onCreateView(LayoutInflater inflater, ViewGroup container,      Bundle savedInstanceState) {
    19.  
    20.        return inflater.inflate(R.layout.fragment_autoloan,container,false);
    21.  
    22.  
    23.     }
    24.  
    25.     }
    26.  
    MainActivity java
    Code (Text):
    1.  
    2.     package com.autotools.autotools;
    3.  
    4.     import android.support.v4.app.FragmentManager;
    5.     import android.os.Bundle;
    6.     import android.support.design.widget.FloatingActionButton;
    7.     import android.support.design.widget.Snackbar;
    8.     import android.view.View;
    9.     import android.support.design.widget.NavigationView;
    10.     import android.support.v4.view.GravityCompat;
    11.     import android.support.v4.widget.DrawerLayout;
    12.     import android.support.v7.app.ActionBarDrawerToggle;
    13.     import android.support.v7.app.AppCompatActivity;
    14.     import android.support.v7.widget.Toolbar;
    15.     import android.view.Menu;
    16.     import android.view.MenuItem;
    17.  
    18.     import com.google.android.gms.ads.AdRequest;
    19.     import com.google.android.gms.ads.AdView;
    20.  
    21.     public class MainActivity extends AppCompatActivity
    22.         implements NavigationView.OnNavigationItemSelectedListener {
    23.  
    24.     @Override
    25.     protected void onCreate(Bundle savedInstanceState) {
    26.         super.onCreate(savedInstanceState);
    27.         setContentView(R.layout.activity_main);
    28.  
    29.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    30.         setSupportActionBar(toolbar);
    31.  
    32.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    33.         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
    34.                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    35.         drawer.setDrawerListener(toggle);
    36.         toggle.syncState();
    37.  
    38.         NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    39.         navigationView.setNavigationItemSelectedListener(this);
    40.  
    41.         MainFragment fragment =  new MainFragment();
    42.         android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    43.         fragmentTransaction.replace(R.id.content_frame, fragment);
    44.         fragmentTransaction.commit();
    45.     }
    46.  
    47.     @Override
    48.     public void onBackPressed() {
    49.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    50.         if (drawer.isDrawerOpen(GravityCompat.START)) {
    51.             drawer.closeDrawer(GravityCompat.START);
    52.         } else {
    53.             super.onBackPressed();
    54.         }
    55.     }
    56.  
    57.     @Override
    58.     public boolean onCreateOptionsMenu(Menu menu) {
    59.         // Inflate the menu; this adds items to the action bar if it is present.
    60.         getMenuInflater().inflate(R.menu.main, menu);
    61.         return true;
    62.     }
    63.  
    64.     @Override
    65.     public boolean onOptionsItemSelected(MenuItem item) {
    66.         // Handle action bar item clicks here. The action bar will
    67.         // automatically handle clicks on the Home/Up button, so long
    68.         // as you specify a parent activity in AndroidManifest.xml.
    69.         int id = item.getItemId();
    70.  
    71.         //noinspection SimplifiableIfStatement
    72.         if (id == R.id.action_settings) {
    73.             return true;
    74.         }
    75.  
    76.         return super.onOptionsItemSelected(item);
    77.     }
    78.  
    79.     @SuppressWarnings("StatementWithEmptyBody")
    80.     @Override
    81.     public boolean onNavigationItemSelected(MenuItem item) {
    82.         // Handle navigation view item clicks here.
    83.  
    84.         int id = item.getItemId();
    85.  
    86.         if (id == R.id.nav_home) {
    87.             MainFragment fragment =  new MainFragment();
    88.             android.support.v4.app.FragmentTransaction fragmentTransaction =   getSupportFragmentManager().beginTransaction();
    89.             fragmentTransaction.replace(R.id.content_frame, fragment);
    90.             fragmentTransaction.commit();
    91.         } else if (id == R.id.nav_autoLoan) {
    92.             autoLoanFragment fragment =  new autoLoanFragment();
    93.             android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    94.             fragmentTransaction.replace(R.id.content_frame, fragment);
    95.             fragmentTransaction.commit();
    96.         } else if (id == R.id.nav_gas) {
    97.  
    98.         } else if (id == R.id.nav_settings) {
    99.  
    100.         } else if (id == R.id.nav_share) {
    101.  
    102.         } else if (id == R.id.nav_send) {
    103.  
    104.         }
    105.  
    106.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    107.         drawer.closeDrawer(GravityCompat.START);
    108.         return true;
    109.     }
    110.     }
    111.  
    Fragment_main xml
    Code (Text):
    1.  
    2.     <?xml version="1.0" encoding="utf-8"?>
    3.     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:layout_width="match_parent"
    5.     android:layout_height="match_parent">
    6.  
    7.  
    8.     <TextView
    9.         android:layout_width="wrap_content"
    10.         android:layout_height="wrap_content"
    11.         android:textAppearance="?android:attr/textAppearanceLarge"
    12.         android:text="Large Text"
    13.         android:id="@+id/textView2"
    14.         android:layout_gravity="center_horizontal|top" />
    15.     </FrameLayout>
    16.  
    activity_main_drawer xml
    Code (Text):
    1.  
    2.     <?xml version="1.0" encoding="utf-8"?>
    3.     <menu xmlns:android="http://schemas.android.com/apk/res/android">
    4.  
    5.     <group android:checkableBehavior="single">
    6.         <item
    7.             android:id="@+id/nav_home"
    8.             android:icon="@drawable/ic_info_black_48dp"
    9.             android:title="Home" />
    10.         <item
    11.             android:id="@+id/nav_autoLoan"
    12.             android:icon="@drawable/ic_attach_money_black_48dp"
    13.             android:title="Auto Loan Calculator" />
    14.         <item
    15.             android:id="@+id/nav_gas"
    16.             android:icon="@drawable/ic_local_gas_station_black_48dp"
    17.             android:title="Slideshow" />
    18.         <item
    19.             android:id="@+id/nav_settings"
    20.             android:icon="@drawable/ic_settings_black_48dp"
    21.             android:title="Settings" />
    22.     </group>
    23.  
    24.     <item android:title="Communicate">
    25.         <menu>
    26.             <item
    27.                 android:id="@+id/nav_share"
    28.                 android:icon="@drawable/ic_menu_share"
    29.                 android:title="Share" />
    30.             <item
    31.                 android:id="@+id/nav_send"
    32.                 android:icon="@drawable/ic_menu_send"
    33.                 android:title="Send" />
    34.         </menu>
    35.     </item>
    36.  
    37.     </menu>
    38.  
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    You're missing an element in activity_main.xml
    This piece of code places the fragment

    Code (Text):
    1.  
    2. fragmentTransaction.replace(R.id.content_frame, fragment);
    3.  
    but it doesn't get added because R.id.content_frame doesn't exist. Add this element to activity_main.xml

    Code (Text):
    1.  
    2. <FrameLayout
    3.     android:orientation="vertical"
    4.     android:layout_width="match_parent"
    5.     android:layout_height="match_parent"
    6.     android:id="@+id/content_frame">
    7. </FrameLayout>
    8.  
     
  3. RivaCom

    RivaCom Lurker
    Thread Starter

    Sorry, my content_main has content_frame , i tried changing it to activity main and still no success.

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:app="http://schemas.android.com/apk/res-auto"
    4.     xmlns:tools="http://schemas.android.com/tools"
    5.     android:layout_width="match_parent"
    6.     android:layout_height="match_parent"
    7.     android:paddingBottom="@dimen/activity_vertical_margin"
    8.     android:paddingLeft="@dimen/activity_horizontal_margin"
    9.     android:paddingRight="@dimen/activity_horizontal_margin"
    10.     android:paddingTop="@dimen/activity_vertical_margin"
    11.     app:layout_behavior="@string/appbar_scrolling_view_behavior"
    12.     tools:context="com.autotools.autotools.MainActivity"
    13.     tools:showIn="@layout/app_bar_main">
    14.  
    15.    <FrameLayout
    16.        android:id="@+id/content_frame"
    17.        android:layout_width="match_parent"
    18.        android:layout_height="match_parent">
    19.  
    20.    </FrameLayout>
    21. </RelativeLayout>
    22.  
     
  4. Deleted User

    Deleted User Guest

    Are there any errors in your Logcat?
     
  5. RivaCom

    RivaCom Lurker
    Thread Starter

    No errors in logcat
     
  6. Deleted User

    Deleted User Guest

    Set a breakpoint at the line where the fragment is replaced. Then step through the code. What happens?
     
  7. RivaCom

    RivaCom Lurker
    Thread Starter

    So it looks like it's not getting to any of that function, i put break points in multiple spots with no luck. I guess why is it skipping it?
     
  8. itterN

    itterN Lurker

    Probably caused by part of your project you did not provide above. A reasonable extrapolation based on what you provided appears to work as intended - MainFragment is loaded initially, clicking on "Auto Loan Calculator" loads
    autoLoanFragment, and clicking on "Home" loads MainFragment; naturally the other menu items do nothing (not implemented). I've attached the sample project's source tree.

    Tested on android version 6.0.1.
     

    Attached Files:

Loading...

Share This Page

Loading...