Android Applications In earlier days we are using ActionBar Tabs over ViewPager But
this ActionBar Tabs is deprecated in material design(Introduced Android 5.0(Lollipop)).
So for that we need to
use Android Material Design Tabs for implementing tabs in our app.
We have to seen like Google Play Store , Whatsapp and many applications
are using Android Material Design Tabs.
Today I’m discussing how to implement this Material Design
Tabs in our application.
activity_main.xml
<?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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.anil.anil.recyclerviewwithtabs.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
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:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"></android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:visibility="gone"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
layout_today.xml
----------------------
<?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">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
layout_yesterday.xml
---------------------------
<?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">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
layout_tomarrow.xml
--------------------------
<?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">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
list_row.xml
------------------
<?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:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/country_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="20sp" />
<TextView
android:id="@+id/country_iso"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ISo"
android:textSize="16sp" />
</LinearLayout>
Today.java
--------------
package com.pass.anil.recyclerviewwithtabs.fragments;
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 com.anil.anil.recyclerviewwithtabs.R;
import com.anil.anil.recyclerviewwithtabs.adapter.RVAdapter;
import com.anil.anil.recyclerviewwithtabs.model.Student;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Anil on 2/17/2017.
*/
public class Today extends Fragment {
RecyclerView student_rview;
List<Student> list_student;
RVAdapter rvAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_today, container, false);
loadStudent();
student_rview = (RecyclerView) view.findViewById(R.id.recyclerview);
rvAdapter = new RVAdapter(getActivity(), list_student);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
student_rview.setLayoutManager(layoutManager);
student_rview.setAdapter(rvAdapter);
return view;
}
void loadStudent() {
list_student = new ArrayList<>();
list_student.add(new Student("Anil", "Tadipatri"));
list_student.add(new Student("Bhaskar", "Tadipatri"));
list_student.add(new Student("Manohar", "Tadipatri"));
list_student.add(new Student("Gopal", "Tadipatri"));
list_student.add(new Student("Krishna", "Tadipatri"));
list_student.add(new Student("JayaRam", "Tadipatri"));
list_student.add(new Student("Kadiri", "Tadipatri"));
// rvAdapter.notifyDataSetChanged();
}
}
Yesterday.Java
----------------------
package com.pass.anil.recyclerviewwithtabs.fragments;
import android.os.Bundle;
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 com.anil.anil.recyclerviewwithtabs.R;
import com.anil.anil.recyclerviewwithtabs.adapter.RVAdapter;
import com.anil.anil.recyclerviewwithtabs.model.Student;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Anil on 2/17/2017.
*/
public class Yesterday extends Fragment {
RecyclerView student_rview;
List<Student> list_student;
RVAdapter rvAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_yesterday, container, false);
loadStudent();
student_rview = (RecyclerView) view.findViewById(R.id.recyclerview);
rvAdapter = new RVAdapter(getActivity(), list_student);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
student_rview.setLayoutManager(layoutManager);
student_rview.setAdapter(rvAdapter);
return view;
}
void loadStudent() {
list_student = new ArrayList<>();
list_student.add(new Student("Adi Reddy", "S.G.Palli"));
list_student.add(new Student("N.Swamy", "S.G.Palli"));
list_student.add(new Student("Sriram", "S.G.Palli"));
list_student.add(new Student("Ram", "S.G.Palli"));
// rvAdapter.notifyDataSetChanged();
}
}
Tomarrow.Java
----------------------
package com.pass.anil.recyclerviewwithtabs.fragments;
import android.os.Bundle;
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 com.anil.anil.recyclerviewwithtabs.R;
import com.anil.anil.recyclerviewwithtabs.adapter.RVAdapter;
import com.anil.anil.recyclerviewwithtabs.model.Student;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Anil on 2/17/2017.
*/
public class Tomorrow extends Fragment {
RecyclerView student_rview;
List<Student> list_student;
RVAdapter rvAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_tommarow, container, false);
loadStudent();
student_rview = (RecyclerView) view.findViewById(R.id.recyclerview);
rvAdapter = new RVAdapter(getActivity(), list_student);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
student_rview.setLayoutManager(layoutManager);
student_rview.setAdapter(rvAdapter);
return view;
}
void loadStudent() {
list_student = new ArrayList<>();
list_student.add(new Student("Mahesh", "Pappaka"));
list_student.add(new Student("Anand", "Pappaka"));
list_student.add(new Student("Aravind", "Pappaka"));
list_student.add(new Student("Prasanth", "Pappaka"));
list_student.add(new Student("Kiran", "Pappaka"));
list_student.add(new Student("Charan", "Pappaka"));
// rvAdapter.notifyDataSetChanged();
}
}
Student.Java
-------------------
package com.anil.anil.recyclerviewwithtabs.model;
/**
* Created by Anil on 2/17/2017.
*/
public class Student {
String name,add;
public Student(String name, String add) {
this.name = name;
this.add = add;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAdd() {
return add;
}
public void setAdd(String add) {
this.add = add;
}
}
ItemViewHolder.Java
--------------------------
package com.anil.anil.recyclerviewwithtabs.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.anil.anil.recyclerviewwithtabs.R;
import com.anil.anil.recyclerviewwithtabs.model.Student;
public class ItemViewHolder extends RecyclerView.ViewHolder {
public TextView name_TextView;
public TextView iso_TextView;
public ItemViewHolder(View itemView) {
super(itemView);
itemView.setClickable(true);
name_TextView = (TextView) itemView.findViewById(R.id.country_name);
iso_TextView = (TextView) itemView.findViewById(R.id.country_iso);
}
public void bind(Student student) {
name_TextView.setText(student.getName());
iso_TextView.setText(student.getAdd() );
}
}
RVAdapter.Java
-------------------
package com.pass.anil.recyclerviewwithtabs.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.anil.anil.recyclerviewwithtabs.R;
import com.anil.anil.recyclerviewwithtabs.model.Student;
import java.util.List;
/**
* Created by Anil on 2/17/2017.
*/
public class RVAdapter extends RecyclerView.Adapter<ItemViewHolder> {
Context mcContext;
List<Student> studentList;
public RVAdapter(Context mcContext, List<Student> studentList) {
this.mcContext = mcContext;
this.studentList = studentList;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
Student student = studentList.get(position);
holder.bind(student);
}
@Override
public int getItemCount() {
return studentList.size();
}
}
MainActivity.Java
---------------------------
package com.anil.anil.recyclerviewwithtabs;
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.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.anil.anil.recyclerviewwithtabs.adapter.ViewPagerAdapter;
import com.anil.anil.recyclerviewwithtabs.fragments.Today;
import com.anil.anil.recyclerviewwithtabs.fragments.Tomorrow;
import com.anil.anil.recyclerviewwithtabs.fragments.Yesterday;
public class MainActivity extends AppCompatActivity {
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) 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();
}
});
}
@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) {
// 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);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Today(), "Today");
adapter.addFragment(new Yesterday(), "Yesterday");
adapter.addFragment(new Tomorrow(), "Tomorrow");
viewPager.setAdapter(adapter);
}
}