Skip to main content

Android Jetpack – Handling Lifecycles Using Lifecycle Library

 

This tutorial is part of my Android Jetpack tutorials and source can be found here

If you’ve been around in Android for quite a while, you probably know that managing an app’s lifecycle is quite a headache! Have you ever had these moments yourself?

Should I put this code in onStart() or onResume()?

Should I put this code in onPause() or onStop()?

*Runs the app. Wait, why isn’t it called?!

*Moves code to another lifecycle callback

It’s sometimes unpredictable and confusing whether a piece of code should be inside this lifecycle callback or not.

Fortunately, after many years, Android heard our outcries and provided us with a solution – Lifecycle library. 🤩🎉

The android.arch.lifecycle package provides classes and interfaces that let you build lifecycle-aware components—which are components that can automatically adjust their behavior based on the current lifecycle state of an activity or fragment.

 

How Can It Help You?

Read More

Android Jetpack – Basic Data Binding Tutorial in Android

 

This tutorial is part of my Android Jetpack tutorials and source can be found here

In this tutorial, we are going to make an Android app that uses the Data Binding library but first…

What is Data Binding in Android?

Data Binding is binding data to UI components. Instead of just “setting” data to a UI component, “binding” data to a UI component is “setting” the data AND “coupling” it with the UI component so that if the data changes, the UI component(s) that are bind to that data are also affected.

 

Structure Of An Android Layout With Data Binding

  1. Root tag is <layout>
  2. Imports and Variables are at the top
  3. Wrap import and variable tags with a <data> tag
  4. Data Binding usage at the bottom

It’s structured like a normal class where our imports and most of our variables are declared at the top and we use them at the bottom.

 

Tags

<layout> – tells the compiler that this layout has data binding in it so generate the binding class for this layout.

<import> – similar to how we interpret imports in normal classes. Import a class in this layout.

<variable> – similar to how we interpret variables in normal classes. Declare a variable and it’s type for the layout to use.

<data> – wraps <import> and <variable> tags for the “data” that we are going bind for this layout.

 

Binding Data

When you set the root tag of your layout to <layout>, a binding class is generated by the Data Binding library. For example, activity_main.xml will have a binding class called ActivityMainBinding.

This class holds all the bindings from the layout properties to the layout’s views and knows how to assign values for the binding expressions.

The recommended method to create the bindings is to do it while inflating the layout:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    val user = User("Test", "User")
    binding.user = user
}

If you are using data binding items in a Fragment, ListView, or RecyclerView adapter, use the inflate() methods of the binding classes or the DataBindingUtil class:

val binding = ListItemBinding.inflate(layoutInflater, viewGroup, false)
// or
val binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false)

 

What are we going to make?

In this tutorial, we will make a very simple app that demonstrates how we can use data binding in Android. The theme for this app is inspired by a game that is very close to my heart – Dota 2.

We will display a pre-determined list of heroes and when you click a hero it shows some details (name, abilities, etc.) Read More

Exploring Android Jetpack

 

Recently at Google I/O 2018, Android Jetpack was announced.

I bet you already know what it is or it’s purpose but for those of you who don’t know yet, Android Jetpack is basically a set of tools, components or libraries that helps Android developers build modernized, well-architected, less boilerplate and testable apps.

 

Jetpack manages tedious activities like background tasks, navigation, and lifecycle management, so you can eliminate boilerplate code and focus on what makes your app great. Built around modern design practices, Jetpack components enable fewer crashes and less memory leaked with backwards-compatibility baked in.

 

Android Jetpack is divided into four categories/branches:

  • Foundation
    • AppCompat (Coming Soon)
    • Android KTX (Coming Soon)
    • Multidex (Coming Soon)
    • Test (Coming Soon)
  • Architecture
    • Data Binding
    • Lifecycles
    • LiveData (Coming Soon)
    • Navigation (Coming Soon)
    • Paging (Coming Soon)
    • Room (Coming Soon)
    • ViewModel (Coming Soon)
    • WorkManager (Coming Soon)
  • Behavior
    • Download Manager (Coming Soon)
    • Media & playback (Coming Soon)
    • Notifications (Coming Soon)
    • Permissions (Coming Soon)
    • Sharing (Coming Soon)
    • Slices (Coming Soon)
  • UI
    • Animation & transitions (Coming Soon)
    • Auto (Coming Soon)
    • Emoji (Coming Soon)
    • Fragment (Coming Soon)
    • Layout (Coming Soon)
    • Palette (Coming Soon)
    • TV (Coming Soon)
    • Wear OS by Google (Coming Soon)

 

JOIN ME! As we will explore Android Jetpack and learn how we can build great apps with it.

I’ll be updating this post and include the link every time I publish a new post about a component in Android Jetpack.

Be sure to subscribe and bookmark this post to be updated with the latest post about Android Jetpack.

[Make-A] Movies app using TMDb API – Exercises and Thanks!

CONGRATULATIONS!!! 🎉🎉🎉

Give yourself a high five and be proud of yourself! I hope that this series improved your skills and you’ve learned a lot from it. There will be times where you encounter new things such as Retrofit – the networking library that we used for this series. It’s normal and I’ve encountered a LOT of new things as an Android developer also.

 

Take your time. No need to rush. You’ll get there. Always be curious of something that you don’t know about. Study them and apply it in your apps in anyway that you can.

 

I’d be super happy to share your work on our Facebook page where this tutorial series is also shared. By doing so, you will be able to inspire other people such as college students or other developers who saw what you made that they can do it too!

Just message me a link to a demo of your app and I’ll happily share it to our page.

Share your work/demo by either:

 

Where to go from here?

Read More

[Make-A] Movies app using TMDb API Part 8 – Colors and a Free Launcher Icon Generator

 

In Material Design, there are two types of colors – Primary Color(s) and Secondary Color.

Primary Color(s) are the most frequently displayed colors across your app’s screens and components. It is usually composed of lighter and darker tones of your primary color.

Secondary Color is what you call “accent” color. It is used to get the attention of the user. It should be complementary of your primary color should not be a light or dark variations of your primary color. It is commonly used in FloatingActionButtons (FAB).

Here’s sample color palette that you can find in the Material Design Color Palette:

 

 

Colors that has a label that starts with an “A” are Accent Colors. Others are variations of a primary color which is 500.

  • 500 – Primary Color
  • 700 – Dark Primary Color
  • A200 – Accent Color

Let’s get started

Read More

[Make-A] Movies app using TMDb API Part 7 – Using CoordinatorLayout, AppBarLayout and CollapsingToolbarLayout for Movie Details Screen

 

TMDb API provides a way to get the information of a movie. There’s a LOT of data that are provided from this endpoint. You can choose whatever you want to use or display. For this tutorial we will focus on these details only:

  • Title
  • Backdrop
  • Genres
  • Overview
  • Release Date
  • Rating
  • Trailers
  • Reviews

There’s a lot more to choose from. Feel free to add some more if you want. You can view all the details here.

Here’s how we will use CoordinatorLayout, AppBarLayout, and CollapsingToolbarLayout to create a better and beautiful UI:

Read More

[Make-A] Movies app using TMDb API Part 6 – Sort Movies by Popular, Top Rated and Upcoming

 

Currently, we show the movies sorted by popularity. But what if your users want to know the latest/upcoming movies this year or top rated movies? We should provide flexibility for our users to give them a much better experience using our app.

TMDb API offers five sorting options for our movies:

  • Popular
  • Top Rated
  • Upcoming
  • Latest
  • Now Playing

For this tutorial we will only use three – Popular, Top Rated and Upcoming. The remaining two will be left as an exercise.

Our sorting options will be presented using a PopupMenu. Here’s how we will implement our sorting feature:

Read More

[Make-A] Movies app using TMDb API Part 5 – Pagination

 

If I were to define it in the most simple way, Pagination is dividing data into smaller parts.

When we fetch movies from TMDb API, we received movies by page. That’s why when fetch movies in Part 4 and you scroll down, you can reach the bottom of your list and that’s it. It’s because what we got was only page 1.

Why? Imagine receiving thousands of movies in one API call. It wouldn’t be very efficient right?

A good solution would be is when you scroll half of your list you should start fetching page 2 and the next page and so on. It depends on your preference, you can start fetching the next page if list is scrolled 25%, 50%, 75%, or 100%. It’s up to you.

Here’s how we can fetch the next page during scrolling:

Read More

[Make-A] Movies app using TMDb API Part 4 – Networking using Retrofit Library

 

Retrofit is a type-safe HTTP client for Android and Java. I use this library a LOT be it at work or personal projects.

Here are 5 things why I like to use Retrofit:

  • It’s super easy to use.
  • Documentation is great and concise.
  • Easy to setup.
  • It makes me more productive and speeds up my development.
  • It solves the problem – an easy way to do networking in Android and Java

Table of Contents

  1. Part 1 – Specs and Introduction
  2. Part 2 – Requesting an API Key
  3. Part 3 – Movie List Item Layout Using ConstraintLayout
  4. Part 4 – Networking using Retrofit Library and Image Loading using Glide
  5. Part 5 – Pagination
  6. Part 6 – Sort Movies by Popular, Top Rated and Upcoming
  7. Part 7 – Using CoordinatorLayout, AppBarLayout, CollapsingToolbarLayout for Movie Details Screen
  8. Part 8 – Colors and a Free Launcher Icon Generator
  9. 5 Exercises and Thanks!

 

1. Importing Retrofit

Open your app-level build.gradle under app/ directory. Under dependencies import the library.

dependencies {
    ...
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
}

As of this writing, this is the latest version of Retrofit. Refer to Retrofit’s website for the latest version.

 

2. Importing Gson Converter

Gson is a Java serialization/deserialization library to convert Java Objects into JSON and back.

You might be wondering what’s the use of this?

Read More