Monday, 16 April 2018

Data Class Example Using Kotlin



Data classes are a powerful kind of classes which avoid the boilerplate we need in Java to create POJO: classes which are used to keep state, but are very simple in the operations they do. They usually only provide plain getters and setters to access to
their fields. Defining a new data class is very easy:

data class Student(var name:String,var Roll no:Int,var Address:String)

1. Creating New Project in Kotlin
  • Open Android Studio.
  • Go to File => New => New Project.Give Name To your application. Then, check Include Kotlin Support and click next button.
  • Select minimum SDK you need. However, we have selected 21 as minimum SDK. Then, click next button.
  • Then, select Empty Activity => click next => click finish.
2. For Custom ListView We need two XML file main xml file contain listView and Other Contain a Single ROW which we want to Show in ListView.Here we Have Two XML file
  1.  main_activity.xml
  2.  list_row.xml
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>


list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp">


    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:padding="4dp"
        android:text="Name :"
        android:textColor="@android:color/black"
        android:textSize="22sp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_toEndOf="@+id/tvName"
        android:gravity="center"
        android:padding="4dp"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp" />

    <TextView
        android:id="@+id/tvId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/tvName"
        android:layout_marginTop="35dp"
        android:gravity="center"
        android:padding="4dp"
        android:text="Id :"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/name"
        android:layout_alignTop="@+id/tvId"
        android:gravity="center"
        android:padding="4dp"
        android:textColor="@color/colorPrimary"
        android:textSize="18sp" />

</RelativeLayout>


3. MainActivity.kt
package com.dharmendra.dataclass

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.ListView
import android.widget.Toast
import java.util.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Find View By Id For Listview
        val listView = findViewById<ListView>(R.id.listView)


        //Array That contain names which we add to our DataModel Class
        val names = arrayOf("Dharmendra", "Vishal", "Sachin", "Raj", "Jatin", "Krishank", "Rishu")

        //Initialize ArrayList of DataModel Class
        var list = ArrayList<DataModel>()


        //For loop that add name From names Array and index as Id in DataModel Class
        //Add object of DataModel in to ArrayList

        for (i in 0 until names.size) {

            val myData = DataModel(i, names[i])

            list.add(myData)

        }

        //We Have Created Custom Adapter Class in that we pass Context,ArrayList of DataModel
        val customAdapter = CustomAdapter(this, list)

        //Set Adapter to ArrayList
        listView.adapter = customAdapter

        //On Click for ListView Item
        listView.setOnItemClickListener { parent, view, position, id ->

            val name = customAdapter.getItem(position).name
            val id = customAdapter.getItem(position).id

            Toast.makeText(this, "Name:$name\n Id: $id", Toast.LENGTH_LONG).show()
        }


    }


}


//DataModel Class which take and store id and name
data class DataModel(var id: Int, var name: String)


4. Now We have to Create Class CustomAdapter.Right Click on Package name---->New--->Kotlin File/Class.


Now Select Class in Kind and Add name of Class.Here Our Class name is CustomAdapter.


5. After Class is Created add parameter to Class(passed in Main Activity) and extend with BaseAdapter()

Ex:
class CustomAdapter(context: Context,list: ArrayList<DataModel>) : BaseAdapter() {
..
..
..
}

You will get Some Error Put Your Cursor on Red line And Press Alt+Enter and click on Implement Members and Select All Methods and click Ok.

CustomAdapter.kt

package com.dharmendra.dataclass

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView


/*We Have Created Constructor of Custom Adapter and Pass
                context
                ArrayList<DataModel> which Contain our Data
                */

//Here We extend over Adapter With BaseAdapter()


class CustomAdapter(context: Context, list: ArrayList<DataModel>) : BaseAdapter() {


    //Passing Values to Local Variables

    private var list = list
    private var context = context


    //Auto Generated Method

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {

        var myView = convertView
        val holder: ViewHolder

        if (convertView == null) {


            //If Over View is Null than we Inflater view using Layout Inflater

            val mInflater = (context as Activity).layoutInflater

            //Inflating our list_row.

            myView = mInflater.inflate(R.layout.list_row, parent, false)

            //Create Object of ViewHolder Class and set our View to it

            holder = ViewHolder()

            //Find view By Id For all our Widget taken in list_row.

            /*Here !! are use for non-null asserted two prevent From null.
             you can also use Only Safe (?.)

            */


            holder.name = myView!!.findViewById(R.id.name)
            holder.id = myView.findViewById(R.id.id)

            //Set A Tag to Identify our view.

            myView.setTag(holder)


        } else {

            //If Ouer View in not Null than Just get View using Tag and pass to holder Object.

            holder = myView!!.getTag() as ViewHolder
        }


        //Getting our data at position of List
        val data = getItem(position)


        //Set Name to our TextView
        holder.name!!.text = data.name

        //Set Id to our TextView
        holder.id!!.text = data.id.toString()


        return myView
    }

    //Auto Generated Method

    override fun getItem(position: Int): DataModel {
        //Return the Data at Position of ArrayList.
        return list.get(position)
    }


    //Auto Generated Method

    override fun getItemId(position: Int): Long {
        return 0
    }

    //Auto Generated Method

    override fun getCount(): Int {

        //Return Size Of ArrayList
        return list.size
    }

    //Create A class To hold over View like we taken in list_row.xml
    class ViewHolder {

        var name: TextView? = null
        var id: TextView? = null

    }
}

Output:-

Sunday, 8 April 2018

Intent using Kotlin in Android



What is Intent?
An Intent is an object that provides runtime binding between separate components, such as two activities. The Intent represents an app’s "intent to do something." You can use intents for a wide variety of tasks.

Types of Intents:

1). Explicit Intents
Explicit intent going to be connected internal world of application,suppose if you wants to connect one activity to another activity, we can do this quote by explicit intent.
2). Implicit Intents
These intents do not name a target and the field for the component name is left blank. Implicit intents are often used to activate components in other applications.

Description: In this Intent Example we Go through both type of Intent.
1).Explicit Intent : We Open Different activity By Click on button which is on MainActivity.
If user Click on Explicit Button than we pass data from one Activity to another Activtiy.
If user Click on Implicit Button than we do not pass data only open new activity.
2).Implicit Intent : we open camera or map based on button click by user.
   To open Specific location on map we have to pass Latitude   and Longitude to map.We also need uses permission in Manifest file.

1. Creating New Project in Kotlin.
  • Open Android Studio.
  • Go to File => New => New Project.Give Name To your application. Then, check Include Kotlin Support and click next button.
  • Select minimum SDK you need. However, we have selected 21 as minimum SDK. Then, click next button.
  • Then, select Empty Activity => click next => click finish.
2.For this example we need 3 XML file.(res->layout->our Files)
  1.  activity_main
  2.  implicit_intent_activity
  3.  explicit_intent_activity

main_activity.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=".MainActivity">


    <Button
        android:id="@+id/implicit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="166dp"
        android:text="Implicit Intent" />

    <Button
        android:id="@+id/explicit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="202dp"
        android:text="Explicit Intent" />
</RelativeLayout>


implicit_intent_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="133dp"
        android:text="Open Camera" />

    <Button
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Open Map" />
</RelativeLayout>


explicit_intent_activity.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="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/number"
        android:padding="5dp"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


</LinearLayout>

3.Here we have 3 kotlin Class
  1. MainActivity
  2. ImplicitIntentActivity
  3. ExplicitIntentActivity

MainActivity.kt

package com.dharmendra.intentexample

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button

class MainActivity : AppCompatActivity(), View.OnClickListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Find View By Id for Both Button

        val implicitIntent = findViewById(R.id.implicit) as Button
        val explicitIntent = findViewById(R.id.explicit) as Button

        /* Set On Click Listerner for both button Here we have implement OnClickListerner
            On Click is Handle in onClick Method.
        */

        implicitIntent.setOnClickListener(this)
        explicitIntent.setOnClickListener(this)
    }

    override fun onClick(v: View?) {

        //Get selected Button Id
        val selectedButtonId = v!!.id

        //Initialize Intent
        val intent: Intent

        when (selectedButtonId) {

            R.id.implicit -> {

                //This is Explicit Intent Without Passing Data
                intent = Intent(this@MainActivity, ImplicitIntentActivity::class.java)

                //StartActivity Open our Intent Activity.
                startActivity(intent)
            }

            R.id.explicit -> {

                //This Is Explicit Intent With Passing Data to Another Activity.

                intent = Intent(this@MainActivity, ExplicitIntentActivity::class.java)

                //Pass String Value
                intent.putExtra("name", "Dharmendra")

                //Pass Integer value
                intent.putExtra("contact", 1234567890)


                //StartActivity Open our Intent Activity.
                startActivity(intent)
            }
        }

    }
}


ImplicitIntentActivity.kt

package com.dharmendra.intentexample


import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.Toast


class ImplicitIntentActivity : AppCompatActivity(), View.OnClickListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.implicit_intent_activity)

        //Find View By Id for both Button

        val camera = findViewById<Button>(R.id.camera)
        val map = findViewById<Button>(R.id.map)

        //Set on Click for both handle in onClick method

        camera.setOnClickListener(this)
        map.setOnClickListener(this)

    }

    override fun onClick(v: View?) {

        //Get selected Button Id
        val selectedButtonId = v!!.id

        //Initialize Intent
        val intent: Intent

        when (selectedButtonId) {
            R.id.map -> {

                //If Google Map in Not install Or any other Error occurs So we use try-catch block

                try {
                    intent = Intent(Intent.ACTION_VIEW)

                    //We need to pass Latitude And Longitude to open Location On Map
                    intent.data = Uri.parse("geo:20.379146,72.913831")
                    startActivity(intent)
                } catch (error: Exception) {

                    Toast.makeText(this, error.message, Toast.LENGTH_LONG).show()

                }

            }
            R.id.camera -> {

                //If Camera in Not install Or any other Error occurs So we use try-catch block
                try {
                    intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                    startActivity(intent)
                } catch (error: Exception) {

                    Toast.makeText(this, error.message, Toast.LENGTH_LONG).show()

                }


            }
        }
    }
}


ExplicitIntentActivity.kt

package com.dharmendra.intentexample

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.TextView

class ExplicitIntentActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.explicit_intent_activity)

        //Find View By Id for Our textView

        val name = findViewById<TextView>(R.id.name)
        val number = findViewById<TextView>(R.id.number)

        //Create Object to get Intent Data
        val i = intent.extras

        //get name Pass from MainActivity
        val myname = i.getString("name")


        //get Contact Number Pass from MainActivity
        val myContact = i.getInt("contact")

        /* You can set Text in TextView in both way as Shown Below*/
        name.setText(myname)

        number.text = myContact.toString()


    }

}

4.we have to take Internet Permission and also register Our classes in AndroidManifest file.


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dharmendra.intentexample">

    <uses-permission android:name="android.permission.INTERNET" />

    <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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".ExplicitIntentActivity" />
        <activity android:name=".ImplicitIntentActivity" />
    </application>

</manifest>

Output:




Wednesday, 4 April 2018

SearchView in ListView with a custom Adapter using Kotlin in Android


1. Creating New Project in Kotlin
  • Open Android Studio.
  • Go to File => New => New Project.Give Name To your application. Then, check Include Kotlin Support and click next button.
  • Select minimum SDK you need. However, we have selected 21 as minimum SDK. Then, click next button.
  • Then, select Empty Activity => click next => click finish.
2. For Search in Custom ListView We need two XML file main xml file contain listView and SearchView.
Other Contain a Single ROW which we want to Show in ListView.Here we Have Two XML file
  1.  main_activity.xml
  2.  list_row.xml
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>


list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="16dp"
        android:layout_marginTop="12dp" />

    <TextView
        android:id="@+id/header"
        android:textSize="16sp"
        android:textColor="@color/colorPrimary"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageview"
        android:layout_marginStart="18dp"
        android:layout_marginTop="13dp"
        android:layout_toEndOf="@+id/imageview" />

    <TextView
        android:id="@+id/subHeader"
        android:textSize="12sp"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/header"
        android:layout_below="@+id/header" />

</RelativeLayout>


We add some Image to res/drawable to display in listView.












  




3. MainActivity.kt


package com.dharmendra.searchview

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.ListView
import android.widget.SearchView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Find View By Id For Listview
        val listview = findViewById(R.id.listView) as ListView

        //Find View By Id For SearchView
        val searchView = findViewById(R.id.searchView) as SearchView

        /*Create and ArrayList of Integer Type To Store Images From drawable.Here we add Images to ArrayList.
        We have Images of Android Icons of Diffrent versions.
        */
        val image = ArrayList<Int>()
        image.add(R.drawable.cupcake)
        image.add(R.drawable.donut)
        image.add(R.drawable.eclair)
        image.add(R.drawable.froyo)
        image.add(R.drawable.gingerbread)
        image.add(R.drawable.honeycomb)
        image.add(R.drawable.icecreamsandwich)
        image.add(R.drawable.jellybean)
        image.add(R.drawable.kitkat)
        image.add(R.drawable.lollipop)
        image.add(R.drawable.marshmallow)
        image.add(R.drawable.nougat)
        image.add(R.drawable.oreo)


        // Here We take and Array of Android OS names in Same Sequence as we take Images.

        val name = arrayOf("Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Icecreamsandwich",
                "Jellybean", "Kitkat", "Lollipop", "Marshmallow", "Nougat", "Oreo")


        // Here We take and Array of Android OS Version in Same Sequence as we take Images and name.

        val version = arrayOf("1.5", "1.6", "2.0", "2.2", "2.3", "3.0", "4.0",
                "4.1", "4.4", "5.0", "6.0", "7.0", "8.0")


        /*Create ArrayList of HashMap to Store Name and Version with Key value Pair at Same poition

        Ex:-
                At Position 1:
                                name:"Cupcake"
                                version:"1.5"
                At Position 2:
                                name:"Donut"
                                version:"1.6"
                                .
                                .
                                .
                                So On
        */
        val info = ArrayList<HashMap<String, String>>()

        //Here We take HashMap in that we add Name and Version from Array
        var hashMap: HashMap<String, String> = HashMap<String, String>()

        for (i in 0..name.size - 1) {
            hashMap = HashMap<String, String>()
            hashMap.put("name", name[i])
            hashMap.put("version", version[i])

            //Add HashMap to ArrayList
            info.add(hashMap)

            /*
            ArrayList Start with Position 0

             So we have At position 0:

                                name:"Cupcake"
                                version:"1.5"

            */

        }

        //We Have Created Custom Adapter Class in that we pass Context,Array of Image and ArrayList<Hashmap<String,String>>
        val customAdapter = CustomAdapter(this, image, info)


        //Set Adapter to ArrayList
        listview.adapter = customAdapter

        //On Click for ListView Item
        listview.setOnItemClickListener { adapterView, view, position, l ->

            //Provide the data on Click position in our listview
            val hashMap: HashMap<String, String> = customAdapter.getItem(position) as HashMap<String, String>

            Toast.makeText(this@MainActivity, "Name : " + hashMap.get("name") + "\nVersion : " + hashMap.get("version"), Toast.LENGTH_LONG).show()
        }

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {

                val text = newText
                /*Call filter Method Created in Custom Adapter
                    This Method Filter ListView According to Search Keyword
                 */
                customAdapter.filter(text)
                return false
            }
        })

    }

}


4. Now We have to Create Class CustomAdapter.
Right Click on Package name---->New--->Kotlin File/Class.


Now Select Class in Kind and Add name of Class.Here Our Class name is CustomAdapter.



5. After Class is Created add parameter to Class(passed in Main Activity) and extend with BaseAdapter()

Ex:
class CustomAdapter(context: Context, image: ArrayList<Int>, arrayList: ArrayList<HashMap<String, String>>) : BaseAdapter() {
..
..
..
}

You will get Some Error Put Your Cursor on Red line And Press Alt+Enter and click on Implement Members and Select All Methods and click Ok.

CustomAdapter.kt


package com.dharmendra.searchview

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import java.util.*
import kotlin.collections.ArrayList

/*We Have Created Constructor of Custom Adapter and Pass
                context
                ArrayList<Int> which Contain images
                ArrayList<HashMap<String,String>> which contain name and version*/

//Here We extend over Adapter With BaseAdapter()

class CustomAdapter(context: Context, image: ArrayList<Int>, arrayList: ArrayList<HashMap<String, String>>) : BaseAdapter() {

    //Passing Values to Local Variables
    var image = image
    var arrayList = arrayList
    var context = context

    //Store image and arraylist in Temp Array List we Required it later
    var tempArrayList = ArrayList(image)
    var tempNameVersionList = ArrayList(arrayList)


    //Auto Generated Method
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        var myview = convertView
        val holder: ViewHolder



        if (convertView == null) {

            //If Over View is Null than we Inflater view using Layout Inflater
            val mInflater = (context as Activity).layoutInflater

            //Inflating our list_row.
            myview = mInflater!!.inflate(R.layout.list_row, parent, false)

            //Create Object of ViewHolder Class and set our View to it
            holder = ViewHolder()

            //Find view By Id For all our Widget taken in list_row.

            /*Here !! are use for non-null asserted two prevent From null.
             you can also use Only Safe (?.)

            */
            holder.mImageView = myview!!.findViewById<ImageView>(R.id.imageview) as ImageView
            holder.mHeader = myview!!.findViewById<TextView>(R.id.header) as TextView
            holder.mSubHeader = myview!!.findViewById<TextView>(R.id.subHeader) as TextView

            //Set A Tag to Identify our view.
            myview.setTag(holder)
        } else {

            //If Ouer View in not Null than Just get View using Tag and pass to holder Object.
            holder = myview!!.getTag() as ViewHolder
        }

        //Getting HasaMap At Perticular Position
        val map = arrayList.get(position)

        //Setting Image to ImageView by position
        holder.mImageView!!.setImageResource(image[position])

        //Setting name to TextView it's Key from HashMap At Position
        holder.mHeader!!.setText(map.get("name"))

        //Setting version to TextView it's Key from HashMap At Position
        holder.mSubHeader!!.setText(map.get("version"))


        return myview

    }

    //Auto Generated Method
    override fun getItem(p0: Int): Any {

        //Return the Current Position of ArrayList.
        return arrayList.get(p0)

    }

    //Auto Generated Method
    override fun getItemId(p0: Int): Long {
        return 0
    }

    //Auto Generated Method

    override fun getCount(): Int {

        //Return Size Of ArrayList
        return arrayList.size
    }


    //Create A class To hold over View like we taken in list_row.xml
    class ViewHolder {

        var mImageView: ImageView? = null
        var mHeader: TextView? = null
        var mSubHeader: TextView? = null
    }


    //Function to set data according to Search Keyword in ListView
    fun filter(text: String?) {


        //Our Search text
        val text = text!!.toLowerCase(Locale.getDefault())


        //Here We Clear Both ArrayList because We update according to Search query.
        image.clear()
        arrayList.clear()


        if (text.length == 0) {

            /*If Search query is Empty than we add all temp data into our main ArrayList

            We store Value in temp in Starting of Program.

            */

            image.addAll(tempArrayList)
            arrayList.addAll(tempNameVersionList)


        } else {


            for (i in 0..tempNameVersionList.size - 1) {

                /*
                If our Search query is not empty than we Check Our search keyword in Temp ArrayList.
                if our Search Keyword in Temp ArrayList than we add to our Main ArrayList
                */

                if (tempNameVersionList.get(i).get("name")!!.toLowerCase(Locale.getDefault()).contains(text)) {

                    image.add(tempArrayList.get(i))
                    arrayList.add(tempNameVersionList.get(i))


                }

            }
        }

        //This is to notify that data change in Adapter and Reflect the changes.
        notifyDataSetChanged()


    }


}


Output: