-
Notifications
You must be signed in to change notification settings - Fork 10
Lecture 2
In this lecture we will cover some basics of Android development.
To create a new Android Application Project, in Eclipse choose File → New → Android Applicatipn Project. Fill the wizard out with the following values for an application using Android 5.0 SDK APIs:


After the second screen, click Finish to have the wizard generate your skeleton code.

Take a look at the AndroidManifest.xml, it describes your application.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.stanford.cs231m"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
</application>
</manifest>
An Activity contains your user interface components. We will now create a very simple Activity that we will launch on the device to show the text "Hello Android!". Go to File → New → Class and create new class that derives from android.app.Activity:

Now, open the newly created java source file, and add the following code:
public class HelloAndroidActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
TextView txtView = new TextView(this);
txtView.setText("Hello Android!");
setContentView(txtView);
}
}
Save the file, and launch the application.

The Activity was compiled, but the Android Package Manager doesn’t know what the entry point of our application is. We define the entry point with an intent filter in the AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.stanford.cs231m"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="HelloAndroidActivity" android:label="@string/app_name" android:screenOrientation="landscape">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
</application>
</manifest>
Launch the application again, what happened? The Application is not responding, which means it has crashed. We will launch now with the debugger to see if we can get further insight on what is going on.


We didn’t call the onCreate method on the parent class. Let’s add that call and try again:
public class HelloAndroidActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
TextView txtView = new TextView(this);
txtView.setText("Hello Android!");
setContentView(txtView);
super.onCreate(savedInstanceState);
}
}
We will now create a set of layouts to arrange a pair of buttons and the text view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="TextView" />
<LinearLayout
android:layout_width="200dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
</LinearLayout>
</LinearLayout>
And modify the onCreate method to: 1. Add references to the elements in the layout 2. Add event handlers. 3. Print to the Android Log.
package edu.stanford.cs231m;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity {
protected static final String TAG = "HelloAndroid";
private TextView mMainText;
private Button mButton1;
private Button mButton2;
@Override
public void onCreate(Bundle settings) {
setContentView(R.layout.main_layout);
mMainText = (TextView) findViewById(R.id.txtMain);
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mMainText.setText("Hello Android!\n");
mButton1.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
mMainText.append("Button 1 was pressed!\n");
Log.i(TAG, "Button 1 was pressed");
}
});
mButton2.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
mMainText.append("Button 2 was pressed!\n");
Log.i(TAG, "Button 2 was pressed");
}
});
super.onCreate(settings);
}
}
First, we are going to create a method to create a log file. Pay attention to how we find where to create the file, and then print to the log the file location.
private BufferedWriter openLogFile()
{
File appExternalDir = new File( Environment.getExternalStorageDirectory(),
"HelloAndroid");
if ( !appExternalDir.exists() )
{
if ( appExternalDir.mkdirs() )
{
Log.i(TAG, "External storage directory created: " + appExternalDir.toString() );
}
else
{
Log.e(TAG, "Failed to create directory " + appExternalDir.toString() );
return null;
}
}
File logFile = new File( appExternalDir, "log.txt");
BufferedWriter writer = null;
try {
writer = new BufferedWriter( new FileWriter(logFile));
Log.i(TAG, "Created log file" + logFile);
} catch (IOException e) {
Log.e(TAG, "Failed to create file " + logFile.toString() );
return null;
}
return writer;
}
Now we are going to add a method to print to the log file
private void logMessage( String message )
{
if ( mLogWriter != null )
{
try {
mLogWriter.write(message);
mLogWriter.newLine();
mLogWriter.flush();
} catch (IOException e) {
Log.e(TAG, "Failed to write to log file");
}
}
}
In the AndroidManifest.xml, you will need to request permission to access the external storage.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.stanford.cs231m"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="HelloAndroidActivity" android:label="@string/app_name" android:screenOrientation="landscape">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
</application>
</manifest>