Facebook

Understanding the android code

This is the second article in the series android application programming. If you haven't gone through the first article, I strongly recommend to do so.
In previous post we learned how to start developing android app in android studio and how to compile and run the application using android device.

For the sake of simplicity, we started with simple HelloWorld app which doesn't have any functionality so far and only displays text 'Hello World' on the screen.
In this article we will explain the source code which helped us display the text 'Hello World' on screen.

This code is generated by-default  by the android studio.

MainActivity.java

   1:  package mycompany.helloworld;
   2:   
   3:  import android.support.v7.app.AppCompatActivity;
   4:  import android.os.Bundle;
   5:  import android.view.Menu;
   6:  import android.view.MenuItem;
   7:   
   8:  public class MainActivity extends AppCompatActivity {
   9:   
  10:      @Override
  11:      protected void onCreate(Bundle savedInstanceState) {
  12:          super.onCreate(savedInstanceState);
  13:          setContentView(R.layout.activity_main);
  14:      }
  15:   
  16:      @Override
  17:      public boolean onCreateOptionsMenu(Menu menu) {
  18:          // Inflate the menu; this adds items to the action bar if it is present.
  19:          getMenuInflater().inflate(R.menu.menu_main, menu);
  20:          return true;
  21:      }
  22:   
  23:      @Override
  24:      public boolean onOptionsItemSelected(MenuItem item) {
  25:          // Handle action bar item clicks here. The action bar will
  26:          // automatically handle clicks on the Home/Up button, so long
  27:          // as you specify a parent activity in AndroidManifest.xml.
  28:          int id = item.getItemId();
  29:   
  30:          //noinspection SimplifiableIfStatement
  31:          if (id == R.id.action_settings) {
  32:              return true;
  33:          }
  34:   
  35:          return super.onOptionsItemSelected(item);
  36:      }
  37:  }

Lets go line by line and see what is achieved by that part of code.

Line 1 : This is the fully qualified name of package where your source files are kept.

Line 3-6 : These are the import statements. Whenever you use something new in your source code, studio gives warning that this class or variable can not be found and suggests to import the relevant class or package. Pressing the alt+enter after bringing cursor on text highlighted in red imports the required package automatically.

Line 8 : Your activity class starts from here. Class override 3 default methods.

Line 10-14 : onCreate is the method which is called when an activity is loaded. We will discuss the activity lifecycle in detail in upcoming articles.

Line 16-21 :  onCreateOptionMenu method is used to create the menu options like settings etc.

Line 23-36 :  onOptionItemSelected method handle the code related to menu item click. For example what activity to call or what code to execute when a menu item is clicked.

Example menu item in the figure below. Like 'settings' there might be other menu items.


This is the screenshot of my first app. This is a simple android app named as 'All In One Utility Offline'. It it include some commonly used converters ans some not-to-be-found-anywhere-else utilities like flip coin, roll a dice, weight on other planets etc. One feature I implemented in this app is adaptive UI. Most heavily used utility will be on the top of list.
Please give it a try.

In the next article we will create activity with more fields, accessing data from those fields and displaying that data.


1 comment:

  1. I'm using some your hints in my work under an android app for secure dataroom . Very useful! Thanks!

    ReplyDelete