MainActivity.kt
package com.cfsuman.kotlintutorials
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
val textView = findViewById<TextView>(R.id.textView)
val btnList = findViewById<Button>(R.id.btnList)
val btnFilter = findViewById<Button>(R.id.btnFilter)
val btnFilter2 = findViewById<Button>(R.id.btnFilter2)
// make textview content scrollable
textView.movementMethod = ScrollingMovementMethod()
val list = generateList()
btnList.setOnClickListener {
textView.text = "Employees:\n"
// Iterate through the list
list.forEach {
textView.append("\n${it.name} : age ${it.age} " +
": salary $${it.salary}")
}
}
btnFilter.setOnClickListener {
val filteredList = list.filter { it.salary>3000 }
textView.text = "Employees: salary>3000\n"
// Iterate through the filtered list
filteredList.forEach {
textView.append("\n${it.name} : age ${it.age} " +
": salary $${it.salary}")
}
}
btnFilter2.setOnClickListener {
val filteredList = list.filter {
it.salary>=2500 && it.age<=30 }
textView.text = "Employees: salary>=2500 and age<=30\n"
// Iterate through the filtered list
filteredList.forEach {
textView.append("\n${it.name} : age ${it.age} " +
": salary $${it.salary}")
}
}
}
}
// Method to generate employee list
private fun generateList():MutableList<Employee>{
val list = mutableListOf<Employee>()
list.add(Employee("Selina",30,2500))
list.add(Employee("Madyson",22,2600))
list.add(Employee("Mable",25,3750))
list.add(Employee("Elenora",42,4800))
list.add(Employee("Jovanny",49,5599))
list.add(Employee("Charlotte",50,4200))
list.add(Employee("Robbie",36,2875))
list.add(Employee("Erika",29,1650))
list.add(Employee("Esta",27,1890))
return list
}
// Data class for Employee
data class Employee(
val name:String,
val age:Int,
val salary:Int
)
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:background="#F8F8F8"
android:padding="24dp">
<Button
android:id="@+id/btnList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All"
android:textAllCaps="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnFilter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Filter Sal>3000"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="@+id/btnList"
app:layout_constraintStart_toEndOf="@+id/btnList" />
<Button
android:id="@+id/btnFilter2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Filter 2"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="@+id/btnFilter"
app:layout_constraintStart_toEndOf="@+id/btnFilter" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnList" />
</androidx.constraintlayout.widget.ConstraintLayout>



- kotlin - Notification progress bar percentage
- kotlin - PreferenceFragmentCompat
- kotlin syntax - ArrayList
- kotlin syntax - List add item
- kotlin syntax - List filter with multiple conditions
- kotlin syntax - filter list by another list
- kotlin syntax - takeIf and takeUnless
- kotlin syntax - take, takeLast, takeWhile, takeLastWhile
- kotlin syntax - Difference between takeWhile and filter
- kotlin syntax - List sortBy and sortByDescending