featured, Technology

My first Android app

Hello all

Today I finally started some Android development. I’m not a Java developer and have never done mobile development before but I was keen to get started. So as usual, Hello World was the first app I had to build.

I started off with AppInventor, which I found was great for drag-n-drop GUI and programming. This was great to start off with and get familiar with some of the GUI components. However, I wanted to see and write code. So I headed over to http://developer.android.com and clicked on getting started. To be honest I skipped the “What is Android?” section and moved to “Application Fundamentals”.

Android SDK installed, Eclipse configured, Android Virtual Device setup and we are GO. New Android Project created and compiled. A blank screen 🙂 . It was time to add a couple of UI controls a TextView and CheckBox. I could have used the friendly UI to drag components onto the device “screen” but I thought I should go straight for XML.
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

A TextView followed by a CheckBox underneath. Switch to the Graphical View tab and we get a visual confirmation of these two controls. Simple stuff so far.

Now we want to change the text of the TextView. We can add android:text="caption" to the above XML but that’s static text and boring. Let’s go to the source code in our HelloWorldActivity.java file. The following two lines of java get the TextView control and sets its text property:

final TextView tv =(TextView) findViewById(R.id.textView1);
tv.setText("changed Text");

Similarly for the CheckBox:

CheckBox chkEnable = (CheckBox)findViewById(R.id.checkBox1);
chkEnable.setText("Enable");
chkEnable.setChecked(false);

We can compile and run this. You will see the “changed Text” at the top and right underneath it a check box.

Hello World

Beautiful.

The next step for me was to add some functionality to the Check Box. Here’s the code:

chkEnable.setOnClickListener(new OnClickListener() {

      	  @Override
      	  public void onClick(View v) {

      		if (((CheckBox) v).isChecked()) {
      			Intent in = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      			startActivity(in);
      			Toast.makeText(HelloWorldActivity.this,
           		 	   "Enable GPS" , Toast.LENGTH_SHORT).show();  			
      		}
      		else {
      			Intent in = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      			startActivity(in);
      			Toast.makeText(HelloWorldActivity.this, "Disable GPS", Toast.LENGTH_SHORT).show();

      		}

      	  }
      	});

Here we add a onClickListener to detect when the Check Box has been clicked/tapped. In this we have a onClick() method which defined the actions to take when the check box is clicked. Here I am opening the Location settings of Android and showing a toast notification. I should mention here what an “Intent” is. An Intent is a message for a specific activity,  service or broadcast. For Activities, this could be the action to perform, and for Broadcast this could be a message. Above I’m using an Intent to perform the action of starting the location settings Activity. I then use Toast.makeText to show a notification.

So far so good. This wasn’t a big challenge so I set out to add Google Maps. I already had the functionality to open Location settings so I thought this follows on nicely. This is where things get complicated. I won’t post the whole code here but I will link to the tutorials I followed.

[1] http://developer.android.com/guide/topics/location/index.html

[2] http://developer.android.com/resources/tutorials/views/hello-mapview.html

[3] http://developer.android.com/guide/topics/location/obtaining-user-location.html

These give a step by step tutorial to add Google Maps and also acquire a position fix using either network or GPS. So here’s one I made earlier:

Hello World with Maps, Notification and Location information

Here I have taken the location information and displayed it in my TextView. The little Android shows the current position and the Toast notification is triggered by checking the “Enable” check box.

You can get this apk here. This is for devices with Android 4.0 and above. I have only tested this on Samsung Galaxy Nexus and with a help of a friend on Samsung S II.

Disclaimer: This software is provided “as is” with no warranty. (I have always wanted a chance to write that 😛 ).

Standard