Search code examples
javaandroidxmlprojects-and-solutions

Finding the Right Files to Build Android Project in Classic Online Tutorial


I am having problems with Week 7 of this tutorial, https://web.stanford.edu/class/cs193a/lectures.shtml. On Week 7, There is only the .java files showing which was problematic when I ran the build on Android studio.

Do I need an xml file or not? Why could they not have been shown as in the earlier weeks the xml files were shown as well.

I opened the zip file for the Targets app and found in Targets/app/src/main/res/layout the following xml file,

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".TargetsActivity">

    <com.example.stepp.targets.TargetsView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

but when I copy it into the project where I pasted the .java files, ExampleView.java and TargetsView.java,

 /*
 * CS 193A, Winter 2015, Marty Stepp
 * This class is a graphical view of a basic example of 2D graphics.
 */

package com.example.stepp.targets;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;

public class ExampleView extends View {
    public ExampleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /*
     * This method draws some shapes and text on the view.
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawARGB(255, 255, 90, 90);

        Paint aqua = new Paint();
        aqua.setARGB(255, 0, 80, 220);

        canvas.drawRect(new RectF(10, 30, 300, 700), aqua);
        canvas.drawOval(new RectF(400, 50, getWidth(), getHeight()), aqua);

        Paint font = new Paint();
        font.setARGB(255, 0, 0, 0);
        font.setTypeface(Typeface.create(Typeface.SERIF, Typeface.BOLD_ITALIC));
        font.setTextSize(40);

        canvas.drawText("CS 193A is great", 80, 200, font);
    }
}

and

    /*
 * CS 193A, Winter 2015, Marty Stepp
 * This class is a graphical view of a drawing of a red/white target figure.
 */

package com.example.stepp.targets;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;

public class TargetsView extends View {
    public TargetsView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /*
     * This method draws the target oval shapes on the view.
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint red = new Paint();
        red.setARGB(255, 255, 0, 0);

        Paint white = new Paint();
        white.setARGB(255, 255, 255, 255);

        int w = getWidth();
        int h = getHeight();

        for (int i = 0; i < 5; i++) {
            canvas.drawOval(new RectF(w*i/10, h*i/10, w*(10-i)/10, h*(10-i)/10), (i % 2 == 0 ? red : white));
        }
    }
}

I dont see why this doesnt work because the same method working in the early weeks. Help is much appreciated. Nor can I see why there is a need for two different activities ExampleView and TargetsView.

I am having the same problem in the other latter weeks of this tutorial as I cannot see where to find the correct java and xml files.

This problems forms what seems to be a common error for those following this prominent tutorials on android app development


Solution

  • Try use the whole Targets.zip.

    The two classes you included in the question are class extends View. They are just UI components that will not make anything happen by themselves.

    These two classes can be included into a layout xml or used (constructed programmatically) by Activity class. As you can see in the layout xml you found, it includes a tag called <com.example.stepp.targets.TargetsView ... />, which means this layout will have a TargetView inside.

    An Activity class is actually the one who coordinating UI components and logics. In your Targets.zip, you have a TargetsActivity.java as well, which is a Acitivity class. It tries to fetch R.layout.activity_targets and set it as content view by calling setContentView(...) in the onCreate() method.

    Finally you have AndroidManifest.xml at src/main. You can see in the <activity ...> ... </activity> node, TargetsActivity is specified as the launcher activity, which means when the app starts, this Acitivity class will be the first activity to start.