DBell
DBell

Reputation: 61

New developer - how to build some of the tutorial applications?

I successfully installed Eclipse, JDK, etc., and have a working IDE. (Windows 7)
Pretty easily worked through HelloAndroid, both hard-coded text based and xml resourced.
Located the .apk, loaded it onto my tablet, and it worked!

Now, trying to do something a little more extensive and interactive, so I am trying to build the TimePicker tutorial application. There seems to be a big jump in what the budding programmer is expected to just "know" to do!

Step 1 - creating a new project, fine.

Step 2 - "inserting" the xml resource code was pretty clear - it replaces all.

Step 3 - "inserting" the class members. Not so clear, but I finally did get them into the proper place in the java source after several tries, AND after recalling the Tip, to Ctrl-Shift-O to import packages. Without that, it naturally threw all sorts of errors, that I couldn't distinguish from inserting the code in the wrong place.

Step 4 - "inserting" code for the onCreate() method: I have NO idea where this goes or exactly what it may replace. Steps 5, 6, 7 don't look promising!

The code offered is protected void onCreate(); does that replace the default generated public void onCreate()? Does it go in in addition to it? Above? Below? Inside the closing }? After?

Needless to say, every place I've tried to put it, I only get more errors...

I don't really want or have the time resources to go take a java course! I've been programming for 40 years, and it's probably an "old dog / new tricks" issue. I'd really appreciate some guidance to get started on this learning curve!

Dave

Upvotes: 0

Views: 306

Answers (2)

J-Rou
J-Rou

Reputation: 2286

Before programming in Android you should know:

  • Java
  • Object Oriented Proggramming
  • XML in general (Most importantly, for the UI)

i'm writing down the tutorials steps in a way that might help you, If you follow them, you would have finished the tutorial, but you might not learn anything. I'd recommend you to start by reading a verry small java tutorial (that gets to object oriented proggraming in java).

After that, you will understand this much better.

This video is also a verry good video for getting started. Learn how to develop for Android, Beyond HelloWorld. (It's long, but i really recommend it, it will help you get going fast)

Then, i would recommend you to finish the layout tutorials (those are much easier and more usefull for basic programs). The Layout totorials are better for starting to learn. (at least do the linearLayout, RelativeLayout and ScrollView tutorials). After the video (in case you saw the video), some of this tutorials might seem easy.

After that, if you still don't understand this code, read a more complex java tutorial (or get to the interface part of the tutorial).

I know the video is long, but its a verry good way of getting started. It helped me A LOT.


Ok, I'm going to write a few things you should know in order to program in android. This things are explained in the HelloWorld tutorial, but in case you didn't understand them there, I'm writing them here.

In android, the different "Windows" are called Activities and are subclases of Activity. For example, an activity can be:

public class MyExampleActivity extends Activity
{

...

}

All activitis should be declared in the android manifest (Like you should have seen in the HelloAndroid example). The first Activity called in the aplication is the activity that has the tag: <category android:name="android.intent.category.LAUNCHER" />

For the rest of the activities you should change that tag for: <category android:name="android.intent.category.DEFAULT" />.

For example, in the manifest my first activity should be declared like:

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

All activities should override a method called onCreate. That method is the first method called when an activity starts.

So in all activities you should create a methjod like this:

public class MyExampleActivity extends Activity
{
    //The On Create Method - Is the first method called in an activity.
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //Executes the onCreate of the superclass (Activity)
        super.onCreate(savedInstanceState);

        //Adds the xml layout to the activity (In the first activity, the layout is usually
        //main, but in other activities you should replace main for the laout of that activity
        setContentView(R.layout.main);

        //Just to prevent mistakes, ALWAYS make the findViewById(R.id.xxx) calls after the
        // setContentView

        // (onCreate CODE SHOULD BE HERE)
    }
...

}

About the tutorial you selected, Its not the one I recommend right after the HeloWorld one, but if you want to finish it:

Step 4: Create the onCreate Method in the HelloTimePicker class (Or however you called it) and copy the code where i wrote (onCreate CODE SHOULD BE HERE).

at this point the class ahould look something like :

public class HelloTimePicker extends Activity
{

    private TextView mTimeDisplay;
    private Button mPickTime;

    private int mHour;
    private int mMinute;

    static final int TIME_DIALOG_ID = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // capture our View elements
        mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
        mPickTime = (Button) findViewById(R.id.pickTime);

        // add a click listener to the button
        mPickTime.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });

        // get the current time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        // display the current date
        updateDisplay();
    }

    //NEW METHODS GO HERE
}

Now you can see:

Class xx extends activity

Global variables (private TextView mTimeDisplay;, etc)

OnCreate metho with super.onCreate, setContentView and extra code for the TimePicker. Step 5 is adding two new methods to the t class:

// updates the time we display in the TextView
private void updateDisplay() {
    mTimeDisplay.setText(
        new StringBuilder()
                .append(pad(mHour)).append(":")
                .append(pad(mMinute)));
}

private static String pad(int c) {
    if (c >= 10)
        return String.valueOf(c);
    else
        return "0" + String.valueOf(c);
}

just cop paste this where i wrote (//NEW METHODS GO HERE).

Step 6 is the same as step 5, but with a listener (its like a method thatis called automatically when something happens). i would recommend reading about Listeners to understand this better.

You should just paste:

    // the callback received when the user "sets" the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
    new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mHour = hourOfDay;
            mMinute = minute;
            updateDisplay();
        }
    };

after the two methods and before the } at the end of the class.

Step 7 is like step 6 but with

    @Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_DIALOG_ID:
        return new TimePickerDialog(this,
                mTimeSetListener, mHour, mMinute, false);
    }
    return null;
}

.

tutorial is finished. You should have:

public class HelloTimePicker extends Activity
    {

        private TextView mTimeDisplay;
        private Button mPickTime;

        private int mHour;
        private int mMinute;

        static final int TIME_DIALOG_ID = 0;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // capture our View elements
            mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
            mPickTime = (Button) findViewById(R.id.pickTime);

            // add a click listener to the button
            mPickTime.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    showDialog(TIME_DIALOG_ID);
                }
            });

            // get the current time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            // display the current date
            updateDisplay();
        }

        // updates the time we display in the TextView
    private void updateDisplay() {
        mTimeDisplay.setText(
            new StringBuilder()
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)));
    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

 // the callback received when the user "sets" the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;
                updateDisplay();
            }
        };

        @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, mHour, mMinute, false);
        }
        return null;
    }

//Sorry for the indexing

Upvotes: 0

Richard
Richard

Reputation: 1590

Well I am not an Android developer but lets say I am in the very same situation as you are. I come from another background and want to get into Android.

This is what I would do as I suppose you know XML and programming languages that support OOP. I just guess you come from c++ or delphi...

I would first of all look for an android forum on the web for android development some kind of community that just deals with android.

Than I would see - because those boards usually have some kind of threads such as: Useful resources and BOOKs for android beginner.

Than I would have a look at amazon and check some reviews and compare some books.

I would just go from there with a book or two books. Does not really take time to scan those books. I am also sure they would simply show some basic Java syntax and programming excersises as most iPhone books have some C and C++ - to Objective-C introduction.

From there I would see how much those book cover and have a look at the "simple" adroid apps that are already outside and try to clone them for practice suppose.

And if there are any kind of Java specific questions just googling the sense of how to approach that in java and then just look for the class/method descriptions at: http://download.oracle.com/javase/tutorial/reallybigindex.html

I suppose with this way you could easily dive into Android within a month or two to cover all the basics and have some simple apps running?

Well as I said, I am not an Android developer and therefore cannot answer your other question but thats the way I would get into Android with a fast learning curve.

Upvotes: 4

Related Questions