home top ad

How to create a list item with RecyclerView in Android Studio

Hello friends today in this article we will learn that How to create a list item with RecyclerView in Android Studio.
We will show each item one by one using the list, which can also contain an image or title.In this article we will see step by step how we will make all items show item using cardView

Step 1. Create project :

First we will open Android Studio As soon as we create the project, we will see an xml file and a java file.

Step 2: Create xml and Java

After creating the project, in the activity_main xml file, a RecyclerView view will be created.Whose code is given step by step.

acivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/const_select_img"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EBC90582">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/no_file"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No Compressed File"
        android:layout_centerInParent="true"
        android:textSize="18sp"
        android:textColor="@color/purple_700"
        android:visibility="gone" />

</RelativeLayout>
Here we have used a RecyclerView view, all the items will be shown in the list in turn After that we will perform this operation in MainActivity.java.whose code is given below.

MainActivity.java

package com.example.imagecompressor;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class CompressedActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ArrayList<Model> arrayList;
    TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.compress_file);
        recyclerView=findViewById(R.id.recyclerView);
        recyclerView.setVisibility(View.VISIBLE);
        textView=findViewById(R.id.no_file);
        arrayList=new ArrayList<>();
        Model model1=new Model("java");
        Model model2=new Model("android");
        Model model3=new Model("C");
        Model model4=new Model("C++");
        Model model5=new Model("Python");
        Model model6=new Model("C#");
        Model model7=new Model("Ruby");
        Model model8=new Model("HTML");
        Model model9=new Model("JavaScript");
        Model model10=new Model("R");
        arrayList.add(model1);
        arrayList.add(model2);
        arrayList.add(model3);
        arrayList.add(model4);
        arrayList.add(model5);
        arrayList.add(model6);
        arrayList.add(model7);
        arrayList.add(model8);
        arrayList.add(model9);
        arrayList.add(model10);
        if (arrayList.size()==11)
        {
            textView.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }
        else
        {
            textView.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
        CompressAdapter messageAdapter=new CompressAdapter(this,arrayList);
        RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(messageAdapter);
    }

}
In this class we will create a recyclerview, a model class and an adapter named CompressAdapter whose code in turn is given below.

Model.class

package com.example.imagecompressor;

public class Model {
    String text;

    public Model(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}
Here we have used a string, a constructor and a getter setter.

CompressAdapter.java
package com.example.imagecompressor;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class CompressAdapter extends RecyclerView.Adapter<CompressAdapter.ViewHolder> {

    private ArrayList<Model>arrayList;
    private Context context;

    public CompressAdapter(Context context, ArrayList<Model> arrayList) {
        this.arrayList = arrayList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
        View view=layoutInflater.inflate(R.layout.item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Model model=arrayList.get(position);
        holder.textView.setText(model.getText());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.textView);
        }
    }
}
In this class we have used a constructor which will get the context and all the data which is coming from the activity here.
And it has an inner class of ViewHolder which extends RecyclerView.ViewHolder class.

item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="100dp"
    android:layout_margin="8dp"
    android:elevation="3dp"
    app:cardBackgroundColor="@color/cardview_light_background"
    app:cardElevation="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_marginBottom="5dp"
            android:gravity="center"
            android:text="textView" />


    </LinearLayout>
</androidx.cardview.widget.CardView>
Here we have used a cardview inside which a textwave is used an imageview.

I hope this article will be of great use to you.

Thanks you for supporting....

Post a Comment

0 Comments