Skip to main content

Simplify Your Tax Calculations with GST Calculator 2024: The Ultimate Tool for Businesses and Individuals

Create Any Recyclerview Adapter within 5 seconds, in Android | Kotlin.

 In every single android application, we need to make many number of recycler-view adapter. So every time you create a blank adapter and modify it as per your requirements. But it is very boring and as well as time taking. So here I give you a solution that you can create any recyclerview adapter within 5 seconds using file template in Android Studio.

So, Lets read this full article-

First we open android studio and create a project. After project creation 

Open File->new->Edit File Template...

Follow this below screenshot


Okay, After click on "Edit File Template" a popup is open "File and Code Template", Now click on the 

"+" icon and set set your template name.


Now set your template name, template extension and template body. after doing everything click on "Apply" and then click on "OK"

The template body I after after the screenshot


Template Body

Copy this code and paste it on the template body section, which I marked in screenshot.

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "") package ${PACKAGE_NAME}

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView


#parse("File Header.java") 
class $AdapterName(private val list: ArrayList<$Model>) :
    RecyclerView.Adapter<$AdapterName .ViewHolder>() {

    //this method is returning the view for each item in the list
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context)
            .inflate(R.layout.$xml_view, parent, false)
        return ViewHolder(v)
    }

    //this method is binding the data on the list
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindItems(list[position])
        
       
    }
    //this method is giving the size of the list
    override fun getItemCount(): Int {
        return list.size
    }
    //the class is hodling the list view
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bindItems(item: $Model) {
           
            #end

        }
    }

}


Now, close the pop-up and click on File->new and check the list you find your created template in the list. For my case, I set the template name "RecyclerViewAdadpter". So I see this and click on this a pop-up is open.


After open the pop-up fill up all the field with appropriate text and simply click on OK. And you can see a ready adapter is created in your project. Now every time you can use this template to create recyclerview with different name, different xml view and different Object, Model class etc. For my case I just take "String",  you can take any object or model class, as you wish.


 

It is the adapter create using template



For better Understanding see the YouTube video below I mentioned-






If you think it is helpful, please share with your android developer community.



Comments

Post a Comment

Popular posts from this blog

How to Insert Multiple rows in a single db transaction in Android Room Database? | Android | Room DB

  To insert multiple rows into a Room database in Android, you can follow these steps: 1. Set up Room Database: First, make sure you have set up your Room database correctly in your Android project. Define your Entity class, create a Database class that extends RoomDatabase, and set up your DAO (Data Access Object) interface. 2. Create Entity Class: Define an Entity class that represents the data you want to insert into the database. For example: 1 2 3 4 5 6 7 8 9 @Entity (tableName = "my_table" ) public class MyEntity { @PrimaryKey (autoGenerate = true ) public int id; public String name; public int age; // Add other fields and getters/setters as needed } Create DAO: Create a DAO interface with a method to insert multiple rows. For example: 1 2 3 4 5 @Dao public interface MyEntityDao { @Insert void insertAll (List<MyEntity> entities); } Initialise Database and DAO: In your application code, create an insta

How to fetch Latitude, Longitude from address and vice-versa(address from Latitude, Longitude) using Google Geo coder SDK in android| Kotlin

 In this Android development related article, you will get a simple solution that, how to get address using Latitude, Longitude and vice-versa. i.e latitude, longitude from an address text. It is very easy and simple. Read full article and carefully follow all the steps. Here we use google Geocoder SDK. Okay, first we create an android project in kotlin and create an Activity say MainActivity.kt. Use the below code- Function get Latitude, Longitude from Address- fun getLatLngFromAddress (context: Context, mAddress: String): String { val coder = Geocoder(context) lateinit var address: List<Address> try { address = coder.getFromLocationName(mAddress, 5 ) if (address == null ) { return "Fail to find Lat,Lng" } val location = address[ 0 ] return " Latitude: ${location.latitude}\n Longitude: ${location.longitude}" } catch (e: Exception

Recycler View Like Google Play Store App | How to use SnapHelper Android, Kotlin

 If we see the google play store android app, we can see that the app list in horizontal recycler view hold a property that the first property hold always full visible or not visible, but a portion visible is not seeing. This property is snap property. In this article I show you how to use it in our own application. Basically we use two type of snap. Center Snap and start snap. I show you both here. So read full article here. The Key Moment code is here- For center snap you need to write 2 lines code- //center Snap val snapHelper = LinearSnapHelper() snapHelper.attachToRecyclerView(recyclerView1) For start snap, create a class in kotlin say StartSnapHelper.kt package com.example.snaphelpersampleapp import android.view.View import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearSnapHelper import androidx.recyclerview.widget.OrientationHelper import androidx.recyclerview.widget.RecyclerView class StartSnapHelpe