Lecture 1 - Introduction

< CS193A Android Programming

Here we walk through the most basic parts of an Android app. Also, homework 1 is at the end of this page.

Step 0 - Installing the Android SDK

Everything below assume the Android SDK is installed and an AVD (Android Virtual Device) set up so the emulator can work -- there are links for those first steps below for homework 1 at the end of this doc.

First Android Project

In class I'll create the android default project, and look around to see the important components.

Manifest

Now we'll look at the sources behind this simple app; then we'll add some features to it. Look at default manifest.xml -- there's one Application and it can contain any number of Activities. Lots of other information about an application is declared in its manifest.xml, so this is just our first simple example.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="edu.stanford.nick"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HelloActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

"@string/app_name" forms refer to data in the res part of the project. The Activity "android:name" refers to the class name that implements the activity -- ".HelloActivity" (the . means in the current package). The MAIN/LAUNCHER intent provides the app icon to launch this activity. Later we'll see many more examples of intents as a way to invoke an activity.

Activity Source Code

package edu.stanford.nick;

import android.app.Activity;
import android.os.Bundle;

public class HelloActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

The one interesting line at this point is the setContentView() call which installs the UI defined in res/layout/main.xml

main.xml Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

Graphical Layout

Now Add Activity Code

public class HelloActivity extends Activity {

    // Store pointer to EditText widget
    private EditText editText1;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        // Store a pointer to editText1. Set up button1 to add a !.
        editText1 = (EditText) findViewById(R.id.editText1);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
	  public void onClick(View v) {
            // Lines inside this onClick() method are run
            // when the button is clicked.
	    editText1.setText(editText1.getText() + "!");
	  }
	});
	...

Seeing Crash Trace - LogCat


This is as far as we got in lecture 1, so skip down to homework 1 below to try that.

Add Image Resource and ImageView

Nested Surf LinearLayout

Surf Code

        // Start a "web url" intent with editText2
        editText2 = (EditText) findViewById(R.id.editText2);
        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_VIEW); // most common
				intent.setData(Uri.parse(editText2.getText().toString()));
				HelloActivity.this.startActivity(intent);
			}
		});

Dial Code

Here is the intent code to invoke the dialer. As above, add a button/EditText in a nested horizontal layout to dial the number in the text field.

				Intent intent = new Intent(Intent.ACTION_DIAL); // vs. _CALL
				intent.setData(Uri.parse("tel:" + editText3.getText().toString()));
				// uri form should be "tel:555-555-5555"
				HelloActivity.this.startActivity(intent);

Homework 1

Now for homework 1: create an app somewhat similar to the demo app (as far as we got in lecture). In general, we expect homework to be done before the next class, although in this case we're not going to collect the homework; it's just for you to get some practice.