Android
This tutorial contains a detailed explanation of the Hello World application created in Android Tutorial #1.
MainActivity.java
package com.computerscience101.testmap; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
package com.computerscience101.testmap;
A 'package' in Java is used to organise classes in Java in a particular namespace, similar to how we arrange similar files in a folder.
package <package-name> denotes which package the file belongs to.
package <package-name> denotes which package the file belongs to.
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem;
The 'import' keyword in Java is used to import (include from external source) built-in and user-defined packages into your Java class. This allows you to access classes and methods from imported packages as if they are defined in the current package
Access specifiers: Access specifiers as its name suggests determine how a particular entity can be accessed i.e. whether it can be accessed from all classes, only the class to which the variable belongs, etc.
In this case an 'entity' could be a class, method, variable. etc.
In Java there are 4 types of access specifiers :
1. public
Can be accessed by all classes in the program, whether they are in the same package or in different packages.
2. private
Can be accessed only by the enclosing class.
3. protected
Entities made protected in super class can be accessed only from sub class. Super classes and subclasses can be better understood after understanding the concept of inheritence will be covered in the next part.
4. default
Java provides a default access specifier when no access specifier is specified. Entities with default access specifier can only be accessed by classes in the same package.
public class MainActivity extends AppCompatActivity {
Inheritance
Inheritance is the process by which one class gets the methods and properties (variables) of another class.
In this situation the receiving class is called the 'sub class' where as the class who's methods and variables will be acquired by the 'sub class' is called the 'super class'.
In Java the keyword 'extends' is used to show inheritence.
For eg.
Sub class extends super class {
The class MainActivity represents an Activity and hence it must contain the necessary methods and variables required by an Activity.
AppCompatActivity internally extends the Activity class.
Hence MainActivity acquires the methods and variables of the AppCompatActivity class which include the methods and variables of the Activity class.
AppCompatActivity is used since it allows you to set an ActionBar and a theme for the Activity.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }Overriding is a feature of OOP which is related to runtime-polymorphism. Polymorphism on a very simplistic level means the same name for many entities.
In this particular case polymorphism refers to methods. We override methods of the super class in the sub class by using the annotation @Override Which method is to be used depends upon the Object used for the class. The method in the sub class should have the same signature as that of the super class.
The signature of a method consists of:
Method Name + parameters
onCreate()
The onCreate() method is used to initialise the Activity. It comes first in the lifecycle of an Activity. Hence when an Activity is created first the onCreate() method is called.
super.onCreate() calls the onCreate() method of the super class.
onCreateOptionsMenu()
The onCreateOptionsMenu() method is used to initialise the contents of the Activity's standard options menu.
As shown above the options menu is enclosed within the red box.
The getMenuInflater().inflate(R.menu.menu_main); method is used to inflate the options menu.
This method returns true if the list is successfully inflated.
The file menu_main.xml contains the list of items to be inflated.
onOptionsItemSelected()
The onOptionsItemSelected() method is used to define what happens when each of the elements in the menu list is clicked.