In android development, Navigation Drawer and Recycler View and tabs layout are some of the most popular interfaces that are used in apps. We often think to collaborate different types of layout in a single screen., with DrawerLayout, NavigationView, ViewPager, RecyclerView, CardView and TabLayout. With material design style, the theme can be combined in an Activity.
Firstly, add the design support library dependency to use Material Design Widgets, add RecyclerView dependency and CardView dependency to use them:
app/build.gradle
- apply plugin: 'com.android.application'
- android {
- compileSdkVersion 24
- buildToolsVersion "25.0.0"
- defaultConfig {
- applicationId "test.ku.codedivas.sampleapp"
- minSdkVersion 14
- targetSdkVersion 24
- versionCode 1
- versionName "1.0"
- testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- }
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
- exclude group: 'com.android.support', module: 'support-annotations'
- })
- compile 'com.android.support:appcompat-v7:24.2.1'
- compile 'com.android.support:design:24.2.1'
- compile 'com.android.support:recyclerview-v7:24.2.0'
- compile 'com.android.support:cardview-v7:24.2.0'
- testCompile 'junit:junit:4.12'
- }
Designing Activity Layout
The layout should be like:
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:app="http://schemas.android.com/apk/res-auto">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
- <android.support.v4.widget.DrawerLayout
- android:id="@+id/drawerLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fitsSystemWindows="true"
- xmlns:tools="http://schemas.android.com/tools"
- tools:openDrawer="start">
- <LinearLayout android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <android.support.design.widget.TabLayout
- android:id="@+id/tab_layout"
- android:layout_width="match_parent"
- app:tabGravity="fill"
- app:tabMode="fixed"
- android:layout_height="?attr/actionBarSize"
- android:background="@color/colorPrimary"
- android:minHeight="?attr/actionBarSize"
- android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
- <android.support.v4.view.ViewPager
- android:id="@+id/view_pager"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <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>
- </LinearLayout>
Here, We have to put ViewPager and Tab Layout in the first child of DrawerLayout, it will make a swipeable view with tabs later and it's child is a Navigation View.
nav_header_main.xml
activity_main_drawer.xml
nav_header_main.xml
- <?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"
- android:layout_width="match_parent"
- android:layout_height="160dp"
- android:paddingBottom="16dp"
- android:paddingLeft="16dp"
- android:paddingRight="16dp"
- android:paddingTop="16dp"
- android:theme="@style/ThemeOverlay.AppCompat.Dark"
- android:orientation="vertical"
- android:gravity="bottom">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingTop="16dp"
- app:srcCompat="@mipmap/ic_launcher"
- android:id="@+id/imageView" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingTop="16dp"
- android:text="Android Studio"
- android:textColor="#ffffff"
- android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="android.studio@android.com"
- android:textAppearance="?android:textAppearanceSmall"
- android:textColor="#ffffff"
- android:textStyle="bold"
- android:id="@+id/textView" />
- </LinearLayout>
The nav_header_main.xml file is made to build the sliding menu drawer.
activity_main_drawer.xml
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <group android:checkableBehavior="single">
- <item
- android:id="@+id/nav_camera"
- android:icon="@drawable/ic_menu_camera"
- android:title="Fragment One" />
- <item
- android:id="@+id/nav_gallery"
- android:icon="@drawable/ic_menu_gallery"
- android:title="Fragment Two" />
- <item
- android:id="@+id/nav_slideshow"
- android:icon="@drawable/ic_menu_slideshow"
- android:title="Fragment Three" />
- <item
- android:id="@+id/nav_manage"
- android:icon="@drawable/ic_menu_manage"
- android:title="NextActivity" />
- </group>
- <item android:title="Communicate">
- <menu>
- <item
- android:id="@+id/nav_share"
- android:icon="@drawable/ic_menu_share"
- android:title="Share" />
- <item
- android:id="@+id/nav_send"
- android:icon="@drawable/ic_menu_send"
- android:title="Send" />
- </menu>
- </item>
- </menu>
The activity_main_drawer.xml file is made to fill the values into sliding menu drawer.
Program Code
In our Main Activity, make sure to locate all the views, set the adapter for ViewPager (containing fragments which have RecyclerView embedded), attach TabLayout with ViewPager. It must implement OnNavigationItemSelectedListener interface to handle NavigationView item clicked event.
Source code should be like:
MainActivity.java
- package test.ku.codedivas.sampleapp;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.design.widget.TabLayout;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.view.ViewPager;
- import android.view.View;
- import android.support.design.widget.NavigationView;
- 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.Menu;
- import android.view.MenuItem;
- public class MainActivity extends AppCompatActivity
- implements NavigationView.OnNavigationItemSelectedListener {
- private ViewPager viewPager;
- private DrawerLayout drawer;
- public String title;
- private TabLayout tabLayout;
- private String[] pageTitle = {"First", "Second", "Third"};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- viewPager = (ViewPager)findViewById(R.id.view_pager);
- tabLayout = (TabLayout) findViewById(R.id.tab_layout);
- for (int i = 0; i < 3; i++) {
- tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i]));
- }
- //set gravity for tab bar
- tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
- 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);
- assert navigationView != null;
- navigationView.setNavigationItemSelectedListener(this);
- //set viewpager adapter
- ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
- viewPager.setAdapter(pagerAdapter);
- //change Tab selection when swipe ViewPager
- viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
- //change ViewPager page when tab selected
- tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
- @Override
- public void onTabSelected(TabLayout.Tab tab) {
- viewPager.setCurrentItem(tab.getPosition());
- }
- @Override
- public void onTabUnselected(TabLayout.Tab tab) {
- }
- @Override
- public void onTabReselected(TabLayout.Tab tab) {
- }
- });
- if (savedInstanceState == null) {
- navigationView.getMenu().performIdentifierAction(R.id.nav_camera,0);
- }
- }
- @Override
- public void onBackPressed() {
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
- if (drawer.isDrawerOpen(GravityCompat.START)) {
- drawer.closeDrawer(GravityCompat.START);
- } else {
- super.onBackPressed();
- }
- }
- @SuppressWarnings("StatementWithEmptyBody")
- @Override
- public boolean onNavigationItemSelected(MenuItem item) {
- // Handle navigation view item clicks here.
- FragmentManager fm = getSupportFragmentManager();
- switch(item.getItemId()) {
- case R.id.nav_camera:
- title="Sample App";
- viewPager.setCurrentItem(0);
- break;
- case R.id.nav_gallery:
- viewPager.setCurrentItem(1);
- break;
- case R.id.nav_slideshow:
- viewPager.setCurrentItem(2);
- break;
- case R.id.nav_manage:
- Intent intent = new Intent(MainActivity.this,NextActivity.class);
- startActivity(intent);
- break;
- }
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
- item.setChecked(true);
- getSupportActionBar().setTitle(title);
- assert drawer != null;
- drawer.closeDrawer(GravityCompat.START);
- return true;
- }
- }
Customizing the ViewPager adapter based on FragmentPagerAdapter or FragmentStatePagerAdapter.
ViewPagerAdapter.java
- package test.ku.codedivas.sampleapp;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentPagerAdapter;
- public class ViewPagerAdapter extends FragmentPagerAdapter {
- public ViewPagerAdapter(FragmentManager fm) {
- super(fm);
- }
- @Override
- public Fragment getItem(int position) {
- if (position ==0) {
- return new FirstFragment();
- }
- else if (position == 1) {
- return new SecondFragment();
- }
- else
- return new ThirdFragment();
- }
- @Override
- public int getCount() {
- return 3;
- }
- }
Now, we need to make the list item layout for RecyclerView. The layout code is as:
list_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v7.widget.CardView
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:card_view="http://schemas.android.com/apk/res-auto"
- android:id="@+id/cvItem"
- android:layout_gravity="top"
- android:layout_width="match_parent"
- android:layout_height="65dp"
- android:layout_marginBottom="4dp"
- android:layout_marginLeft="8dp"
- android:layout_marginRight="8dp"
- android:layout_marginTop="4dp"
- android:minHeight="65dp"
- card_view:cardCornerRadius="10dp">
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/text_container"
- android:layout_width="match_parent"
- android:layout_height="65dp"
- android:minHeight="65dp"
- android:gravity="center_vertical"
- android:orientation="vertical">
- <TextView
- android:id="@+id/list_text"
- android:text="text"
- android:gravity="center_vertical"
- android:layout_gravity="center"
- android:paddingLeft="16dp"
- android:textSize="16sp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textAppearance="?android:textAppearanceSmall"
- android:textColor="@android:color/black"
- android:textStyle="normal"/>
- </LinearLayout>
- </android.support.v7.widget.CardView>
Now, We need to make the model for taking data in RecyclerView and adapter class also needs to be built for setting data into it and adding RecyclerView item one after the other.
RecyclerViewModel.java
- package test.ku.codedivas.sampleapp;
- public class RecyclerViewModel {
- private String mItemName;
- public RecyclerViewModel(String ItemName){
- mItemName = ItemName;
- }
- public String getmItemName() {
- return mItemName;
- }
- }
RecyclerViewAdapter.java
- package test.ku.codedivas.sampleapp;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.support.v7.widget.CardView;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
- private static final String LOG_TAG = RecyclerViewAdapter.class.getSimpleName();
- private ArrayList<RecyclerViewModel> mItems;
- ItemListener mListener;
- Context context;
- public static int position;
- public RecyclerViewAdapter(Activity context, ArrayList<RecyclerViewModel> program, ItemListener listener) {
- this.context = context;
- mItems = program;
- mListener = listener;
- }
- public void setOnItemClickListener(ItemListener listener) {
- mListener = listener;
- }
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View v = LayoutInflater
- .from(parent.getContext())
- .inflate(R.layout.list_item, parent, false);
- context = parent.getContext();
- return new ViewHolder(v);
- }
- @Override
- public void onBindViewHolder(ViewHolder holder, int position) {
- holder.setData(mItems.get(position));
- }
- @Override
- public int getItemCount() {
- if (mItems != null) {
- return mItems.size();
- }
- return 0;
- }
- public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
- public CardView cardView;
- public RecyclerViewModel pName;
- TextView name;
- View textContainer;
- public ViewHolder(View itemView) {
- super(itemView);
- itemView.setOnClickListener(this);
- cardView = (CardView)itemView.findViewById(R.id.cvItem);
- name = (TextView) itemView.findViewById(R.id.list_text);
- textContainer = itemView.findViewById(R.id.text_container);
- }
- public void setData(RecyclerViewModel pName) {
- this.pName = pName;
- name.setText(pName.getmItemName());
- }
- @Override
- public void onClick(View v) {
- if (mListener != null) {
- mListener.onItemClick(pName, getAdapterPosition());
- }
- Toast.makeText(context,pName.getmItemName(),Toast.LENGTH_SHORT).show();
- }
- }
- public interface ItemListener {
- void onItemClick(RecyclerViewModel pName, int position);
- }
- }
There are 3 fragments of ViewPager in this project.
FirstFragment.java
- package test.ku.codedivas.sampleapp;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import java.util.ArrayList;
- public class FirstFragment extends Fragment {
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_layout,container, false);
- return rootView;
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- final ArrayList<RecyclerViewModel> pname = new ArrayList<RecyclerViewModel>();
- pname.add(new RecyclerViewModel("Alpha"));
- pname.add(new RecyclerViewModel("Beta"));
- pname.add(new RecyclerViewModel("Cupcake"));
- pname.add(new RecyclerViewModel("Donut"));
- pname.add(new RecyclerViewModel("Eclairs"));
- pname.add(new RecyclerViewModel("Froyo"));
- pname.add(new RecyclerViewModel("GingerBread"));
- pname.add(new RecyclerViewModel("HoneyComb"));
- pname.add(new RecyclerViewModel("IceCreamSandwich"));
- pname.add(new RecyclerViewModel("JellyBean"));
- pname.add(new RecyclerViewModel("KitKat"));
- pname.add(new RecyclerViewModel("Lollipop"));
- pname.add(new RecyclerViewModel("MarshMallow"));
- pname.add(new RecyclerViewModel("Nougat"));
- final RecyclerViewAdapter itemsAdapter = new RecyclerViewAdapter(FirstFragment.this.getActivity(), pname, null);
- final RecyclerView clv = (RecyclerView) view.findViewById(R.id.clist);
- clv.setLayoutManager(new LinearLayoutManager(FirstFragment.this.getActivity()));
- clv.setHasFixedSize(true);
- clv.setAdapter(itemsAdapter);
- }
- }
SecondFragment.java
- package test.ku.codedivas.sampleapp;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import java.util.ArrayList;
- public class SecondFragment extends Fragment {
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_layout,container, false);
- return rootView;
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- final ArrayList<RecyclerViewModel> pname = new ArrayList<RecyclerViewModel>();
- pname.add(new RecyclerViewModel("Alpha"));
- pname.add(new RecyclerViewModel("Beta"));
- pname.add(new RecyclerViewModel("Cupcake"));
- pname.add(new RecyclerViewModel("Donut"));
- pname.add(new RecyclerViewModel("Eclairs"));
- pname.add(new RecyclerViewModel("Froyo"));
- pname.add(new RecyclerViewModel("GingerBread"));
- pname.add(new RecyclerViewModel("HoneyComb"));
- pname.add(new RecyclerViewModel("IceCreamSandwich"));
- pname.add(new RecyclerViewModel("JellyBean"));
- pname.add(new RecyclerViewModel("KitKat"));
- pname.add(new RecyclerViewModel("Lollipop"));
- pname.add(new RecyclerViewModel("MarshMallow"));
- pname.add(new RecyclerViewModel("Nougat"));
- final RecyclerViewAdapter itemsAdapter = new RecyclerViewAdapter(SecondFragment.this.getActivity(), pname, null);
- final RecyclerView clv = (RecyclerView) view.findViewById(R.id.clist);
- clv.setLayoutManager(new LinearLayoutManager(SecondFragment.this.getActivity()));
- clv.setHasFixedSize(true);
- clv.setAdapter(itemsAdapter);
- }
- }
ThirdFragment.java
- package test.ku.codedivas.sampleapp;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import java.util.ArrayList;
- public class ThirdFragment extends Fragment {
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_layout,container, false);
- return rootView;
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- final ArrayList<RecyclerViewModel> pname = new ArrayList<RecyclerViewModel>();
- pname.add(new RecyclerViewModel("Alpha"));
- pname.add(new RecyclerViewModel("Beta"));
- pname.add(new RecyclerViewModel("Cupcake"));
- pname.add(new RecyclerViewModel("Donut"));
- pname.add(new RecyclerViewModel("Eclairs"));
- pname.add(new RecyclerViewModel("Froyo"));
- pname.add(new RecyclerViewModel("GingerBread"));
- pname.add(new RecyclerViewModel("HoneyComb"));
- pname.add(new RecyclerViewModel("IceCreamSandwich"));
- pname.add(new RecyclerViewModel("JellyBean"));
- pname.add(new RecyclerViewModel("KitKat"));
- pname.add(new RecyclerViewModel("Lollipop"));
- pname.add(new RecyclerViewModel("MarshMallow"));
- pname.add(new RecyclerViewModel("Nougat"));
- final RecyclerViewAdapter itemsAdapter = new RecyclerViewAdapter(ThirdFragment.this.getActivity(), pname, null);
- final RecyclerView clv = (RecyclerView) view.findViewById(R.id.clist);
- clv.setLayoutManager(new LinearLayoutManager(ThirdFragment.this.getActivity()));
- clv.setHasFixedSize(true);
- clv.setAdapter(itemsAdapter);
- }
- }
In order to show Recycler View in each fragment of ViewPager, the layout code should be like this:
fragment_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v7.widget.RecyclerView
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/clist"
- android:drawSelectorOnTop="true"
- android:background="#D0D0D0"
- app:layout_scrollFlags="scroll|enterAlways"
- app:layout_behavior="@string/appbar_scrolling_view_behavior" />
Now, when we need to call next activity on sliding menu item click then we need to create new Activity and its layout and call it from MainActivity of sliding menu (Navigation Drawer).
The code for new activity should be like this:
NextActivity.java
- package test.ku.codedivas.sampleapp;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- public class NextActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_next);
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- }
- @Override
- public void onBackPressed() {
- finish();
- }
- }
In layout file, any layout can be created according the user.
The code for layout could be like this:
activity_next.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/activity_next"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_gravity="center_vertical"
- android:gravity="center_vertical"
- android:orientation="vertical">
- <TextView
- android:id="@+id/tv1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Hi"
- android:gravity="center"
- android:textSize="40sp"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="Inside NextActivity"
- android:textSize="30sp"/>
- </LinearLayout>
Note: In this project, I use Toolbar as ActionBar, so the project theme is "No Action Bar" theme.
styles.xml
- <resources>
- <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
- <!-- Customize your theme here. -->
- <item name="colorPrimary">@color/colorPrimary</item>
- <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
- <item name="colorAccent">@color/colorAccent</item>
- </style>
- </resources>
The manifest file should be like this:
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="test.ku.codedivas.sampleapp">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name"
- android:theme="@style/AppTheme.NoActionBar">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".NextActivity"
- android:label="@string/activityname"
- android:parentActivityName=".MainActivity">
- <meta-data
- android:name="android.support.PARENT_ACTIVITY"
- android:value=".MainActivity"/>
- </activity>
- </application>
- </manifest>
Running Application
Demo Video :
Output should be like:
Conclusions
Through this post, I hope you could learn about combining the popular and powerful widgets in developing Android Application. Further, you can subscribe my blog to get the newest tutorials..!
thanks for the Combining Navigation Drawer and RecyclerView in ViewPager Fragments(with Tab) in one screen,
ReplyDeleteI tried and its working
waiting for your future post
Thanks Ashish :)
DeleteIt's working 👍
ReplyDeleteGreat..!
Deleteapp:headerLayout="@layout/nav_header_main"
ReplyDeleteapp:menu="@menu/activity_main_drawer" />
getiing errorr
can u plz uload full source code
ReplyDeleteHi Rushi,
DeletePlease check the updated code and let me know if you still get an error.
Thanks
protected void onCreate(Bundle savedInstanceState) {
ReplyDeletesuper.onCreate(savedInstanceState);
setContentView(R.layout.pagerdrawer);
no layout pagerdrawer
Hi Rahmat,
DeleteLayout pagerdrawer is to be replaced with activity_main layout. Please check. Thanks
thanks CodeDivas, but i still get force close when i click nextactivity in navigation drawer,could u check it again and give the solution?
ReplyDeleteCode is completely working fine. Could you let me know What error you are getting?
Deletecould you post it using retrofit
ReplyDeleteHi Atul,
DeleteThis post is related to provide an idea to add content in recyclerView Listitem, not about extracting the data from server and then displaying it in recyclerView.
I would try to cover up your suggestion in my next post.
Thanks!
Thanks codedivas...great blog
ReplyDeleteHow to add content inside that recycler view
ReplyDeleteHi,
DeleteI'm not able to get your question but according to my understanding you might be asking about adding text or any other content in recyclerView. So, firstly, manipulate the list_item.xml file only according to your requirement.
Make a model of the contents( like string or integer variables) you want to show in recyclerview list item then in the Activity(or fragment) make an arraylist of your model type.
Using the same reference variable of that arraylist, you can call add() method to add different data items accordingly.
In my case, I add only one textview in list_item.xml file and use it in RecyclerViewModel as String variable. Then, in Activity, I used that variable to add different contents like:
final ArrayList pname = new ArrayList();
pname.add(new RecyclerViewModel("Tomato")); // passing the string value to model. you can pass multiple values as parameters according to the number of parameters of your Model constructor
pname.add(new RecyclerViewModel("Potato"));
Hope it's helpful to you.
Thanks
nice! big help! could please show us how to filter those items in the recyclerview using searchview? a searchview that will work in all tabs. thanks a lot!
ReplyDeleteHi Jiren,
DeleteYou can refer to http://searchonrecyclerviewbycodedivas.blogspot.co.uk/2017/04/search-on-recyclerview-having-cardview.html to filter those items in the recyclerview using searchview.
pode liberar para download?
ReplyDeletethanks for the Combining Navigation Drawer and RecyclerView in ViewPager Fragments(with Tab) in one screen,
ReplyDeleteI tried and its working but i want with Actionbar iam using your code but its not showing the actionbar.please help me
waiting for your future post
R.string.navigation_drawer_open, R.string.navigation_drawer_close
ReplyDeletehow to fix this redline?
In string.xml, add a string name for navigation_drawer_open and navigation_drawer_close.
Deletehay, im confusing, how to open other activity for every recycler view we click?
Deletehello friend, very good job, it would be possible to add icons to the tablayout thanks a big hello
ReplyDeleteTheres error in toolbar
ReplyDeletecheck the image here :
https://ibb.co/H7GLVRh
please reply
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
ReplyDeleteerror in nextactivity
I was surfing the Internet for information and came across your blog. I am impressed by the information you have on this blog. It shows how well you understand this subject. Viking Freezer Repair in Los Angeles
ReplyDeleteThe Best Casinos in Canada | List of All The Best Online Casinos
ReplyDeleteTop casinos from every Canadian luckyclub to choose a top Canadian online casino that's a winner of the prestigious EGR SuperBook of the Year award.