mh9
mh9

Reputation: 45

navigation drawer main activity content

hi i have navigation drawer in my android application in the main activity java class and this is the code

    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);

         //add this line to display menu1 when the activity is loaded
         displaySelectedScreen(R.id.nav_menu1);
     }

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

     private void displaySelectedScreen(int itemId) {

         //creating fragment object
         Fragment fragment = null;
         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

         //initializing the fragment object which is selected
         switch (itemId) {
             case R.id.nav_menu1:
                 fragment = new gallary();
                 break;
             case R.id.nav_menu2:
                 fragment = new about();
                 break;
             case R.id.nav_menu3:
                 fragment = new contact();
                 break;
         }

         //replacing the fragment
         if (fragment != null) {
             FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
             ft.replace(R.id.content_frame, fragment);
             ft.commit();
         }

         drawer.closeDrawer(GravityCompat.START);
     }


     @SuppressWarnings("StatementWithEmptyBody")
     @Override
     public boolean onNavigationItemSelected(MenuItem item) {

         //calling the method displayselectedscreen and passing the id of selected menu
         displaySelectedScreen(item.getItemId());
         //make this method blank
         return true;
     }
 }

and this is the main activity xml code

<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: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>

so i want to add some content such an images and texts to main activity xml page of the app but when i add it in main content of navigation it looks good and everything okay but when i click on for example gallary fragment from the navigation drawer it took me to new page which has the content that i put in gallary xml but the problem is the content of main activity appear too with all fragment pages that i click from navigation drawer how can i make the content of each fragment appear without the content of main activity anyone understand me ? @_@

Upvotes: 1

Views: 4603

Answers (2)

Ayesh Qumhieh
Ayesh Qumhieh

Reputation: 1147

You fragments are overlapping each other. Just make sure you remove the previous fragment before adding a new fragment when selecting a navigation item from the drawer:

FragmentManager fm = getSupportFragmentManager();
fm.bginTransaction()
  .remove(oldFragment)
  .add(newFragment)
  .commit();

Upvotes: 0

Abhilash Maurya
Abhilash Maurya

Reputation: 308

I would suggest you to not to use any element other than navigationView and the fragment which will show up your screen for the navigationDrawer items, because you if clicked on some item you will be redirected to that page but later if you ever wanted to get back to that mainactivity page it becomes nearly impossible.Either you restart the activity or try some complex method to achieve that.

Instead you should use fragments. You can call a default fragment which will be loaded on opening the app in onResume() method. This fragment should also be available in navigation item for later references.You can do it like this:

    fragment = new YourFragment();
    transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.contentfragment,fragment).commitNow();

But if you still wanted to do so, you can set the elements of the mainactivity to visibility to gone. I don't know if this method will work.

This should be set on navigation item click

[your_element].setVisibility(View.GONE);

Upvotes: 2

Related Questions