Let's All Learn How to Develop an Android App, the Final Part

By Colin Polonowski on at

So here we are, tantalisingly close to realising our dreams of our first Gizmodo UK app. We've just a few final small steps to take before we get to see the app appear on our handsets, so let's crack on.

 

Saving Settings

Android has a variety of options for storing settings -- from popping things into a database to the more simple pairing of settings and values that are stored as 'Shared Preferences' -- it's the latter that we'll use to store the information on the Gizmodo UK feed location.

First, we need to pick up a click on the 'Settings' button so we can be shown the right view. To do this we use the setOnClickListener function:

final Button settingsbutton = (Button) findViewById(R.id.settings_button);
settingsbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.activity_settings);
}
});

The listener uses the onClick function to pick up the change in button status and update the app's view to show activity_settings; the screen we built a few weeks ago.

Now we need to add the code that sits behind this page. It's another class: SettingsActivity. We need to create methods to load and save preferences and then tie them all together.

package com.gizmodouk;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SettingsActivity extends Activity implements OnClickListener {
EditText rssurl;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
rssurl = (EditText) findViewById(R.id.rss_url);
b = (Button) findViewById(R.id.save_settings_button);
b.setOnClickListener(this);
loadPrefs();
}
private void loadPrefs() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String url = sp.getString("URL", "RssURL");
rssurl.setText(url);
}
private void savePrefs(String key, String value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
@Override
public void onClick(View v) {
savePrefs("URL", rssurl.getText().toString());
finish();
}
}

 

Testing and Bug Fixing

A key part of ANY development is to ensure you properly test the application. You can go about doing this in various different ways; there are countless different methodologies and much like coding, covering all of them would take a series of articles in and of itself. Given we're creating a pretty small app, it shouldn't take long to go over the functionality and test all of the possible things that can happen.

One useful way to test is to create use cases. These are mini scripts that replicate what a user might do. For example, a user might:

1. Load the app
2. Click the first item

Or a user might:

1. Load the app
2. Click the Settings button
3. Change the settings
4. Save the settings
5. Click any item

As you can imagine, even for a small app like this there may be half a dozen use cases. Spend some time using the app and noting them all down -- then as you make changes to the code you have a simple series of steps to follow to test as much functionality as possible. NO application is bug free, but we can definitely make a stab at making it as stable and functionally correct as possible.

Getting into good habits with testing at this stage will ensure you continue to create good quality code in the future.

Despite it actually working, the code we've created for this series has some intentional bugs and unexpected or non-working "features". You can use it as a basis to do some proper testing to identify as many of these problems as possible. Then, using what you've learnt to date, they should all be relatively straightforward to investigate and fix.

 

Running the Gizmodo UK App on Your Android Phone...

This is what we've been waiting for! The moment when we see that big blue G appear on our mobiles and seeing it actually functioning for REAL!

Your phone should be in Debug mode if you followed the instructions in our first tutorial, but you also need to locate and download the appropriate USB drivers for your device. So, pop over to the manufacturer's site and find the most up-to-date drivers you can and get them installed.

Once you have the drivers up and running, Eclipse will give you the option to run the application on your handset. Here's the app listed on my Galaxy Note II:

 

What's Next?

For me, a much needed rest! This has been as much a learning experience for me as it was for you too; until I started this series I'd never even touched on Android development, so learning how it all works and then sharing my findings with all of you has been a great experience.

For you, the fun is only just beginning. You can download all of the code HERE -- this is now yours to do with as you please. First I'd recommend fixing the features and bugs, and then why not have a go at making it better? This really is just a proof of concept prototype (it would get savaged on Google Play reviews), but we reckon the Giz readership could take this skeleton and turn it into something beautiful. Are any of you up for the challenge?

Colin Polonowski is a developer at Giz UK’s parent company Future Publishing, working across a number of their sites. He also runs The Digital Fix in his spare time. Check in on the 18th of April for his next App Millionaire guide.

To get creative guides, app tips and the full lowdown on Samsung’s S4, Note 8.0 and Note II, check out Samsung’s Your Mobile Life over here.