ListView inside Navigation Drawer in Android Android by Rajesh Kumar Sahanee - July 18, 2019July 18, 20190 Post Views: 7,652 Hello Friends, Today I am going to share a code to place ListView inside Navigation Drawer in Android. A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. To know more about list view click here. Placing ListView inside Navigation Drawer concept can be used to generate dynamic menu, i.e fetching menu items from database using web service, etc. ListView item comes from Adapter which can be of two types:- 1. ArrayAdapter – to show single item 2. BaseAdapter – to show custom list item just as in video below So, Here is code MainActivity.java MainActivity.java Java package com.zatackcoder.listviewinsidenavigationdrawer; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button button1; Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = findViewById(R.id.button1); button2 = findViewById(R.id.button2); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), NavigationDrawer.class); intent.putExtra("list-type", "arrayadapter"); startActivity(intent); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), NavigationDrawer.class); intent.putExtra("list-type", "baseadapter"); startActivity(intent); } }); } } 123456789101112131415161718192021222324252627282930313233343536373839 package com.zatackcoder.listviewinsidenavigationdrawer; import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button; public class MainActivity extends AppCompatActivity { Button button1; Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = findViewById(R.id.button1); button2 = findViewById(R.id.button2); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), NavigationDrawer.class); intent.putExtra("list-type", "arrayadapter"); startActivity(intent); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), NavigationDrawer.class); intent.putExtra("list-type", "baseadapter"); startActivity(intent); } }); }} activity_main.xml activity_main.xml XHTML <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ListView with ArrayAdapter"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ListView with BaseAdapter"/> </LinearLayout> 1234567891011121314151617181920212223 <?xml version="1.0" encoding="utf-8"?><LinearLayout 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" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ListView with ArrayAdapter"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ListView with BaseAdapter"/> </LinearLayout> Item.java Item.java Java package com.zatackcoder.listviewinsidenavigationdrawer; public class Item { private String name; private int image; public Item(String name, int image) { this.name = name; this.image = image; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getImage() { return image; } public void setImage(int image) { this.image = image; } } 123456789101112131415161718192021222324252627 package com.zatackcoder.listviewinsidenavigationdrawer; public class Item { private String name; private int image; public Item(String name, int image) { this.name = name; this.image = image; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getImage() { return image; } public void setImage(int image) { this.image = image; }} NavigationDrawer.java NavigationDrawer.java Java package com.zatackcoder.listviewinsidenavigationdrawer; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class NavigationDrawer extends AppCompatActivity { ListView navLV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_navigation_drawer); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); DrawerLayout drawer = findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); Intent i = getIntent(); navLV = findViewById(R.id.navLV); //ArrayAdapter ListView if(i.getStringExtra("list-type").equals("arrayadapter")) { final String items[] = {"Item 1", "Item 2", "Item 3", "Item 3", "Item 4", "Item 5"}; ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_view_item_type1, R.id.textView, items); navLV.setAdapter(adapter); navLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), items[position] + " Clicked", Toast.LENGTH_SHORT).show(); } }); } //BaseAdapter ListView else if(i.getStringExtra("list-type").equals("baseadapter")) { final List<Item> items = new ArrayList<>(); items.add(new Item("Camera", R.drawable.ic_menu_camera)); items.add(new Item("Gallery", R.drawable.ic_menu_gallery)); items.add(new Item("Manage", R.drawable.ic_menu_manage)); items.add(new Item("Share", R.drawable.ic_menu_share)); items.add(new Item("Send", R.drawable.ic_menu_send)); CustomAdapter adapter = new CustomAdapter(this, items); navLV.setAdapter(adapter); navLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), items.get(position).getName() + " Clicked", Toast.LENGTH_SHORT).show(); } }); } } @Override public void onBackPressed() { DrawerLayout drawer = 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.navigation_drawer, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } class CustomAdapter extends BaseAdapter { Context context; List<Item> items; public CustomAdapter(Context context, List<Item> items) { this.context = context; this.items = items; } @Override public int getCount() { return items.size(); } @Override public Object getItem(int i) { return items.get(i); } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View v = LayoutInflater.from(context).inflate(R.layout.list_view_item_type2, null); ImageView icon = v.findViewById(R.id.icon); TextView text = v.findViewById(R.id.text); icon.setImageResource(items.get(i).getImage()); text.setText(items.get(i).getName()); //event handling code can be done here return v; } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 package com.zatackcoder.listviewinsidenavigationdrawer; import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v4.view.GravityCompat;import android.support.v4.widget.DrawerLayout;import android.support.v7.app.ActionBarDrawerToggle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast; import java.util.ArrayList;import java.util.List; public class NavigationDrawer extends AppCompatActivity { ListView navLV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_navigation_drawer); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); DrawerLayout drawer = findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); Intent i = getIntent(); navLV = findViewById(R.id.navLV); //ArrayAdapter ListView if(i.getStringExtra("list-type").equals("arrayadapter")) { final String items[] = {"Item 1", "Item 2", "Item 3", "Item 3", "Item 4", "Item 5"}; ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_view_item_type1, R.id.textView, items); navLV.setAdapter(adapter); navLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), items[position] + " Clicked", Toast.LENGTH_SHORT).show(); } }); } //BaseAdapter ListView else if(i.getStringExtra("list-type").equals("baseadapter")) { final List<Item> items = new ArrayList<>(); items.add(new Item("Camera", R.drawable.ic_menu_camera)); items.add(new Item("Gallery", R.drawable.ic_menu_gallery)); items.add(new Item("Manage", R.drawable.ic_menu_manage)); items.add(new Item("Share", R.drawable.ic_menu_share)); items.add(new Item("Send", R.drawable.ic_menu_send)); CustomAdapter adapter = new CustomAdapter(this, items); navLV.setAdapter(adapter); navLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), items.get(position).getName() + " Clicked", Toast.LENGTH_SHORT).show(); } }); } } @Override public void onBackPressed() { DrawerLayout drawer = 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.navigation_drawer, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } class CustomAdapter extends BaseAdapter { Context context; List<Item> items; public CustomAdapter(Context context, List<Item> items) { this.context = context; this.items = items; } @Override public int getCount() { return items.size(); } @Override public Object getItem(int i) { return items.get(i); } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View v = LayoutInflater.from(context).inflate(R.layout.list_view_item_type2, null); ImageView icon = v.findViewById(R.id.icon); TextView text = v.findViewById(R.id.text); icon.setImageResource(items.get(i).getImage()); text.setText(items.get(i).getName()); //event handling code can be done here return v; } } } activity_navigation_drawer.xml activity_navigation_drawer.xml XHTML <?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_navigation_drawer" 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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingTop="32dp" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin"> <TextView android:id="@+id/navTitleTV" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:text="Menu" android:textSize="25dp" android:textStyle="bold" /> <ListView android:id="@+id/navLV" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout> 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 <?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_navigation_drawer" 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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingTop="32dp" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin"> <TextView android:id="@+id/navTitleTV" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:text="Menu" android:textSize="25dp" android:textStyle="bold" /> <ListView android:id="@+id/navLV" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout> app_bar_navigation_drawer.xml app_bar_navigation_drawer.xml XHTML <?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" tools:context=".NavigationDrawer"> <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_navigation_drawer" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout> 123456789101112131415161718192021222324252627282930313233 <?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" tools:context=".NavigationDrawer"> <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_navigation_drawer" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout> content_navigation_drawer.xml content_navigation_drawer.xml XHTML <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".NavigationDrawer" tools:showIn="@layout/app_bar_navigation_drawer"> <!-- Activity Content/Controls Goes Here --> </android.support.constraint.ConstraintLayout> 12345678910111213 <?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".NavigationDrawer" tools:showIn="@layout/app_bar_navigation_drawer"> <!-- Activity Content/Controls Goes Here --> </android.support.constraint.ConstraintLayout> list_view_item_type1.xml list_view_item_type1.xml XHTML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="@dimen/activity_horizontal_margin" /> </LinearLayout> 1234567891011121314 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="@dimen/activity_horizontal_margin" /> </LinearLayout> list_view_item_type2.xml list_view_item_type2.xml XHTML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/icon" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="@dimen/activity_horizontal_margin" android:text="Item"/> </LinearLayout> 1234567891011121314151617181920 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/icon" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="@dimen/activity_horizontal_margin" android:text="Item"/> </LinearLayout> Video https://zatackcoder.com/wp-content/uploads/2019/07/listview-inside-navigationdrawer.mp4 Download Android Studio Project ListView inside NavigationDrawer in Android 1 file(s) 14.08 MB Download Thanks for stopping by Please do share if you like it