Session 5: Android Deep Dive

A Deep Dive into Android

Hi Hack Schoolers! Hope everybody had a blast at hack on the hill, and learned something new from the experience! Let's get back into some more Hack School, and do some more android development. Topics today include reviewing the Android Lifecycle, Intents, List Views, Timers, and Fragments.

  1. Want to get more involved in Hack? we invite everyone to join us for DINNER after the session tomorrow! This is your chance to ask questions, know more about Hack and bond with your fellow hackers!
  2. Don't forget to submit the projects! The current winning teams are ACM Hack & Chill, HungryNerds, Make UCLA Great Again, and TheLoneCoder, each with 48 points.
  3. PRIZES FOR HACK SCHOOL: Did we mention there are prizes for the top three teams by the end of this quarter? Come to win — and see you all tomorrow!

If you missed today's session, here are the relevant sources: Slides


The topics we'll be covering are as follows:

  • Review of Activities
  • Android Life Cycle
  • Intents
  • List Views
  • Timers
  • Fragments

The Activity class basically serves as the entry point for user interaction and Main Activity is the first screen of the app. Generally, there's one activity per screen but most apps have multiple screens (and thus multiple activities). All Activities have what's known as a lifecycle.

Android Lifecycle

The Android Activity Lifecycle is controlled by 7 methods, which are as follows:

  • onCreate()
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onRestart()
  • onDestroy()

This 7 lifecycle method of Activity describes how activity will behave at different states.


Intent filters are a powerful feature of the Android platform. They allow you to do a whole range of things, but they are most commonly used to launch an activity or to transition from one screen to another in your application.

Here's is an example snippet that can be used to move from your current activity to NextActivity (which will only work if NextActivity is defined in the manifest:

Intent i = new Intent(getApplicationContext(),NextActivity.class);


Sometimes, you want to be able to send additional information to the activity you are transitioning to. You can send variables like string, int, etc. as extras in your intent:

Intent i = new Intent(context,NextActivity.class);  
i.putExtra(“key”, “value”);  

Once you're in NextActivity, you want to be able to receive the data. These variables are often used to set up and configure the new activity:

Intent i = getIntent();  
String extra = i.getStringExtra(“key”);  

There are other purposes of Intents besides using them to start new Activities. The other two most common ones are:

  • Starting a service, which is a component that operates in the background of the app without a user interface
  • Delivering a broadcast, which is a message that any app can receive. The system delivers various broadcasts for events like booting up or for when the device starts charging.

In addition, there are two types of intents: explicit and implicit. Explicit intents allow you to start a component by its name. You need to specify the component to start this Intent and you'll generally use them in your own apps because you know the activity/service you want to start. Implicit intents describe a general action to perform, which means that you don't need to specify the component to start.

List Views

List Views are also considered a table view. This is among the most commonly used interface for displaying information to the user. It basically contains a view group of scrollable items. These items can be automatically inserted into list that pulls content from a source or database. The purpose of this structure is to use a repeating UI layout for an arbitrary number of dynamic cells.

Adapters are used to bind a data element with the List View. In this example, an array list is being connected with the list view:

listArray = new ArrayList<String>();  
listAdapter = new ArrayAdapter<String>(  

Here's an example source code that can help you confirm your understanding of the list view click here.


Timers are useful for running asynchronous events in Android (i.e. if you want to display a ticking stopwatch). The class CountDownTimer provides a simple strategy for implementing this without much effort. There are two event functions: onTick() and onFinish(). These are called whenever the tick interval passes and the timer reaches the end respectively. Here's an example of the CountDownTimer class:

new CountDownTimer(3000, 1000) {  
    public void onTick(long millisUntilFinished){
        textField.setText(“Seconds remaining:” + millisUntilFinished/1000);
    public void onFinish(){
        textField.setText(“Timer is finished!”);

A fragment is usually part of an activity's user interface, and contributes its own layout to the activity. You can combine multiple fragments in an activity to make a multi-pane UI and reuse fragments in different activities. Fragments have their own lifecycle and receive their own input events. You can also add or remove them to an activity while it's running. A fragment is directly affected by its host, so if you close the activity, the fragment will close by default.

You should implement at least the following for the Fragment lifecycle:

  • OnCreate()
  • onCreateView()
  • onPause()

To provide a layout for a fragment, you must implement the onCreateView() callback method, which the Android system calls when it's time for the fragment to draw its layout. To return a layout from onCreateView(), you can inflate it from a layout resource defined in XML.

Here is an example Fragment:

public static class ExampleFragment extends Fragment {  
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);

Here is an example code of adding a Fragment in XML:

    <fragment android:name=""
            android:layout_height="match_parent" />

Let's get some points!

For beginners you can solve all of the exercises and receive points for completing those. For intermediate/advanced students you can work on the weekly project with your team, and receive points for completing that. You can only get credit for one or the other, but not for both so choose the one most appropriate for your skill level. Please only submit one .zip file per team. Include the contents of your entire android project, and also provide your team access code in a text file. Otherwise you will not receive credit!


Submissions will be due 1 week from the session, but we will accept late submissions taking some points for each day it is late. The lateness penalty for an assignment that is submitted between N and N+1 full days late (where N is nonnegative) is 2^N % of the assignment's value. That is, the penalty is 1% for being up to 1 day late, 2% for being from 1 to 2 days late, 4% for being from 2 to 3 days late, and so forth.


Exercise 1:

For the first exercise your goal is build a basic ToDo List

  • It should have a ListView with an adapter tied to an ArrayList
  • It should have a EditText where you can type a new item in
  • It should have a Button that when pressed add the current string in the EditText to the ListView
  • Try not to copy the solution provided in the slides, fully attempt it on your own first and then refer to it if stuck.

Extra Credit:

  • Add ability to delete items from the list
Exercise 2:

For the second part your goal is build a basic timer

  • Use the CountDownTimer() discussed in the slides
  • Add an EditText that allows you to specify when the timer will go off
  • Create a Button that when pressed, begins the timer
  • Create a TextView for updating the time as it runs out

Extra Credit:

  • Add another Button the gives user's the ability to pause/play the timer.
Exercise 3:

Build a basic Activity with a fragment

  • Add a Button that when pressed loads the fragment into the activity
  • Add another Button that when pressed removes the fragment from the activity

Note: Put all of your exercises into the same android project when submitting

Project 5

For the fifth project we will be building out a Pokedex using the skills you learned today. The goal is to build out this project with your team based off some of the knowledge gained from this section. If you are feeling confident you can attempt to complete the project without the skeleton, but it will be a lot easier if you use it. We will be uploading the project solution towards the end of the week, so if you haven't cracked it by then look at the solution to learn the correct implementation.


The goal is to build a Pokedex with as little guidance as possible, so try to come up with unique solutions when you run into problems. You're being given lots of flexibility so come up with your own implementation: grading will be very generous. For this project you've been only provided with the image assets for this project, so the goal is to create the rest of the application your self. Here are instruction for each specific screen:

Main Screen
  • Build out a ListView that loads in the appropriate Pokemon based on the row number
  • Figure out how to parse the CSV file to get the right Pokemon data. You've been provided with a class that should handle the majority of the work for you
  • Each cell should have an ImageView and Textview with the right data loaded
  • When pressed each cell should transition from the current MainActivity into the StatsActivity
Stats Screen
  • This page should receive the selected Pokemon as a intent extra as well as the stats
  • The current stats for weight, height, and exp should be displayed to the user. These are all available in the csv file provided
  • There should be a button that allows you to return to the previous page

Here is the link to the project skeleton:

End Product Goal

Main Page

Stats Page