CS185c
Chris Pollett
Feb. 13, 2012
Which of the following is true?
To test state saving code you can just test how your app works when the screen orientation changes. In the emulator you can test this by hitting CTRL-F11.
Here are some key building blocks of an Android App:
<?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>
<?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" /> <ImageView android:id ="@+id/me_shoes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/both_on" /> <Button android:id ="@+id/button" android:text="Click Me" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package org.pollett.ViewExperiment; public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int button=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }
package org.pollett.ViewExperiment; import android.app.Activity; import android.os.Bundle; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; public class ViewExperiment extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Here is where we set up the button when the Activity starts View button = this.findViewById(R.id.button); button.setOnClickListener(this); } public void onClick(View v) /** Handles clicks events for this activity -- new code we added @param v - view that event is coming from */ { switch(v.getId()) { /* handle the case coming from thing on our Activity with id button */ case R.id.button: // launch the SecondView Activity Intent i = new Intent(this, SecondView.class); startActivity(i); break; } } }
package org.pollett.ViewExperiment;
import android.app.Activity;
import android.os.Bundle;
public class SecondView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// here we use a new layout that we will need to create
setContentView(R.layout.second);
}
}
<?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="second screen" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.pollett.ViewExperiment" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ViewExperiment" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondView" android:label="@string/app_name"> </activity> </application> <uses-sdk android:minSdkVersion="2" /> </manifest>