2

I am working on Navigation Drawer Layout. Another layout must load in place of the drawer_layout onClick of the navigation items.

 if (id == R.id.nav_camera) {

  //code to inflate another layout inplace of current layout
  //setContentView(R.layout.another_layout1);


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

  //code to inflate another layout inplace of current layout
  //setContentView(R.layout.another_layout2);

    } 

How to load another layout in the mainlayout.?

Melwin
  • 47
  • 6
  • 2
    Possible duplicate of [Android studio navigation drawer with fragment or activities](https://stackoverflow.com/questions/36079028/android-studio-navigation-drawer-with-fragment-or-activities) – AskNilesh Apr 02 '18 at 05:33
  • https://stackoverflow.com/questions/3334048/android-layout-replacing-a-view-with-another-view-on-run-time – Ankita Apr 02 '18 at 05:34
  • 2
    use **`Fragments`** – AskNilesh Apr 02 '18 at 05:34
  • if you want to load different layout then use baseactivty and load each layout their in its oncreate method.! – Atif AbbAsi Apr 02 '18 at 05:53

1 Answers1

1

You have to create fragment (Right click on your project -> New -> Fragment).then load inside Activity below this..

 if (id == R.id.nav_camera) {

     CameraFragment camera = new CameraFragment();
     FragmentManager fragmentManager = getFragmentManager();
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();              
     fragmentTransaction.add(R.id.fragment_container, camera, "HELLO");
     fragmentTransaction.commit();


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

     GalleryFragment gallery = new GalleryFragment();
     FragmentManager fragmentManager = getFragmentManager();
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();              
     fragmentTransaction.add(R.id.fragment_container, gallery, "HELLO");
     fragmentTransaction.commit();

    } 
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54