0% found this document useful (0 votes)
21 views11 pages

MAD Lab 3

This lab covers developing Android applications using Kotlin and implementing a list view and recycler view. Students will learn how to create list views and recycler views with Kotlin code, including creating model classes, adapter classes and XML layouts. The document provides code examples for implementing a list view with a model, adapter and XML layout as well as code for initializing a basic recycler view.

Uploaded by

Mujtaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views11 pages

MAD Lab 3

This lab covers developing Android applications using Kotlin and implementing a list view and recycler view. Students will learn how to create list views and recycler views with Kotlin code, including creating model classes, adapter classes and XML layouts. The document provides code examples for implementing a list view with a model, adapter and XML layout as well as code for initializing a basic recycler view.

Uploaded by

Mujtaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Lab 3

Objective: In this course the students will learn how to develop applications for
android mobile devices, including smartphones and tablets. Students are introduced to the
survey of current mobile platforms, mobile application development environments,
mobile device input methods, as well as developing applications for most popular mobile
platforms. Students will design and build a variety of Apps throughout the course to
reinforce learning and to develop real competency with latest ASO strategies.

Description:
Programming is an increasingly important skill, whether you aspire to a career in
software development, or in other fields. This course is the first in the specialization
Android Application Development, but its lessons extend to any language you might
want to learn like java or Kotlin. This is because programming is fundamentally about
figuring out how to solve a class of problems, development of application and writing the
algorithm, a clear set of steps to solve any problem in its class.

Assessment tools:
The assessment is according to the student participation in the lab if the student solves the
exercise, he will get the participation mark for this lab.

Classes and Objects:


Kotlin supports both object oriented programming (OOP) as well as functional
programming. Object oriented programming is based on real time objects and classes.
Kotlin also support pillars of OOP language such as encapsulation, inheritance and
polymorphism.
Listview Kotlin Code:

package com.example.senthil.kotlin_listview.Activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.AdapterView
import android.widget.ListView
import com.example.senthil.kotlin_listview.Adapter.ListViewModelAdapter
import com.example.senthil.kotlin_listview.R
import com.example.senthil.kotlin_listview.Utils.Helper

class ListViewActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_list_view)

val listView = findViewById<ListView>(R.id.sample_listVw)

var listViewAdapter = ListViewModelAdapter(this,


Helper.Companion.getListViewModelList())
listView.adapter = listViewAdapter
listView.onItemClickListener = AdapterView.OnItemClickListener {
adapterView, view, position, id ->

}
}
}

ListView Adapter Kotlin Code:

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.example.senthil.kotlin_listview.Model.ListViewModel
import com.example.senthil.kotlin_listview.R

class ListViewModelAdapter(val context: Context, val listModelArrayList:


ArrayList<ListViewModel>) : BaseAdapter() {

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


ViewGroup?): View? {
val view: View?
val vh: ViewHolder

if (convertView == null) {
val layoutInflater = LayoutInflater.from(context)
view = layoutInflater.inflate(R.layout.list_view_item,
parent, false)
vh = ViewHolder(view)
view.tag = vh
} else {
view = convertView
vh = view.tag as ViewHolder
}

vh.tvTitle.text = listModelArrayList[position].title
vh.tvContent.text = listModelArrayList[position].content
return view
}

override fun getItem(position: Int): Any {


return listModelArrayList[position]
}

override fun getItemId(position: Int): Long {


return position.toLong()
}

override fun getCount(): Int {


return listModelArrayList.size
}
}

private class ViewHolder(view: View?) {


val tvTitle: TextView = view?.findViewById<TextView>(R.id.tvTitle)
as TextView
val tvContent: TextView =
view?.findViewById<TextView>(R.id.tvContent) as TextView
}
ListView Model Kotlin Code:

class ListViewModel{
var id: Int? = null
var title: String? = null
var content: String? = null

constructor(id: Int, title: String, content: String) {


this.id = id
this.title = title
this.content = content
}
}
ListView XML Code:

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.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=".Activity.ListViewActivity">

<ListView
android:id="@+id/sample_listVw"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
ListView Item Code:

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.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="wrap_content"
android:paddingBottom="12dp">

<TextView
android:id="@+id/tvTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:text="TextView"
android:textColor="?android:attr/colorForeground"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvContent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="@+id/tvTitle"
app:layout_constraintStart_toStartOf="@+id/tvTitle"
app:layout_constraintTop_toBottomOf="@+id/tvTitle" />
</android.support.constraint.ConstraintLayout>

RecyclerView XML Code:

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"

android:layout_height="match_parent"></android.support.v7.widget.Recycle
rView>
package com.bett.kotlinrecyclerview

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import bett.com.kotlinlistview.dtos.UserDto
import com.bett.kotlinrecyclerview.adapters.UsersAdapter

class MainActivity : AppCompatActivity() {

private var recyclerView: RecyclerView? = null

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

recyclerView = findViewById(R.id.recyclerView)
var adapter = UsersAdapter(generateData())
val layoutManager = LinearLayoutManager(applicationContext)
recyclerView?.layoutManager = layoutManager
recyclerView?.itemAnimator = DefaultItemAnimator()

recyclerView?.adapter = adapter
adapter.notifyDataSetChanged()
}
private fun generateData(): ArrayList<UserDto> {
var result = ArrayList<UserDto>()

for (i in 0..9) {
var user: UserDto = UserDto("Bett", "Awesome work ;)")
result.add(user)
}

return result
}

RecyclerView Library (Gradle File)

implementation 'com.android.support:recyclerview-v7:26.0.1'

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy