Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
How to Build Android Apps with Kotlin

You're reading from   How to Build Android Apps with Kotlin A hands-on guide to developing, testing, and publishing your first apps with Android

Arrow left icon
Product type Paperback
Published in Feb 2021
Publisher Packt
ISBN-13 9781838984113
Length 794 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (4):
Arrow left icon
Eran Boudjnah Eran Boudjnah
Author Profile Icon Eran Boudjnah
Eran Boudjnah
Jomar Tigcal Jomar Tigcal
Author Profile Icon Jomar Tigcal
Jomar Tigcal
Alex Forrester Alex Forrester
Author Profile Icon Alex Forrester
Alex Forrester
Alexandru Dumbravan Alexandru Dumbravan
Author Profile Icon Alexandru Dumbravan
Alexandru Dumbravan
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface
1. Creating Your First App 2. Building User Screen Flows FREE CHAPTER 3. Developing the UI with Fragments 4. Building App Navigation 5. Essential Libraries: Retrofit, Moshi, and Glide 6. RecyclerView 7. Android Permissions and Google Maps 8. Services, WorkManager, and Notifications 9. Unit Tests and Integration Tests with JUnit, Mockito, and Espresso 10. Android Architecture Components 11. Persisting Data 12. Dependency Injection with Dagger and Koin 13. RxJava and Coroutines 14. Architecture Patterns 15. Animations and Transitions with CoordinatorLayout and MotionLayout 16. Launching Your App on Google Play

Adding Items Interactively

We have just learned how to remove items interactively. What about adding new items? Let's look into it.

Similar to the way we implemented the removal of items, we start by adding a function to our adapter:

fun addItem(position: Int, item: ListItemUiModel) {
    listData.add(position, item)
    notifyItemInserted(position)
}

You will notice that the implementation is very similar to the removeItem(Int) function we implemented earlier. This time, we also receive an item to add and a position to add it. We then add it to our listData list and notify RecyclerView that we added an item in the requested position.

To trigger a call to addItem(Int, ListItemUiModel), we could add a button to our main activity layout. This button could be as follows:

<Button
    android:id="@+id/main_add_item_button"
    android:layout_width="match_parent"
 ...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime