# Week 3

In this week, we will cover one of the fundamental topics in Android development - the Activity Lifecycle. We also gain exposure logging with commands like `log` and `logcat`. We will also familiarize ourselves with `Toasts` messages and how notification works.

We will end the week by understanding how to read and use `XML` to define our user interface layouts.

## Android Activity Lifecycle

An **activity** represents a screen that the user sees. Sometimes people refer to each screen as the presentation layer.

{% hint style="info" %}
Here's the actual definition from the official [document](https://developer.android.com/reference/android/app/Activity.html): "*An activity is a single, focused thing that the user can do.*"
{% endhint %}

An app can have multiple screens. That means there can also be multiple activities, and they can be switched and interacted with one another during runtime.

Activities are switched and interacted through what's called the **lifecycle**. Let's imagine that a user is using your awesome app. He sees a notification from another app and decides to open it. When he's done checking out the other app, he switches back to your app. So how should your app respond to each different event/scenario that takes place?

![Activity Lifecycle](https://2866356551-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LLzc1Y93b3r65PX88FE%2F-LM-A6JjChanoH1oHvfJ%2F-LM-ACVmbm71UcYoE9pR%2Factivity_lifecycle.png?alt=media\&token=9ba84019-4d9f-4844-875b-996bb4866b70)

It is very important that you read this [link](https://developer.android.com/guide/components/activities/activity-lifecycle) from the official documentation to understand how each individual methods work.

{% hint style="info" %}
Big Hint: Your understanding of how each of these methods work and relate to one another will be very useful for successfully completing a homework assignment.
{% endhint %}

## Logging Messages to Console

While working on your homework assignments or final project, there will come a point in time where you encounter a bug and are unsure of how to proceed. One of the best and easiest way to detect the source of the problem is to write log messages in your code by utilizing the `Log` class.

A very handy time to use log messages is when you want to catch exceptions:

```kotlin
fun someOtherMethod() {
    try {
        ...
    } catch (e : SomeException) {
        Log.d(TAG, "someOtherMethod()", e)
    }
}
```

All messages logged to the system can be found in the **Logcat** window.

## Toasts

A toast displays a simple message as a popup. You can determine how long the popup will continue to display.

```kotlin
val text = "Hello, Sam!"
Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT).show()
```

The toast we've created above is extremely basic. Read this [link](https://developer.android.com/guide/topics/ui/notifiers/toasts) to learn more about how to configure a toast to best align with your project' needs.

## Alerts

A dialog differs from a toast because it provides a way to prompt the user to make a decision or enter information. Usually, you'd use a dialog when you want the user to take an action before proceeding.

To create an alert, you must creating and implementing the `AlertDialog` object.

Read this section in the [Android Documentation](https://developer.android.com/guide/topics/ui/dialogs) website for more information on alerts and how to use them.

## Layouts

You must learn to understand when and how to utilize certain types of layouts. Each layout (a view group) will create a unique look and feel to your application's user interface.

There are many types of layouts to pick from, and here are some of them:

| Layouts         | Description                                                             |
| --------------- | ----------------------------------------------------------------------- |
| Linear Layout   | Aligns all children in a single direction -- vertically or horizontally |
| Relative Layout | Displays child views in a relative position                             |
| Table Layout    | Groups views into rows and columns                                      |
| Absolute Layout | You can specify the exact X and Y coordinates for its children.         |
| List View       | Displays a list of scrollable items (similar to iOS)                    |
| Grid View       | Displays items in a 2D grid and can be scrolled.                        |

### Identifying Each View

Each view object may have a unique ID assigned to it:

```kotlin
android:id="@+id/button1"
```

This allows you to easily locate the object programmatically inside a class:

```kotlin
Button myButton = (Button) findViewById(R.id.my_button)
```

{% hint style="info" %}
With Kotlin, you can also effectively reduce many lines of code needed to initialize each `findViewById` for each object. Read this [blog post](https://medium.com/@quiro91/improving-findviewbyid-with-kotlin-4cf2f8f779bb) for more details about this.
{% endhint %}

### Links to Articles

* Tutorials Point - [Android - UI Layouts](https://www.tutorialspoint.com/android/android_user_interface_layouts.htm)
* Udacity - [Android XML Visualizer](https://labs.udacity.com/android-visualizer/#/android/sandbox)
* Futurice - [Organizing layout XMLs](https://github.com/futurice/android-best-practices#resources)
