Thursday, 14 September 2017

Android Recyclerview Cards With Images In Gridview Style


#ref-menu

1)Cards.Java

class Cards {
    Integer Img;
    String category;


    public Cards(Integer img, String category ) {

        Img = img;
        this.category = category;
    }



    public String getCategory() {
        return category;
    }

    public int getImg() {
        return Img;
    }
}

2)Cards_Adapter.java

Class Cards_Adapter extends RecyclerView.Adapter<Cards_Adapter.ViewHolder>
{
    List<Cards> list;
    Context context;

    public Cards_Adapter(List<Cards> 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.msg_category_cards, parent, false);
        return new Cards_Adapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
       Cards Cards = list.get(position);
        final int img = Cards.getImg();
        final String categiry = Cards.getCategory();

        holder.sms_cat_img.setImageResource(img);
        holder.sms_category.setText(categiry);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView sms_cat_img;
        TextView sms_category;
        public ViewHolder(View itemView) {
            super(itemView);

            sms_cat_img = (ImageView)itemView.findViewById(R.id.sms_cat_img);
            sms_category = (TextView) itemView.findViewById(R.id.sms_category);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, MainActivity.class);
                    Bundle extras = new Bundle();
                    extras.putString("category_msg",sms_category.getText().toString());
                    intent.putExtras(extras);
                    view.getContext().startActivity(intent);
                }
            });
        }
    }
}

3)Cards.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="wrap_content"
    android:background="@drawable/card_shadow">


<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/card_shadow">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/card_shadow">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:id="@+id/sms_cat_img"
            android:padding="10dp"
            android:src="@drawable/bank_icon"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/sms_category"
            android:text="Category Name"
            android:textSize="18dp"
            android:textColor="@color/black"
            android:textStyle="bold"/>

    </LinearLayout>



</android.support.v7.widget.CardView>
</LinearLayout>

 4) MyData.Java

public class MyData {
    static String[] nameArray = {"Property","Offers","Travel","Self-Help","GOVT","Bank","Investment",
            "Entertainment","Fashion","OTP","Wallet","Income Tax","Insurance", "Uncategorized"};

    static Integer[] drawableArray = {
            R.drawable.property_icon,
            R.drawable.offers_icon,
            R.drawable.travel_icon,
            R.drawable.self_help_icon,
            R.drawable.govt_icon,
            R.drawable.bank_icon,
            R.drawable.investment_icon,
            R.drawable.entertainment_icon,
            R.drawable.fashion_icon,
            R.drawable.otp_icon,
            R.drawable.wallet_icon,
            R.drawable.tax_icon,
            R.drawable.insurance_icon,
            R.drawable.uncategorized};

}

5) MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="sms.smsrec.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/sms_category_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        >
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>


5)MainActivity.java

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private List<Cards> Cardss;
    private RecyclerView.LayoutManager mLayoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        recyclerView = (RecyclerView) findViewById(R.id.sms_category_recycler_view);
        recyclerView.setHasFixedSize(true);

       //  To Set Gridview Style
        mLayoutManager = new GridLayoutManager(getApplicationContext(),2);
        recyclerView.setLayoutManager(mLayoutManager);
        Cardss = new ArrayList<>();
        SMS_Category();
    }

    private void SMS_Category()
    {
        for (int i = 0; i < MyData.nameArray.length; i++) {
            Cardss.add(new Cards(
                    MyData.drawableArray[i],
                    MyData.nameArray[i]
            ));
        }

        adapter = new Cards_Adapter(Cardss, getApplicationContext());
        recyclerView.setAdapter(adapter);
    }
}

No comments:

Android Studio - Get Current Latitude And Longitude

1. Add this dependencies in your gradle file     implementation 'com.google.android.gms:play-services-location:18.0.0' 2. Add this p...