1

I'm new to Android app programming and started to read a book, but it just said write this to do this.

1) I want to know what each line of code actually means and does... So could someone explain the most common bits of code, what they do for someone completely new to Android (and programming in general) programming.

2) Could you also explain what each line of the following starter code means and where I edit?

    package com.Vibris;

import android.app.Activity;
import android.os.Bundle;

public class VibrisActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

3) I know it's a stupid question but the book confused me so much... Could someone explain what activities are and what they do and how they act? How do activities make up the app? Do you have to start with a particular activity?

4) What is the BEST way to start with Android development... Because (for me) a book really isn't the way to go!

2
  • Please, first visit the Android developer portal. This contains everything you need.
    – kameny
    Aug 5, 2011 at 8:58
  • It does not answer my question.
    – Oliver
    Aug 5, 2011 at 9:00

3 Answers 3

1

1) No can do. There is no such thing as 'common code'. Figure out what you want to do, and then figure out how to.

2) It is just a normal Java class. The class extends Activity, to show the user some stuff. The super.onCreate(..) should always be called. The setContentView grabs an xml file and inflates it so there is actually something to see on the screen. The editing then happens below the setContentView, and ofcourse normal Java functions can be added.

3) An Activity is the basic building block in Android. If you only need one screen, you'll only need one Activity. In a phone app there could be an activity for dialing, one for showing contacts, etc.

4) Just try something! The best way to learn is "How can I do x", and then figure out how to. There is plenty of documentation and there are many examples that can help you out. Start with small steps. (Like, "How can I show the menu?" or "How can I make a button do something?")

1

1&2)

package com.Vibris;  // This is the name of your package/application
// The next two lines let you use the android libraries
import android.app.Activity;
import android.os.Bundle;

// next comes the Activity class which is interaction point between your app and  the user
public class VibrisActivity extends Activity {
/** Called when the activity is first created. */
@Override

    // next comes your method/function whihc is the workhorse of your app
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  // call the parent code for this method
        setContentView(R.layout.main);  // set the contentview to display to the user. The layout/main.xml should contain the layout for the interface
    }
}

3) An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows. See http://developer.android.com/guide/topics/fundamentals/activities.html

4) I would recommend the Dev Guide (http://developer.android.com/guide/index.html) and the Tutorials (http://developer.android.com/resources/browser.html?tag=tutorial) on the Android Developer's site. I am pretty new to Android development myself and they got me up and running very quickly.

0

First, I start with this tutorial that explain some bases:

Then, I follow the official Google documentation. Their "Developers Guide" is very nice! Hereby the URL:

1) Activity provides callback method. onCreate() is called when the activity is created.

2) setContentView(R.layout.main);

  • R is the Ressource class, automatically generated from the res/ directory.
  • R.layout.main matches res/layout<...>/main.xml file that define the views tree (ie the GUI)
  • setContentView set the root view for that activity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.