Search code examples
javaandroidandroid-drawer

I want to add other content with navigation drawer


I am new to android. I have created a drawer layout using list view (custom list view using base adapter). this is my code activity_main.xml. When i run the application i see an empty screen. Listview is working properly. But i just need to add another content in the screen, like mapview or anything else. And i want that content to move when the navigation menu is opened. How to add content other than list view in the below code?

  <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/drawerLayout"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

<FrameLayout 
    android:id="@+id/mainContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 </FrameLayout>

<ListView 
    android:id="@+id/drawerList"
    android:background="#CECECE"
    android:divider="@null"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"></ListView>


Solution

  • FrameLayout is the one that holds your main content. Create a fragment and its layout, then on your MainActivity you add the fragment to the FrameLayout like this.

     MyFragment fragment = new MyFragment();
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
     ft.add(R.id.mainContent, fragment, "fragment").commit()`;