Create RecyclerView.Adapter Adapter with One-Click

Yugandhar
Android App Development for beginners
2 min readAug 25, 2019

Hi Developers while Developing Android Project we commonly use RecyclerView for displaying Data in the form of list. If we have to create multiple times we going to copy past the code and modify the code to avoid this we have simple solution in-front of us. Follow these steps.

  1. Open the Settings dialog in android studio from File->Settings then select Editor->File and Code Templates option then add new file template by clicking + icon
Creating File Templates

2. Then Provide the name for the template Eg. RecyclerViewAdapter

Template name RecyclerViewAdapter.

3. Then Past the provided code here

#if (${PACKAGE_NAME} != “”)package ${PACKAGE_NAME};#end

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class ${NAME} extends RecyclerView.Adapter<${NAME}.${ViewHolderName}> {
private Context context;
private ArrayList<String>list;

public ${NAME}(Context context,ArrayList<String> list) {
this.context = context;
this.list=list;
}
@NonNull
@Override
public ${NAME}.${ViewHolderName} onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.${Layout}, viewGroup, false);
return new ${NAME}.${ViewHolderName}(view);
}

@Override
public void onBindViewHolder(@NonNull ${NAME}.${ViewHolderName} viewHolder, int i) {

}

@Override
public int getItemCount() {
return list.size();
}
class ${ViewHolderName} extends RecyclerView.ViewHolder{
public ${ViewHolderName}(@NonNull View itemView) {
super(itemView);
}
}
}

4. That’s it you crated a file template. Now we have to use it Right click on the folder to insert new Adapter File. Select New->RecyclerViewAdapter Option.

RecyclerViewAdapter Option

5. Now provide the AdapterName, ViewHolder name, Layout name.

6. That’s it our work is easy with this small one time file creation reduce our work.

If you like my article, please don’t forget to click 👏👏👏 to recommend it to others 👏👏👏.

Also, to be notified about my new articles and stories, follow me on Twitter. You can find me on LinkedIn as well. Cheers!

--

--