0

I am using Navigation view with drawer layout and a custom layout in side Navigation view as menu, and included it inside navigation view but noting is visible, i have also tried to add other type of views but nothing is showing in navigationview. below is the main activity xml code where i am using the drawerlayout and navigation view and main activity class

Main Activity

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Drawer"
tools:context="com.example.minhasoft_pc.drawer.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="4"
    android:orientation="vertical">

    <ImageView
        android:layout_margin="10dp"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher_round"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:layout_margin="5dp"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher_round"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:layout_margin="10dp"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher_round"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />



</LinearLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:layout_marginEnd="-65dp"
    android:layout_marginRight="-65dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/drawer_menu"/>


    </LinearLayout>



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


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

Activity class

public class MainActivity extends AppCompatActivity {


private DrawerLayout mDrawerlayout ;
private ActionBarDrawerToggle mToggle;

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



   // ActionBar bar = getActionBar();
   // bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));



    mDrawerlayout = (DrawerLayout) findViewById(R.id.Drawer) ;
    mToggle = new ActionBarDrawerToggle(this,mDrawerlayout,R.string.draweropen,R.string.drawerclosed) ;



    mDrawerlayout.addDrawerListener(mToggle);
    mToggle.syncState();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(mToggle.onOptionsItemSelected(item))
    {
           return true ;
    }

    return super.onOptionsItemSelected(item);
}

}

Farrukh khalid
  • 93
  • 1
  • 2
  • 9
  • 1
    `NavigationView` is not meant to have arbitrary `View`s added to it. If you're not using the `NavigationView` for its specific functionalities, then remove it, and make the `LinearLayout` the drawer, by setting `android:layout_gravity="start"` on it, and giving it a definite `layout_width`, e.g., `240dp`. – Mike M. Feb 14 '18 at 08:04
  • remove the `LinearLayout` inside the `NavigationView`. Just `` the menu layout inside it. – Ishita Sinha Feb 14 '18 at 08:07
  • @IshitaSinha That's still adding arbitrary `View`s to the `NavigationView`, which is not how that's meant to be used. If the `NavigationView` is not actually being used for what it does, it should just be removed. – Mike M. Feb 14 '18 at 08:15
  • @ishita sinha tried it didnt worked ! – Farrukh khalid Feb 14 '18 at 08:17
  • @Mike M with reference to this https://stackoverflow.com/questions/30626324/navigationview-and-custom-layout. i wanted to add a custom menu and use navigation menu just like this .. and my requirement were also a full screen navigation view – Farrukh khalid Feb 14 '18 at 08:18
  • 1
    @MikeM. I agree, which is why I upvoted your comment. – Ishita Sinha Feb 14 '18 at 08:20
  • Yeah, don't do that. That's a terrible answer, and I can't believe it has that many upvotes. `NavigationView` is meant for a very specific design. It's not supposed to be a general-purpose container for other `View`s. You're not using it for what it does, so just remove it, and make the `LinearLayout` the drawer. A drawer does not have to be a `NavigationView`. It can be pretty much any `View`, like your `LinearLayout`, or even just the ``, depending on what's in that. – Mike M. Feb 14 '18 at 08:21
  • 1
    @Mike M. Thanks it worked ! but now a different issue arises the lay out that i added is not showing background colors that its supposed to show .. – Farrukh khalid Feb 14 '18 at 08:33
  • 1
    sorry man my bad ! just a silly mistake ! – Farrukh khalid Feb 14 '18 at 08:40
  • I'm not sure what you mean. You can set the `background` on the `LinearLayout`, if that's what you're talking about. I believe the default for `NavigationView` is `android:background="?android:attr/windowBackground"`, which you could add to the `LinearLayout` yourself. – Mike M. Feb 14 '18 at 08:42
  • Oh, got it? Cool. – Mike M. Feb 14 '18 at 08:42
  • answer the question so i can accept your answer ! – Farrukh khalid Feb 14 '18 at 08:55

1 Answers1

0

You have to include your Nav. Drawer menu items like this , Check the below xml code as references :

Here included as app:menu="@menu/nav_drawer_items"

You can take the reference of this question with solution : Align custom view to left of navigation drawer

 <?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">

    <android.support.design.widget.NavigationView
        android:id="@+id/nav1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_drawer_items"
        app:itemBackground="@color/app_bg_color"
        app:itemIconTint="@color/text_white"
        app:itemTextColor="@color/text_white"
        android:background="@color/bottom_navigation_color"
        android:fitsSystemWindows="true"
        android:theme="@style/NavigationView" />
</android.support.v4.widget.DrawerLayout>
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44