0

I am making navigation Drawer, i have added all the files and added libraries for the same..i have copied this navigation drawer from example..and it was working fine..but when i tried that in my project ..its not showing up. But its showing the FragmentOne which should be open when first item click(it means on 0 position.)

here is my actvitity where i want to show navigation drawer

PMCompaniesActivity:

public class PMCompaniesActivity extends AppCompatActivity
          implements NavigationView.OnNavigationItemSelectedListener {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_pmcompanies);
            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(0);
        }


        @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.menu_main
                    , menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_settings:
                    // User chose the "Settings" item, show the app settings UI...


               // case R.id.action_cart:


                    //Intent ibs = new Intent(MainActivity.this,AddToCart.class);
                    //ibs.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                   // startActivity(ibs);
                   // finish();
                    break;
                // User chose the "Favorite" action, mark the current item
                // as a favorite...


                default:
                    // If we got here, the user's action was not recognized.
                    // Invoke the superclass to handle it.
                    return super.onOptionsItemSelected(item);

            }
            return true;
        }
        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();

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

                displaySelectedScreen(0);
            }
            else if (id == R.id.pgnsrch)
            {
                displaySelectedScreen(1);
            }
            else if (id == R.id.Fanciers1)
            {
                displaySelectedScreen(2);
            } else if (id == R.id.company1)

            {
                displaySelectedScreen(3);
            }
            else if (id == R.id.Article1)
            {
                displaySelectedScreen(4);
            } else if (id == R.id.Settings1)
            {
                displaySelectedScreen(5);
            }
            return true;
        }

    private void displaySelectedScreen(int position)
    {
        try {

            Fragment fragment = null;

            if (position == 0)
            {
                fragment = new FragmentOne();
            }
            else if (position == 1)
            {

            }
            else if (position == 2)
            {

            } else if (position == 3) {

            } else if (position == 4) {

            } else if (position == 5) {

            }

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

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

here is activity_pmcompanies.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"
    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>

here is my app_bar_main:

   <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:fitsSystemWindows="true"
    tools:context="com.pigeonmarket.PMSettingsActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include
        layout="@layout/content_main2"
        />
</android.support.design.widget.CoordinatorLayout>

and here is contain_main2.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:layout_marginTop="?actionBarSize"
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>
</RelativeLayout>

Its not showing me any error on logcat..

  • You have two `DrawerLayout`s, both with the same ID, one nested inside the other. Get rid of the `DrawerLayout` in `app_bar_main`. I would also point out that you don't have a `ViewGroup` with ID `content_frame` in either of the posted layouts, so I'm not quite sure how your `FragmentTransaction` is working. – Mike M. Jan 18 '17 at 05:09
  • sorry i am not getting you...in app bar which id u r talking abt?i will post other files also where u can see content _frame –  Jan 18 '17 at 05:13
  • Get rid of the ``, and its corresponding `` in the `app_bar_main` layout. You only need one `DrawerLayout` setup, and you've got it in `activity_pmcompanies`. – Mike M. Jan 18 '17 at 05:15
  • when i am deleting another drawer layout..it shows error in xml.....java.lang.IllegalArgumentException: No drawer view found with gravity LEFT at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1651) –  Jan 18 '17 at 05:17
  • Rendering Problems Unable to open navigation drawer –  Jan 18 '17 at 05:18
  • i have edited my app bar and included other file also..plz see it –  Jan 18 '17 at 05:31
  • OK, that looks acceptable, though the `contain_main2` layout include is a little superfluous. What's the problem now? – Mike M. Jan 18 '17 at 05:36
  • i dont know..it shows in xml design preview but when i tried run it.its nt showing only –  Jan 18 '17 at 05:54
  • If you mean the drawer isn't open right when the app runs, you actually have to open it yourself; either by dragging it, or by using the `ActionBarDrawerToggle` that'll be on the `Toolbar`. The `tools:openDrawer="start"` is what causes it to be open in the layout designer, but that doesn't affect the runtime behavior. If you want it to be open automatically from the start, you can add a `drawer.openDrawer(GravityCompat.START);` call in `onCreate()`. – Mike M. Jan 18 '17 at 05:58
  • but its not showing only ..i mean no navigation drawer ... –  Jan 18 '17 at 05:59
  • thr is not even icon of navigation –  Jan 18 '17 at 06:00
  • You sure you're looking at the right `Activity`? Which `` has the `LAUNCHER` `` in your manifest? – Mike M. Jan 18 '17 at 06:02
  • thr is one splash activity then this activity whr navigation is thr –  Jan 18 '17 at 06:05
  • launcher is splash activity –  Jan 18 '17 at 06:05
  • wht u think ..now i have showed u my files also ...is thr any still missing?? –  Jan 18 '17 at 06:14
  • And you're absolutely certain `PMCompaniesActivity` opens after the splash? Can you see the other layout elements, like the `Toolbar`? What happens if you add the `openDrawer()` call I mentioned above? Did you clean/rebuild the project after making all those changes? – Mike M. Jan 18 '17 at 06:14
  • i did add above drawer.openDrawer(GravityCompat.START);but i didnt clean d project..ok i will clean it rebuild again –  Jan 18 '17 at 06:19
  • no sorry..PmCompanies not open after splash..first it will ask username and password(Login activity) after successful login ..it will open PMcompanies –  Jan 18 '17 at 06:32
  • can u tell wht is wrong i am doing??? –  Jan 18 '17 at 06:32
  • Nope. Everything you have posted is good, AFAICT. If you added the `openDrawer()` call, & you still can't see the drawer, or the toggle, then I really don't think you're looking at what you think you are. Unless you're doing some incredibly odd, bad things to the `Activity`'s layout from `FragmentOne` when it loads, then you should be able to at least drag the drawer open, if not see it at startup. You'll have to do some investigating, change things, narrow down what's happening. First thing I would do is make `PMCompaniesActivity` the `LAUNCHER` temporarily, so you know it's actually running. – Mike M. Jan 18 '17 at 06:49

0 Answers0