How to Make Android Apps 8

Android App UML DiagramsIn this tutorial we take it to the next level. In the last video we designed the interface for our Android conversion app. This time we will create the whole backend and finish the app.

The goal of this tutorial is to show you how to make an entire app in one video. We’ll cover use case descriptions, activity diagrams, class diagrams, flexible ways to solve our conversion problem, dynamically changing classes and much more. If you don’t understand everything don’t worry about it. This tutorial is all about seeing it all so you’ll grasp what is coming next.

If you like videos like this it helps to tell others on Google Plus with a click here [googleplusone]

Here is the entire package for download Android Conversion App.

Note : In the video I set up the emulator to lvl 20, change to 19 in your build.gradle file and try 19.1.0 as I show below. I was trying to do too many things and that was a mistake.

UML Diagrams

Click the image below a few times to view it full screen

Android UML Diagrams

Use Case Description


1. The user enters a number they want converted 
into an EditText

2. They change the spinner to the unit of 
measure to convert from

3. The other conversion types all show their
measurement version of that unit amount

1. Create the layout in 

a. Use relative layout for for 
label "Convert From", Edittext and

b. Use GridLayout for bottom that
contains 2 columns and 6 rows

c. Make gridlayout a blue color

d. Use the units teaspoons, tablespoons,
cup, ounces, pints, quarts, gallons,
pounds, milliliter, liter, milligram

2. Setup MainActivity

a. Create Spinner, EditText, TextViews

b. In onCreate

I. add items to the spinner

i. Create an ArrayAdapter using String array resource 
(Make strings.xml)

ii. Define the layout to use with the Spinner

iii. Connect the Adapter to the Spinner

II. Add a listener to the Spinner

i. Use setOnItemSelectedListener for Spinner

ii. NOTE : Maybe check if I am going to use a base class for unit 

III. Get the value in the EditText

IV. Initialize the TextViews

c. End onCreate ---------------------

3. Define a Quantity Class ----------

a. Quantity class contains a value and Unit enum

b. Define an enum for each unit type

c. Get the conversion for each unit type from teaspoon which is the base unit

d. Receive the number of teaspoons for the starting unit

e. To convert to the base unit teaspoons divide by the base unit

f. To convert to another unit other then teaspoons multiply
by the base unit

g. Constructor receives the value and the unit

h. Create a toString that prints out the value and unit type
to 4 decimals

4. Finish MainActivity

a. Item changed in the Spinner

I. Check if I'm converting from teaspoons?

i. If I am check if I'm updating tsps or not

* I Have Decided to Model this in an Activity Diagram *

Code From the Video


apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ''

package com.newthinktank.derekbanas.conversion;

import java.text.DecimalFormat;

public class Quantity {

    // Each object will have both a value and a unit of measure
    final double value;
    final Unit unit;

    // Enum types use a constant key to represent a value
    // They allow use to easily define how to convert all the other types
    // of measurements to convert from teaspoon to anything. Then to make
    // any conversion we convert from the starting type to teaspoon and then
    // to the final required type.

    public static enum Unit {
        tsp(1.0d), tbs(0.3333d), cup(0.0208d), oz(0.1666d),
        pint(0.0104d), quart(0.0052d), gallon(0.0013), pound(0.0125d),
        ml(4.9289d), liter(0.0049d), mg(5687.5d), kg(0.0057d);

        // We define that tsp will be the base unit of measure that we will
        // convert to and then convert from.
        final static Unit baseUnit = tsp;

        // Will hold the number of tsp the original unit converts to
        final double byBaseUnit;

        // Receives the number of tsps the starting unit equals
        private Unit(double inTsp) {
            this.byBaseUnit = inTsp;

        // Converts any other unit value to the number of tsps
        public double toBaseUnit(double value) {
            return value / byBaseUnit;

        // We convert to another unit by using the teaspoon conversion percent
        // defined in the enum
        public double fromBaseUnit(double value) {
            return value * byBaseUnit;


    // The constructor that receives the value and unit of measure
    public Quantity(double value, Unit unit) {
        this.value = value;
        this.unit = unit;

    // Converts from tsp to the desired unit type
    public Quantity to(Unit newUnit) {
        Unit oldUnit = this.unit;
        return new Quantity(newUnit.fromBaseUnit(oldUnit.toBaseUnit(value)),

    // Prints out to screen the unit amount and unit type
    public String toString() {

        DecimalFormat df = new DecimalFormat("#.0000");

        return df.format(value) + " " +;


package com.newthinktank.derekbanas.conversion;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    private Spinner unitTypeSpinner;

    private EditText amountTextView;

    TextView teaspoonTextView, tablespoonTextView, cupTextView, ounceTextView,
            pintTextView, quartTextView, gallonTextView, poundTextView,
            milliliterTextView, literTextView, milligramTextView, kilogramTextView;

    protected void onCreate(Bundle savedInstanceState) {

        // Fills the spinner with the unit options

        // Add listener to the Spinner

        // Get a reference to the edit text view to retrieve the amount of the unit type
        amountTextView = (EditText) findViewById(;



    public void initializeTextViews(){

        teaspoonTextView = (TextView) findViewById(;
        tablespoonTextView = (TextView) findViewById(;
        cupTextView = (TextView) findViewById(;
        ounceTextView = (TextView) findViewById(;
        pintTextView = (TextView) findViewById(;
        quartTextView = (TextView) findViewById(;
        gallonTextView = (TextView) findViewById(;
        poundTextView = (TextView) findViewById(;
        milliliterTextView = (TextView) findViewById(;
        literTextView = (TextView) findViewById(;
        milligramTextView = (TextView) findViewById(;
        kilogramTextView = (TextView) findViewById(;


    public void addItemsToUnitTypeSpinner(){

        // Get a reference to the spinner
        unitTypeSpinner = (Spinner) findViewById(;

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> unitTypeSpinnerAdapter = ArrayAdapter.createFromResource(this,
                R.array.conversion_types, android.R.layout.simple_spinner_item);

        // Specify the layout to use when the list of choices appears

        // Apply the adapter to the spinner


    public void addListenerToUnitTypeSpinner() {
        unitTypeSpinner = (Spinner) findViewById(;
        unitTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long arg3)
                // Get the item selected in the Spinner
                String itemSelectedInSpinner = parent.getItemAtPosition(pos).toString();

                // Verify if I'm converting from teaspoon so that I use the right
                // conversion algorithm

            public void onNothingSelected(AdapterView<?> arg0)
                // TODO maybe add something here later

    public void checkIfConvertingFromTsp(String currentUnit){



        } else {



            } else if(currentUnit.equals("cup")){


            } else if(currentUnit.equals("ounce")){


            } else if(currentUnit.equals("pint")){


            } else if(currentUnit.equals("quart")){


            } else if(currentUnit.equals("gallon")){


            } else if(currentUnit.equals("pound")){


            } else if(currentUnit.equals("milliliter")){


            } else if(currentUnit.equals("liter")){


            } else if(currentUnit.equals("milligram")){


            } else {





    public void updateUnitTypesUsingTsp(Quantity.Unit currentUnit){

        // Convert the value in the EditText box to a double
        double doubleToConvert = Double.parseDouble(amountTextView.getText().toString());

        // Combine value to unit
        String teaspoonValueAndUnit = doubleToConvert + " tsp";

        // Change the value for the teaspoon TextView

        // Update all the Unit Text Fields
        updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.tbs, tablespoonTextView);
        updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.cup, cupTextView);
        updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.oz, ounceTextView);
        updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.pint, pintTextView);
        updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.quart, quartTextView);
        updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.gallon, gallonTextView);
        updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.pound, poundTextView);
        updateUnitTextFieldUsingTsp(doubleToConvert,, milliliterTextView);
        updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.liter, literTextView);
        updateUnitTextFieldUsingTsp(doubleToConvert,, milligramTextView);
        updateUnitTextFieldUsingTsp(doubleToConvert,, kilogramTextView);


    public void updateUnitTextFieldUsingTsp(double doubleToConvert, Quantity.Unit unitConvertingTo,
                                            TextView theTextView){

        Quantity unitQuantity = new Quantity(doubleToConvert, Quantity.Unit.tsp);

        String tempUnit =;



    public void updateUnitTypesUsingOther(Quantity.Unit currentUnit){

        // Convert the value in the EditText box to a double
        double doubleToConvert = Double.parseDouble(amountTextView.getText().toString());

        // Create a Quantity using the teaspoon unit
        Quantity currentQuantitySelected = new Quantity(doubleToConvert, currentUnit);

        // Create the String for the teaspoon TextView
        String valueInTeaspoons =;

        // Set the text for the teaspoon TextView

        updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit,
                Quantity.Unit.tbs, tablespoonTextView);

        updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit,
                Quantity.Unit.cup, cupTextView);

        updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit,
                Quantity.Unit.oz, ounceTextView);

        updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit,
                Quantity.Unit.pint, pintTextView);

        updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit,
                Quantity.Unit.quart, quartTextView);

        updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit,
                Quantity.Unit.gallon, gallonTextView);

        updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit,
                Quantity.Unit.pound, poundTextView);

        updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit,
      , milliliterTextView);

        updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit,
                Quantity.Unit.liter, literTextView);

        updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit,
      , milligramTextView);

        updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit,
      , kilogramTextView);

        // Set the currently selected unit to the number in the EditText

            // Create the TextView text by taking the value in EditText and adding
            // on the currently selected unit in the spinner
            String currentUnitTextViewText = doubleToConvert + " " +

            // Create the TextView name to change by getting the currently
            // selected quantities unit name and tacking on _text_view
            String currentTextViewName = +

            // Get the resource id needed for the textView to use in findViewById
            int currentId = getResources().getIdentifier(currentTextViewName, "id",

            // Create an instance of the TextView we want to change
            TextView currentTextView = (TextView) findViewById(currentId);

            // Put the right data in the TextView



    public void updateUnitTextFieldUsingTsp(double doubleToConvert, Quantity.Unit currentUnit,
                                            Quantity.Unit preferredUnit, TextView targetTextView){

        Quantity currentQuantitySelected = new Quantity(doubleToConvert, currentUnit);

        // Algorithm used

        String tempTextViewText =




Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="com.newthinktank.derekbanas.conversion" >

        android:theme="@style/Theme.AppCompat" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


13 Responses to “How to Make Android Apps 8”

  1. Cedric says:

    Hi Derek, great tutorial on all of the tutorials.
    I am a beginner.
    I would like to ask you if I want to make an android game like Jetpack Joyride, which software I need?
    Is Android Studio enough?
    Or I need to use Unity to make it?
    I want to make a game which has the menu just like other android app. And I need to select the start button to play not just open the game then it start.

  2. Nikolay says:

    Hi Derek,
    How do you install and run an app on an android device?
    Thanks for the great tutorials.

  3. Felipe says:

    Hi Derek, I implemented this same app in a different way,
    what do you think about this approach?



  4. Mohamed says:

    Hi Derek,
    Thanks a lot for every thing. I have been following you step by step since first video and so far so good.
    This time I am having this problem:

    I keep getting this error:
    Error: error: missing method body, or declare abstract

    In the line (089) public void onNothingSelected(AdapterView arg0)

    Any idea what I have done wrong! Thanks a lot.

  5. Anna says:

    There is a slight miscalculation. mg and kg are measures of weight, not volume. A teaspoon of molasses will not weigh the same as a teaspoon of flour. But videos are most informative and very professional. Thank you

Leave a Reply

Your email address will not be published.