The first part had brief information about the Android history and installation. The second part of the Android guide will deal with the coding, good practices and Android versions

Activity life cycle Methods

1)onCreate()

Called when the activity is first created. This is where you should do all your normal static set up. This method also provides you with a Bundle containing the activity’s previously frozen state, if there was one.

@Override
protected void onCreate(Bundle savedInstanceState)
{              super.onCreate(savedInstanceState);
}


2)onStart()

Followed by onResume() if the activity is being created or onRestart() if it is being shown after previously being stopped.

@Override
protected void onStart()
{super.onStart();
}


3)onRestart()

Called after your activity has been stopped prior to it being resumed again. Always followed by

@Override
protected void onRestart()
{ super.onRestart();
}


4)onResume()

Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.

@Override
protected void onResume()
{ super.onResume();
}


5)onPause()

called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to the data, stop animations and other things that may be consuming cpu, etc.

@Override
protected void onPause()
{super.onPause();
}


6)onStop()

Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one.this may happen either because a new activity is being started,an existing one is being brought to front of this one, or this one is being destroyed.

@Override
protected void onStop()
{ super.onStop();
}


7)onDestroy()

The final call you receive before your activity is destroyed.

@Override
protected void onDestroy()
{ super.onDestroy();
}

Some important functions

  • setContentView() :An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(). This function sets the current view to main.xml file.
    Eg. setContentView(R.layout.main);
  • findViewById():Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle)
    Eg.Button btn1=(Button)findViewById(R.id.button1);
  • startActivity():This method is used to load a new Activity from current activity.
    Eg. startActivity(intent);
  • Intent():An Intent is exactly what it describes. It’s an “intention” to do an action. An Intent is basically a message to say you did or want something to happen. Depending on the intent, apps or the OS might be listening for it and will react accordingly.
    Eg. Intent intent=new Intent(this,Activity_name.class);
  • Toast():Toast is used to display a message to the user. It appears for certain amount of time and then disappears.
    Eg. Toast.makeText(this,”hello world”,Toast.Length_short).show();
  • Log.i():This function is used to trace the program with the help of a logcat file.Syntax:Log.i(“tag”,”values”);
    Eg.Log.i(“line 7”,”working”);

Example

Class structure for implementing progress bars

class progressbar extends AsyncTask<Params, Progress, Result>{
@Override
protected void onPreExecute(){
// All code which should be implemented before progress bar starts
}
@Override
protected Result doInBackground(Params… params){
//All code which should work in background
return null;
}
@Override
protected void onProgressUpdate(Progress[] values){
//code to calculate the progress and display the progress of progress bars
} @Override
protected void onPostExecute(Result result){
// All code which should be implemented after progress bar stops
}
}

After creating the class structure you just need to create a object of class and call the execute method.
Eg. new progressbar().execute();

Site to refer for android : developer.android.com

 

Good Practices in Android/My tips

  • Create a habit of placing the activity files, class files in packages. Avoid using default packages.
  • Prefix a <Activity_> for activity file, <Class_> for class file and <layout_> for every layout (xml) files to differentiate them from each.
  • Avoid running the project from layout (xml) files as you would end up in generating an .out.xml file which will cause an error. Press ctrl+F11 from the activity file and not from layout file.
  • Every time you create an activity file it has to be added in the manifest file. Else you’ll get an error.
  • Avoid manual handling of R.java file. Don’t ever manipulate it.
  • Before closing the emulator go to ddms tab and stop the system.process.
  • Use Log.i (“Tag”,”value”); in program and trace it in the logical file to debug your program.
  • Use dp while mentioning values in the xml files instead of using px ( eg. android:layout_width=”50dp”)..
  • When using a local server like wamp or xampp use http://10.0.2.2 to access it from emulator. http://localhost won’t work on emulator.
  • Keep a habit of cleaning the project from time to time.
  • Use Ctrl + space while programming so that eclipse can provide u a set of proposals which will make your programming faster, efficient and accurate.
  • Use select all text in eclipse and press Ctrl + I to indent your code automatically.
  • Ctrl + F11-Run, F11-Debug, Ctrl+F11/key 9 or key 7 of numpad (numlock off)-to change orientation of emulator to landscape or portrait.

Android versions

Android versions are well known by their tastier codename. Here are few with their images.

[slideshow]