User Tools

Site Tools


android_learning:headfirst_android_development_notes:chapter_2

Chapter 2 notes

About XML

In the previous chapter, we pretty much punted our way through the XML parts of a basic Android Project. We’ll use Android’s XML configuration files to a greater extent in this and subsequent chapters. You don’t need to be an XML expert to write the XML parts of an Android project, but you do need basic XML knowledge.

If you are brand new to XML and want a crash course in it, here are some resources for you to look into:

  • W3School’s XML tutorial (look at Introduction through Attributes)
  • A reasonable tutorial 🎥 by Mark Lassof:


It might help you to know that XML was originally developed for use with Web technologies and was based on HTML syntax. It has since been adopted for use in many different situations and is particularly popular in Java circles.

p. 42

When creating the Beer Adviser app,

  • Don’t forget to use hfad.com as the Company Domain.
  • Be sure to change the Activity Name to FindBeerActivity.
  • Make sure that the Layout name is activity_find_beer.

If you mess anything up, close the project, open the folder where your Android Studio projects are, delete the project, and start again.

p. 43

Remember that Android Studio now puts the “meat” of the layout code in content_{whatever}.xml rather than in activity_{whatever}.xml. Therefore, the file you should edit is content_find_beer.xml. Also remember that the current version of Android Studio doesn’t create @string resources for TextViews but uses string literals instead.

p. 45: Some notes on Views

Views are what Android calls the UI widgets that make up an Android user interface. All views inherit from a View superclass. Button and TextView are two examples of Views.

You specify the initial properties for Views in a content_{whatever}.xml layout file. You can add Views to a layout using Android Studio’s GUI builder or by manually writing the XML code.

If you add a View to the layout using Android Studio’s GUI builder, Android Studio will give each View a unique name with the android:id attribute (e.g., android:id=“@+id/button7”). The name that Android Studio generates will be syntactically valid, but semantically it will not tell you much.

The first thing you should do when adding a new View to a layout is to change its name to something semantically useful!

If you don’t, you (or your boss, coworker, or instructor) will experience tremendous agony as you try to remember whether the button you want to use is button1 or button7.

Notice that the text attribute for the button you added using the GUI builder was given as a string literal (android:text=“New Button”). That’s bad practice, but Android Studio does it anyway.

p. 48: Code block

Code from p. 48 is reproduced here to make it easy for you to copy/paste it into your project:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".FindBeerActivity" >
    <Spinner
        android:id="@+id/color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp" />
    <Button
        android:id="@+id/find_beer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/color"
        android:layout_below="@+id/color"
        android:text="Button" />
    <TextView
        android:id="@+id/brands"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/find_beer"
        android:layout_below="@+id/find_beer"
        android:layout_marginTop="18dp"
        android:text="TextView" />
</RelativeLayout>

p. 53

Another kind of resource you often add to strings.xml is a <string-array>, which (not surprisingly) is an array of strings:

    <string-array name="beer_colors">
        <item>light</item>
        <item>amber</item>
        <item>brown</item>
        <item>dark</item>
    </string-array>

p. 54

Hook up a <string-array> to a Spinner using an @array resource on its android:entries attribute:

android:entries="@array/beer_colors"

p. 56

Hook up a Button to a Java method by specifying the Button's android:onClick attribute:

android:onClick="onClickFindBeer"

p. 56: Codecheck 1

The app through p. 56.


p. 57: Code block

Code from p. 57 is reproduced here to make it easy for you to copy/paste it into your project:

package com.hfad.beeradviser;
import android.os.Bundle;
import android.app.Activity;
public class FindBeerActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_beer);
    }
}

p. 59

TextView brands = (TextView)findViewById(R.id.brands);

Why do you have to cast the reference returned by findViewById(R.id.brands) to a TextView? It's because the findViewById() method returns values of type View. This syntax is clumsy and error prone, but it's the way Android does it.

pp. 60-62

String beerType = String.valueOf(color.getSelectedItem());

Why do you need to convert the value returned by Spinner.getSelectedValue() to a String? It's because that method returns a value of type Object. See the Spinner documentation page and search for “getSelectedValue”.

p. 65: Codecheck 2

The app through p. 65.


p. 66: Quicktip

Creating a new class in Android Studio


p. 66: Code block

Code from p. 66 is reproduced here to make it easy for you to copy/paste it into your project:

package com.hfad.beeradviser;
        import java.util.ArrayList;
        import java.util.List;
 
public class BeerExpert {
    List<String> getBrands(String color) {
        List<String> brands = new ArrayList<String>();
        if (color.equals("amber")) {
            brands.add("Jack Amber");
            brands.add("Red Moose");
        } else {
            brands.add("Jail Pale Ale");
            brands.add("Gout Stout");
        }
        return brands;
    }
}

p. 69

The StringBuilder class.

p. 71: Finished


android_learning/headfirst_android_development_notes/chapter_2.txt · Last modified: 2016/01/07 04:45 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki