1) Add This Two Dependency In Your build.gradle File:
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
2) Hide Toolbar From Manifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.miajs.dashboardui">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3) Cards_layout.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_layout"
android:orientation="vertical">
<!--Todo: CardView-->
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="12dp"
app:cardElevation="6dp"
app:cardMaxElevation="6dp"
app:contentPadding="25dp"
app:cardBackgroundColor="@android:color/white"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/card_img"
android:padding="10dp"
android:src="@drawable/alarm"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/card_title"
android:text="Category Name"
android:textSize="18dp"
android:textColor="@android:color/black"
android:textStyle="bold"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
4) Download The Drawable Resource File From Here
https://drive.google.com/open?id=1MBpcgppAWvpr0O32yFWGSTt_ga3mQD9M
5) MainActivity.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:background="@drawable/bg1"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_back"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mijas Siklodi"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2:00 pm"/>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
6) List_pojo.java
public class List_pojo {
/*Todo: Pojo class*/
Integer Img;
String category;
public List_pojo(Integer img, String category ) {
Img = img;
this.category = category;
}
public String getCategory() {
return category;
}
public int getImg() {
return Img;
}
}
7) ListAdapter.java
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder>
{
/*Todo: Recyclerview Adapter class*/
List<List_pojo> list;
Context context;
public ListAdapter(List<List_pojo> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cards_layout, parent, false);
final ViewHolder viewHolder = new ViewHolder(view);
/*Todo: adding Click Events Of Each Menu On Dashboard by title*/
viewHolder.card_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (viewHolder.card_title.getText().toString().toLowerCase()){
case "alarm":
Toast.makeText(context, "Alarm Is click", Toast.LENGTH_SHORT).show();
break;
case "dollar":
Toast.makeText(context, "Dollar Is click", Toast.LENGTH_SHORT).show();
break;
case "location":
Toast.makeText(context, "location Is click", Toast.LENGTH_SHORT).show();
break;
case "photo":
Toast.makeText(context, "photo Is click", Toast.LENGTH_SHORT).show();
break;
case "train":
Toast.makeText(context, "Train Is click", Toast.LENGTH_SHORT).show();
break;
case "video":
Toast.makeText(context, "Video Is click", Toast.LENGTH_SHORT).show();
break;
case "watch":
Toast.makeText(context, "Watch Is click", Toast.LENGTH_SHORT).show();
break;
case "game":
Toast.makeText(context, "Game Is click", Toast.LENGTH_SHORT).show();
break;
case "store":
Toast.makeText(context, "Store Is click", Toast.LENGTH_SHORT).show();
break;
case "love":
Toast.makeText(context, "Love Is click", Toast.LENGTH_SHORT).show();
break;
}
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
List_pojo Cards = list.get(position);
final int img = Cards.getImg();
final String categiry = Cards.getCategory();
holder.card_img.setImageResource(img);
holder.card_title.setText(categiry);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView card_img;
TextView card_title;
LinearLayout card_layout;
public ViewHolder(View itemView) {
super(itemView);
card_img = (ImageView)itemView.findViewById(R.id.card_img);
card_title = (TextView) itemView.findViewById(R.id.card_title);
card_layout = (LinearLayout) itemView.findViewById(R.id.card_layout);
}
}
}
8) MyData.java
public class MyData {
/*Todo: Data To Load In Recyclerview*/
static String[] nameArray = {
"Alarm",
"Dollar",
"Location",
"Photo",
"Train",
"Video",
"Watch",
"Game",
"Store",
"Love"
};
static Integer[] drawableArray = {
R.drawable.alarm,
R.drawable.dollar,
R.drawable.location,
R.drawable.photo,
R.drawable.train,
R.drawable.video,
R.drawable.watch,
R.drawable.game,
R.drawable.store,
R.drawable.love};
}
9) MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<List_pojo> Cardss;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setHasFixedSize(true);
mLayoutManager = new GridLayoutManager(MainActivity.this,2);
recyclerView.setLayoutManager(mLayoutManager);
Cardss = new ArrayList<>();
loaddata();
}
private void loaddata()
{
for (int i = 0; i < MyData.nameArray.length; i++) {
Cardss.add(new List_pojo(
MyData.drawableArray[i],
MyData.nameArray[i]
));
}
adapter = new ListAdapter(Cardss, MainActivity.this);
recyclerView.setAdapter(adapter);
}
}