CS75
Chris Pollett
Sep 15, 2014
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:
<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="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.secondproject.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
<?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_world" /> </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/my_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/myphoto" /> <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.androidtest; public final class R { ... //lots of junk we're skipping in ... public static final class id { ... public static final int button=0x7f05003d; ... } ... public static final class string { ... public static final int hello_world=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="8" android:targetSdkVersion="20" /> </manifest>