Bird
Raised Fist0
Android Kotlinmobile~20 mins

Pagination basics in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Simple Pagination List
This screen shows a list of items with pagination. It loads 10 items at a time and has buttons to go to the next or previous page.
Target UI
-------------------------
| Simple Pagination List |
-------------------------
| Item 1                |
| Item 2                |
| Item 3                |
| ...                   |
| Item 10               |
-------------------------
| < Prev       Next >   |
-------------------------
Display a vertical list of 10 items per page
Show current page items labeled 'Item X'
Add 'Prev' and 'Next' buttons at the bottom
'Prev' button disabled on first page
'Next' button disabled on last page (page 5)
Update list when buttons are tapped
Starter Code
Android Kotlin
package com.example.pagination

import android.os.Bundle
import android.widget.Button
import android.widget.ListView
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity

class PaginationActivity : AppCompatActivity() {
    private lateinit var listView: ListView
    private lateinit var prevButton: Button
    private lateinit var nextButton: Button

    private val itemsPerPage = 10
    private val totalPages = 5
    private var currentPage = 1

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

        listView = findViewById(R.id.listView)
        prevButton = findViewById(R.id.prevButton)
        nextButton = findViewById(R.id.nextButton)

        // TODO: Initialize list and buttons
    }

    // TODO: Add functions to update list and handle button clicks
}
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
package com.example.pagination

import android.os.Bundle
import android.widget.Button
import android.widget.ListView
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity

class PaginationActivity : AppCompatActivity() {
    private lateinit var listView: ListView
    private lateinit var prevButton: Button
    private lateinit var nextButton: Button

    private val itemsPerPage = 10
    private val totalPages = 5
    private var currentPage = 1

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

        listView = findViewById(R.id.listView)
        prevButton = findViewById(R.id.prevButton)
        nextButton = findViewById(R.id.nextButton)

        updateList()

        prevButton.setOnClickListener {
            if (currentPage > 1) {
                currentPage--
                updateList()
            }
        }

        nextButton.setOnClickListener {
            if (currentPage < totalPages) {
                currentPage++
                updateList()
            }
        }
    }

    private fun generateItemsForPage(page: Int): List<String> {
        val start = (page - 1) * itemsPerPage + 1
        val end = start + itemsPerPage - 1
        return (start..end).map { "Item $it" }
    }

    private fun updateList() {
        val items = generateItemsForPage(currentPage)
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, items)
        listView.adapter = adapter

        prevButton.isEnabled = currentPage > 1
        nextButton.isEnabled = currentPage < totalPages
    }
}

This solution creates a simple pagination screen in Android using Kotlin. It shows 10 items per page labeled "Item X".

We keep track of the current page with currentPage. The generateItemsForPage function creates the list of items for that page.

The updateList function updates the ListView with the current page's items and enables or disables the Prev and Next buttons depending on the page.

Click listeners on the buttons change the page number and refresh the list.

This approach is simple and easy to understand for beginners learning pagination basics.

Final Result
Completed Screen
-------------------------
| Simple Pagination List |
-------------------------
| Item 11               |
| Item 12               |
| Item 13               |
| Item 14               |
| Item 15               |
| Item 16               |
| Item 17               |
| Item 18               |
| Item 19               |
| Item 20               |
-------------------------
| < Prev       Next >   |
-------------------------
Tapping 'Next' loads the next 10 items and updates the list
Tapping 'Prev' loads the previous 10 items
'Prev' button is disabled on first page
'Next' button is disabled on last page
Stretch Goal
Add a TextView showing the current page number between the Prev and Next buttons.
💡 Hint
Add a TextView in the layout and update its text in updateList() to show 'Page X of Y'.

Practice

(1/5)
1. What is the main purpose of pagination in mobile apps?
easy
A. To split data into smaller pages for faster loading and less memory use
B. To combine all data into one big list
C. To delete old data permanently
D. To change the app's theme colors

Solution

  1. Step 1: Understand pagination concept

    Pagination divides large data into smaller parts called pages.
  2. Step 2: Identify benefits of pagination

    This helps apps load data faster and use less memory by not loading everything at once.
  3. Final Answer:

    To split data into smaller pages for faster loading and less memory use -> Option A
  4. Quick Check:

    Pagination = split data for speed and memory [OK]
Hint: Pagination means loading data in small parts, not all at once [OK]
Common Mistakes:
  • Thinking pagination deletes data
  • Believing pagination merges all data
  • Confusing pagination with UI themes
2. Which Kotlin code snippet correctly represents fetching page 2 with page size 10?
easy
A. val page = 2 val size = 10 val start = size - page
B. val page = 2 val size = 10 val start = page * size
C. val page = 2 val size = 10 val start = (page - 1) * size
D. val page = 2 val size = 10 val start = page + size

Solution

  1. Step 1: Calculate start index for page 2

    Start index = (page number - 1) * page size, so (2 - 1) * 10 = 10.
  2. Step 2: Check code snippets

    val page = 2 val size = 10 val start = (page - 1) * size uses correct formula; others use wrong math.
  3. Final Answer:

    val start = (page - 1) * size -> Option C
  4. Quick Check:

    Start index formula = (page - 1) * size [OK]
Hint: Start index = (page - 1) times page size [OK]
Common Mistakes:
  • Using page * size instead of (page - 1) * size
  • Subtracting page from size incorrectly
  • Adding page and size instead of multiplying
3. Given this Kotlin code snippet, what will be printed?
val page = 3
val size = 5
val start = (page - 1) * size
println("Loading items from index $start to ${start + size - 1}")
medium
A. Loading items from index 10 to 14
B. Loading items from index 15 to 19
C. Loading items from index 5 to 9
D. Loading items from index 0 to 4

Solution

  1. Step 1: Calculate start index

    start = (3 - 1) * 5 = 2 * 5 = 10.
  2. Step 2: Calculate end index

    end = start + size - 1 = 10 + 5 - 1 = 14.
  3. Final Answer:

    Loading items from index 10 to 14 -> Option A
  4. Quick Check:

    Start=10, End=14 [OK]
Hint: End index = start + size - 1 for page ranges [OK]
Common Mistakes:
  • Calculating start as page * size
  • Forgetting to subtract 1 for end index
  • Mixing up start and end indexes
4. What is wrong with this Kotlin pagination code?
val page = 1
val size = 10
val start = page * size
println("Loading items from index $start to ${start + size - 1}")
medium
A. Page size should be multiplied by 2
B. Start index calculation is off by one page
C. Page number should start from 0
D. No error, code is correct

Solution

  1. Step 1: Check start index formula

    Correct formula is (page - 1) * size, but code uses page * size.
  2. Step 2: Understand impact

    For page 1, start should be 0, but code gives 10, skipping first 10 items.
  3. Final Answer:

    Start index calculation is off by one page -> Option B
  4. Quick Check:

    Start index must be (page - 1) * size [OK]
Hint: Page 1 start index is zero, so subtract 1 in formula [OK]
Common Mistakes:
  • Using page * size instead of (page - 1) * size
  • Starting page count at 0 incorrectly
  • Assuming no off-by-one errors
5. You want to load pages of data in your app. If the total data size is 47 items and page size is 10, how many pages do you need to load all items?
hard
A. 4
B. 7
C. 6
D. 5

Solution

  1. Step 1: Calculate full pages

    47 items divided by 10 per page gives 4 full pages (4 * 10 = 40 items).
  2. Step 2: Account for leftover items

    Remaining 7 items need an extra page, so total pages = 4 + 1 = 5.
  3. Step 3: Re-check calculation

    Wait, 4 full pages cover 40 items, leftover 7 items need 1 page, total 5 pages.
  4. Final Answer:

    5 -> Option D
  5. Quick Check:

    Pages = ceil(47/10) = 5 [OK]
Hint: Divide total items by page size and round up [OK]
Common Mistakes:
  • Using integer division without rounding up
  • Counting only full pages
  • Miscounting leftover items
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard