Android Mobile Food Ordering App For Restaurant – Project Idea Source Code

This is part of the android student’s project idea. I have received many request on Android Mobile Food Ordering App for Restaurant from readers on how to create it in android.

This is not a complete project for anybody working on a related topics but rather it is for learning purpose. By going through the source code you can learn how to implement a feature(s) you might need in your project.

This project will give you an insight on how to create android food ordering application and you can use the source code as a foundation to achieve your project requirement.

The application comes with different features. You can easily customize it the way you want.

This project comes with source code, documentation, Database Schema, Use Case Diagram and Web admin panel in Php and Mysql.

BUY NOW

 

I also have custom android and iOS applications I have develop that will be of help to students planning their thesis, dissertation or course project in mobile related fields.

Below are some of the screen-shots associated with this project.

Login and Registration Section

shot1 shot2

Menu Category and Menu Items

menusection

Order History, Favorite and Hot Deals

sectiontwo

Cart, Checkout, Payment and Order Confirmation

payment1 payment2

Database EER Diagram

dbdesign

Sample Code Snippet from the project

codes

Server Side Web Admin Panel

panel

 

APK File for testing purposes.

Please note that the latest changes might not reflect in the apk file. Kindly ask me any question with respect to that.

 

Package Includes

1 Android Java Source code

2. Web Admin Panel (Php and MYSQL)

3. Database file (.sql file)

4. Documentation

 

Link to documentation and setup questions

 

Promotion Price : $10

 

If you do not have any experience with android or Slim 3 Php framework then this source code is not for you. But if you still need to and will like me to set it up for you then you can contain for the setup price.

How to filter a RecyclerView with a SearchView in Kotlin

In this android kotlin source code example, we are going to filter a RecyclerView with a SearchView in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to filter a RecyclerView with a SearchView in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".recyclerView.RecyclerViewActivity15">

    <EditText
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:hint="Search" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/search"/>


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import android.widget.EditText
import com.bluapp.kotlinview.R
import android.widget.TextView
import android.view.LayoutInflater
import android.view.ViewGroup
import android.content.Context;
import android.view.View;
import android.widget.Filter;
import android.widget.Filterable;
import android.text.Editable
import android.text.TextWatcher
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager


class RecyclerViewActivity15 : AppCompatActivity() {
    private var list: RecyclerView? = null
    private var search: EditText? = null
    private var recyclerAdapter: adapter? = null
    var country: ArrayList<String>? = null
    var listcountry: ArrayList<String>? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view15)
        list = findViewById(R.id.list) as RecyclerView
        search = findViewById(R.id.search) as EditText
        listcountry = getListCountry()
        country = ArrayList()
        country!!.addAll(listcountry!!)
        val layoutManager = LinearLayoutManager(this)
        list!!.setLayoutManager(layoutManager)
        recyclerAdapter = adapter(this@RecyclerViewActivity15, country!!)
        list!!.addItemDecoration(DividerItemDecoration(list!!.getContext(), layoutManager.orientation))
        list!!.setAdapter(recyclerAdapter)


        search!!.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                recyclerAdapter!!.getFilter().filter(charSequence.toString())
            }

            override fun afterTextChanged(editable: Editable) {}
        })
    }

    private inner class adapter(internal var context: Context, internal var mData: List<String>) : RecyclerView.Adapter<adapter.myViewHolder>(), Filterable {
        internal var mfilter: NewFilter

        override fun getFilter(): Filter {
            return mfilter
        }

        init {
            mfilter = NewFilter(this@adapter)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): adapter.myViewHolder {
            val view =
                LayoutInflater.from(context).inflate(R.layout.recyclerview_adapter1, parent, false)
            return myViewHolder(view)
        }

        override fun onBindViewHolder(holder: adapter.myViewHolder, position: Int) {
            holder.country.text = mData[position]
        }

        override fun getItemCount(): Int {
            return mData.size
        }

        inner class myViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            internal var country: TextView

            init {
                country = itemView.findViewById(R.id.country)
            }
        }

        inner class NewFilter(var mAdapter: adapter) : Filter() {
            override fun performFiltering(charSequence: CharSequence): FilterResults {
                country!!.clear()
                val results = FilterResults()
                if (charSequence.length == 0) {
                    country!!.addAll(listcountry!!)
                } else {
                    val filterPattern = charSequence.toString().toLowerCase().trim { it <= ' ' }
                    for (listcountry in listcountry!!) {
                        if (listcountry.toLowerCase().startsWith(filterPattern)) {
                            country!!.add(listcountry)
                        }
                    }
                }
                results.values = country
                results.count = country!!.size
                return results
            }

            override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) {
                this.mAdapter.notifyDataSetChanged()
            }

        }
    }

    fun getListCountry(): ArrayList<String> {
        val listcountry = arrayListOf<String>()
        listcountry.add("Nigeria")
        listcountry.add("China")
        listcountry.add("USA")
        listcountry.add("Ghana")
        listcountry.add("Canada")
        listcountry.add("Finland")
        listcountry.add("Denmark")
        listcountry.add("Argentina")
        listcountry.add("Andorra")
        listcountry.add("Togo")
        return listcountry
    }

}

recyclerview_adapter1.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="wrap_content"
    android:padding="10dp">

    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Country"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to create RecyclerView with Header in Kotlin

In this android kotlin source code example, we are going to create RecyclerView with Header in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to create RecyclerView with Header in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".recyclerView.RecyclerViewActivity16">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.bluapp.kotlinview.R
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager


class RecyclerViewActivity16 : AppCompatActivity() {
    private var list: RecyclerView? = null
    private val TYPE_HEADER: Int = 0
    private val TYPE_ITEM: Int = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view16)
        list = findViewById(R.id.list) as RecyclerView
        val linearLayoutManager = LinearLayoutManager(this@RecyclerViewActivity16)
        list!!.setLayoutManager(linearLayoutManager)
        list!!.setHasFixedSize(true)
        val customAdapter = CustomRecyclerViewAdapter(getDataSource())
        list!!.setAdapter(customAdapter)
    }

    inner class CustomRecyclerViewAdapter(private val itemObjects: List<ItemObject>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            if (viewType == TYPE_HEADER) {
                val layoutView = LayoutInflater.from(parent.context).inflate(R.layout.header_layout, parent, false)
                return HeaderViewHolder(layoutView)
            } else if (viewType == TYPE_ITEM) {
                val layoutView = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
                return ItemViewHolder(layoutView)
            }
            throw RuntimeException("No match for $viewType.")
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val mObject = itemObjects[position]
            if (holder is HeaderViewHolder) {
                (holder as HeaderViewHolder).headerTitle.setText(mObject.contents)
            } else if (holder is ItemViewHolder) {
                (holder as ItemViewHolder).itemContent.setText(mObject.contents)
            }
        }

        private fun getItem(position: Int): ItemObject {
            return itemObjects[position]
        }

        override fun getItemCount(): Int {
            return itemObjects.size
        }

        override fun getItemViewType(position: Int): Int {
            return if (isPositionHeader(position)) TYPE_HEADER else TYPE_ITEM
        }

        private fun isPositionHeader(position: Int): Boolean {
            return position == 0
        }
    }

    inner class HeaderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var headerTitle: TextView
        init {
            headerTitle = itemView.findViewById(R.id.header)
        }
    }

    inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var itemContent: TextView
        init {
            itemContent = itemView.findViewById(R.id.itemcontent) as TextView
        }
    }

    inner class ItemObject(val contents: String)

    private fun getDataSource(): List<ItemObject> {
        val data = ArrayList<ItemObject>()
        data.add(ItemObject("Country"))
        data.add(ItemObject("Nigeria"))
        data.add(ItemObject("Finland"))
        data.add(ItemObject("Denmark"))
        data.add(ItemObject("Argentina"))
        data.add(ItemObject("Andorra"))
        return data
    }
}

header_layout.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="wrap_content"
    android:padding="16sp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#000000"
        android:text="Header"/>
</LinearLayout>

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        card_view:cardElevation="2dp"
        card_view:cardUseCompatPadding="true">

        <TextView
            android:id="@+id/itemcontent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp"
            android:gravity="center"
            android:text="Item" />

    </androidx.cardview.widget.CardView>

</LinearLayout>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to create empty RecyclerView adapter in Kotlin

In this android kotlin source code example, we are going to create empty RecyclerView adapter in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to create empty RecyclerView adapter in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".recyclerView.RecyclerViewActivity17">

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

    <TextView
        android:id="@+id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data is empty"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/colorAccent"
        android:visibility="gone"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import android.widget.TextView
import com.bluapp.kotlinview.R
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.ViewGroup
import android.content.Context


class RecyclerViewActivity17 : AppCompatActivity() {
    private var list: RecyclerView? = null
    private var emptyData: TextView? = null
    private var recyclerAdapter: adapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view17)
        list = findViewById(R.id.list) as RecyclerView
        emptyData = findViewById(R.id.empty) as TextView
        val country = arrayListOf<String>()

        if (country.isEmpty()) {
            list!!.setVisibility(View.GONE)
            emptyData!!.setVisibility(View.VISIBLE)
        } else {
            emptyData!!.setVisibility(View.GONE)
            list!!.setVisibility(View.VISIBLE)
        }
        val layoutManager = LinearLayoutManager(this)
        list!!.setLayoutManager(layoutManager)
        recyclerAdapter = adapter(this@RecyclerViewActivity17, country)
        list!!.addItemDecoration(DividerItemDecoration(list!!.getContext(), layoutManager.orientation))
        list!!.setAdapter(recyclerAdapter)
    }

    private inner class adapter(internal var context: Context, internal var mData: List<String>) : RecyclerView.Adapter<adapter.myViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): adapter.myViewHolder {
            val view =
                LayoutInflater.from(context).inflate(R.layout.recyclerview_adapter1, parent, false)
            return myViewHolder(view)
        }
        override fun onBindViewHolder(holder: adapter.myViewHolder, position: Int) {
            holder.country.text = mData[position]
        }
        override fun getItemCount(): Int {
            return mData.size
        }
        inner class myViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            internal var country: TextView
            init {
                country = itemView.findViewById(R.id.country) as TextView
            }
        }
    }

}

recyclerview_adapter1.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="wrap_content"
    android:padding="10dp">

    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Country"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to disable RecyclerView scrolling in Kotlin

In this android kotlin source code example, we are going to disable RecyclerView scrolling in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to disable RecyclerView scrolling in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".recyclerView.RecyclerViewActivity19">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import com.bluapp.kotlinview.R
import android.view.MotionEvent
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import android.widget.TextView
import android.view.LayoutInflater
import android.view.ViewGroup
import android.content.Context
import android.view.View


class RecyclerViewActivity19 : AppCompatActivity() {
    private var list: RecyclerView? = null
    private var recyclerAdapter: adapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view19)
        list = findViewById(R.id.list) as RecyclerView
        val country = arrayListOf<String>()
        country.add("Nigeria");
        country.add("China");
        country.add("USA");
        country.add("Ghana");
        country.add("Canada");
        country.add("Finland");
        country.add("Denmark");
        country.add("Argentina");
        country.add("Andorra");
        country.add("Togo");
        country.add("Nigeria");
        country.add("China");
        country.add("USA");
        country.add("Ghana");
        country.add("Canada");
        country.add("Finland");
        country.add("Denmark");
        country.add("Argentina");
        country.add("Andorra");
        country.add("Togo");
        val layoutManager = LinearLayoutManager(this)
        list!!.setLayoutManager(layoutManager)
        recyclerAdapter = adapter(this@RecyclerViewActivity19, country)
        list!!.addItemDecoration(DividerItemDecoration(list!!.getContext(), layoutManager.orientation))
        list!!.setAdapter(recyclerAdapter)
        list!!.setOnTouchListener(object : View.OnTouchListener{
            override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
                return true
            }
        })
    }

    private inner class adapter(internal var context: Context, internal var mData: List<String>) : RecyclerView.Adapter<adapter.myViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): adapter.myViewHolder {
            val view =
                LayoutInflater.from(context).inflate(R.layout.recyclerview_adapter1, parent, false)
            return myViewHolder(view)
        }
        override fun onBindViewHolder(holder: adapter.myViewHolder, position: Int) {
            holder.country.text = mData[position]
        }
        override fun getItemCount(): Int {
            return mData.size
        }
        inner class myViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            internal var country: TextView
            init {
                country = itemView.findViewById(R.id.country)
            }
        }
    }

}

recyclerview_adapter1.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="wrap_content"
    android:padding="10dp">

    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Country"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to get a RecyclerView item at a given position in Kotlin

In this android kotlin source code example, we are going to get a RecyclerView item at a given position in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to get a RecyclerView item at a given position in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".recyclerView.RecyclerViewActivity18">

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

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get Item"
        android:textColor="@color/white"
        android:background="@color/colorAccent"
        android:onClick="getitem"
        app:layout_constraintBottom_toBottomOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import com.bluapp.kotlinview.R
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import android.widget.TextView
import android.view.LayoutInflater
import android.view.ViewGroup
import android.content.Context
import android.view.View


class RecyclerViewActivity18 : AppCompatActivity() {
    private var list: RecyclerView? = null
    private var recyclerAdapter: adapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view18)
        list = findViewById(R.id.list) as RecyclerView
        val country = arrayListOf<String>()
        country.add("Nigeria")
        country.add("China")
        country.add("USA")
        country.add("Ghana")
        country.add("Canada")
        country.add("Finland")
        country.add("Denmark")
        country.add("Argentina")
        country.add("Andorra")
        country.add("Togo")
        val layoutManager = LinearLayoutManager(this)
        list!!.setLayoutManager(layoutManager)
        recyclerAdapter = adapter(this@RecyclerViewActivity18, country)
        list!!.addItemDecoration(DividerItemDecoration(list!!.getContext(), layoutManager.orientation))
        list!!.setAdapter(recyclerAdapter)
    }

    fun getitem(view: View) {
        val view2 = list!!.getLayoutManager()!!.findViewByPosition(2)
        val textView = view2!!.findViewById(R.id.country) as TextView
        textView.text = "Usa has been change to Nigeria"
    }

    private inner class adapter(internal var context: Context, internal var mData: List<String>) : RecyclerView.Adapter<adapter.myViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): adapter.myViewHolder {
            val view =
                LayoutInflater.from(context).inflate(R.layout.recyclerview_adapter1, parent, false)
            return myViewHolder(view)
        }

        override fun onBindViewHolder(holder: adapter.myViewHolder, position: Int) {
            holder.country.text = mData[position]
        }

        override fun getItemCount(): Int {
            return mData.size
        }

        inner class myViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            internal var country: TextView
            init {
                country = itemView.findViewById(R.id.country)
            }
        }
    }
}

recyclerview_adapter1.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="wrap_content"
    android:padding="10dp">

    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Country"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to add Recyclerview GridLayoutManager in Kotlin

In this android kotlin source code example, we are going to add Recyclerview GridLayoutManager in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to add Recyclerview GridLayoutManager in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".recyclerView.RecyclerViewActivity7">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import com.bluapp.kotlinview.R
import androidx.recyclerview.widget.GridLayoutManager
import android.widget.TextView
import android.view.LayoutInflater
import android.view.ViewGroup
import android.content.Context
import android.graphics.Rect
import android.view.View


class RecyclerViewActivity7 : AppCompatActivity() {
    private var list: RecyclerView? = null
    private var recyclerAdapter: adapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view7)
        list = findViewById(R.id.list) as RecyclerView
        val country = arrayListOf<String>()
        country.add("Nigeria")
        country.add("China")
        country.add("USA")
        country.add("Ghana")
        country.add("Canada")
        country.add("Finland")
        country.add("Denmark")
        country.add("Argentina")
        country.add("Andorra")
        country.add("Togo")

        val layoutManager = GridLayoutManager(this, 3)
        list!!.setLayoutManager(layoutManager)
        list!!.addItemDecoration(SpaceGrid(3, 10, true))
        recyclerAdapter = adapter(this@RecyclerViewActivity7, country)
        list!!.setAdapter(recyclerAdapter)
    }

    private inner class SpaceGrid(private val mSpanCount: Int, private val mSpacing: Int, private val mIncludeEdge: Boolean) : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
            val position = parent.getChildAdapterPosition(view)
            val column = position % mSpanCount
            if (mIncludeEdge) {
                outRect.left = mSpacing - column * mSpacing / mSpanCount
                outRect.right = (column + 1) * mSpacing / mSpanCount
                if (position < mSpanCount) {
                    outRect.top = mSpacing
                }
                outRect.bottom = mSpacing
            } else {
                outRect.left = column * mSpacing / mSpanCount
                outRect.right = mSpacing - (column + 1) * mSpacing / mSpanCount
                if (position < mSpanCount) {
                    outRect.top = mSpacing
                }
            }
        }
    }

    private inner class adapter(internal var context: Context, internal var mData: List<String>) :
        RecyclerView.Adapter<adapter.myViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): adapter.myViewHolder {
            val view = LayoutInflater.from(context).inflate(R.layout.recyclerview_adapter3, parent, false)
            return myViewHolder(view)
        }
        override fun onBindViewHolder(holder: adapter.myViewHolder, position: Int) {
            holder.country.text = mData[position]
        }

        override fun getItemCount(): Int {
            return mData.size
        }

        inner class myViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            internal var country: TextView
            init {
                country = itemView.findViewById(R.id.country)
            }
        }
    }

}

recyclerview_adapter3.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"
    app:cardCornerRadius="5dp"
    android:layout_margin="3dp"
    android:layout_width="100dp"
    android:layout_height="100dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:background="@color/colorAccent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/country"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Nigeria"
                android:layout_centerInParent="true"
                android:textSize="18sp"
                android:textStyle="bold"
                android:textColor="#ffffff" />

        </RelativeLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to change background color of selected RecyclerView item in Kotlin

In this android kotlin source code example, we are going to change background color of selected RecyclerView item in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to change background color of selected RecyclerView item in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".recyclerView.RecyclerViewActivity20">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import com.bluapp.kotlinview.R
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import android.widget.TextView
import android.widget.RelativeLayout
import android.view.LayoutInflater
import android.view.ViewGroup
import android.content.Context
import android.graphics.Color
import android.view.View


class RecyclerViewActivity20 : AppCompatActivity() {
    private var list: RecyclerView? = null
    private var recyclerAdapter: adapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view20)
        list = findViewById(R.id.list) as RecyclerView
        val country = arrayListOf<String>()
        country.add("Nigeria")
        country.add("China")
        country.add("USA")
        country.add("Ghana")
        country.add("Canada")
        country.add("Finland")
        country.add("Denmark")
        country.add("Argentina")
        country.add("Andorra")
        country.add("Togo")
        val layoutManager = LinearLayoutManager(this)
        list!!.setLayoutManager(layoutManager)
        recyclerAdapter = adapter(this@RecyclerViewActivity20, country)
        list!!.addItemDecoration(DividerItemDecoration(list!!.getContext(), layoutManager.orientation))
        list!!.setAdapter(recyclerAdapter)
    }

    private inner class adapter(internal var context: Context, internal var mData: List<String>) :
        RecyclerView.Adapter<adapter.myViewHolder>() {
        internal var index = -1

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): adapter.myViewHolder {
            val view =
                LayoutInflater.from(context).inflate(R.layout.recyclerview_adapter20, parent, false)
            return myViewHolder(view)
        }

        override fun onBindViewHolder(holder: adapter.myViewHolder, position: Int) {
            holder.country.text = mData[position]
            holder.relativeLayout.setOnClickListener {
                index = position
                notifyDataSetChanged()
            }

            if (index == position) {
                holder.relativeLayout.setBackgroundColor(Color.parseColor("#FF4081"))
                holder.country.setTextColor(Color.parseColor("#FFFFFF"))
            } else {
                holder.relativeLayout.setBackgroundColor(Color.parseColor("#FFFFFF"))
                holder.country.setTextColor(Color.parseColor("#000000"))
            }
        }

        override fun getItemCount(): Int {
            return mData.size
        }

        inner class myViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            internal var relativeLayout: RelativeLayout
            internal var country: TextView

            init {
                relativeLayout = itemView.findViewById(R.id.relativeLayout)
                country = itemView.findViewById(R.id.country)
            }
        }
    }

}

recyclerview_adapter20.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="10dp"
        android:layout_margin="10dp"
        android:text="Country"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to populate a Spinner with Room database in Kotlin

In this android kotlin source code example, we are going to populate a Spinner with Room database in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to populate a Spinner with Room database in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:padding="24dp"
    tools:context=".spinner.SpinnerActivity14">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Spinner
import com.bluapp.kotlinview.R

class SpinnerActivity14 : AppCompatActivity() {
    private var spinnerDatabase: SpinnerDatabase? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_spinner14)
        val spinner = findViewById(R.id.spinner) as Spinner
        spinnerDatabase = SpinnerDatabase.getInstance(this)
        val usernameSpinner = Spinner14(id = null, username = "Clark")
        spinnerDatabase!!.spinnerDAO().insert(usernameSpinner)
        val username = spinnerDatabase!!.spinnerDAO().allSpinner()
        val item = arrayOfNulls<String>(username.size)
        for(i in 0 until username.size){
            item[i] = username.get(i).username
        }
        val arrayadapter = ArrayAdapter(this@SpinnerActivity14, android.R.layout.simple_spinner_item, item)
        spinner.adapter = arrayadapter
    }
}

SpinnerDatabase.java

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = arrayOf(Spinner14::class), version = 1, exportSchema = false)
abstract class SpinnerDatabase : RoomDatabase() {
    abstract fun spinnerDAO(): SpinnerDAO

    companion object {
        private var instance: SpinnerDatabase? = null

        fun getInstance(context: Context): SpinnerDatabase {
            if (instance == null) {
                instance =
                    Room.databaseBuilder(context, SpinnerDatabase::class.java, "spinnerdb")
                        .fallbackToDestructiveMigration()
                        .allowMainThreadQueries()
                        .build()
            }
            return instance as SpinnerDatabase
        }

        fun destroyInstance() {
            instance = null
        }
    }
}

SpinnerDAO.java

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy.REPLACE
import androidx.room.Query

@Dao
interface SpinnerDAO {

    @Query("select * from spinner")
    fun allSpinner(): List<Spinner14>

    @Insert(onConflict = REPLACE)
    fun insert(spinner14: Spinner14)
}

Spinner14.java

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "spinner")
data class Spinner14(
    @PrimaryKey
    var id: Long?,
    @ColumnInfo(name = "username")
    var username: String
)

app/build.gradle

implementation 'androidx.room:room-runtime:2.2.1'
kapt 'androidx.room:room-compiler:2.2.1'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to populate Spinner with a List of String in Kotlin

In this android kotlin source code example, we are going to populate Spinner with a List of String in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to populate Spinner with a List of String in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:padding="24dp"
    tools:context=".spinner.SpinnerActivity13">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Spinner
import com.bluapp.kotlinview.R
import java.util.ArrayList

class SpinnerActivity13 : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_spinner13)
        val spinner = findViewById(R.id.spinner) as Spinner
        val arrayadapter = ArrayAdapter(this@SpinnerActivity13, android.R.layout.simple_spinner_item, getUserNames())
        spinner.adapter = arrayadapter
    }

    fun getUserNames(): List<String> {
        val userNames = ArrayList<String>()
        userNames.add("Henry")
        userNames.add("Mark")
        userNames.add("Jack")
        userNames.add("Smith")
        userNames.add("Clark")
        userNames.add("Kelvin")
        userNames.add("Denzel")
        return userNames
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to populate Spinner directly from xml layout in Kotlin

In this android kotlin source code example, we are going to populate Spinner directly from xml layout.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to populate Spinner directly from xml layout.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:padding="24dp"
    tools:context=".spinner.SpinnerActivity12">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/user_name"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

values/strings.xml

<string-array name="user_name">
        <item>Henry</item>
        <item>Mark</item>
        <item>Jack</item>
        <item>Smith</item>
        <item>Clark</item>
        <item>Kelvin</item>
        <item>Denzel</item>
    </string-array>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.bluapp.kotlinview.R

class SpinnerActivity12 : AppCompatActivity() {

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

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.