Tag Archives: Java

10 more open source Android apps which every developer must look into

Sometime back, I listed top 10 Open source Android apps, which every developer must look into. It’s been quite sometime since I wrote it and now I am adding a few more to the list.

Ubuntu One

Ubuntu One is a cloud storage service by Ubuntu. They have released an Android app which allows you to upload files to the cloud from your mobile and also manage your Ubuntu One account from your phone.

You can learn how to upload files, make Rest API calls, store auth tokens, detect WIFI activation etc by browsing the source code.

Source code Url: https://code.launchpad.net/ubuntuone-android-files

Mixare

Mixare is an open source Augmented Reality Engine for Android. It is pretty well build and complete.

You will learn how to draw custom shapes on top of the live video feed captures by the camera by browsing the source code. You can also learn about creating Plugin models in Android apps.

Source code Url: http://code.google.com/p/mixare/

Desksms

DeskSMS is an android app that forwards all your text messages and call history to your GTalk, GMail, or a convenient website and browser extension. It written by koushikdutta.com, the author of Rom Manager

You can learn how to read, send and forward sms and call log by browsing the source code. In addition to the android app source code, checkout the other browser extension source code as well.

Source code Url: https://github.com/ClockworkMod/DeskSMS

Call Meter NG

Call Meter NG/3G is an Android application summing up your call and text logs.

You can learn how to read call logs, SMS logs, MMS logs and data logs by browsing the source code. In addition to it, you can also learn how to create variable length widgets, which is one of the cool things about the app.

Source code Url: http://code.google.com/p/callmeter/

Scrobble Droid

Scrobble Droid is an Android app, that keeps track of music you listen and sends the details to Last.fm

You can learn how to track music that is currently playing in Android by browsing the source code. In addition to it, you can also learn how to consume Last.fm API from ndroid.

Source code Url: http://code.google.com/p/scrobbledroid/

Gmote

Gmote is an Android app that turns your Android phone into a remote control for a computer, allowing users to run movies and music at a distance.

You can learn how to transfer touch and other gestures from phone to computer and also about music streaming between phone and computer by browsing the source code.

Source code Url: http://code.google.com/p/gmote/

Ema Personal Wiki

Ema Personal Wiki is a notebook with linkable pages for tracking your ideas, todo lists, tasks, bookmarks, brainstorms etc. The notes are stored in markdown format and can be synchronized using Dropbox account.

You can learn about using markdown and syncing files using Dropbox Api by browsing the source code.

Source code Url: https://github.com/janwillemb/Ema-Personal-Wiki

Chrome to Phone

Google Chrome to Phone Extension is a project consisting of a Chrome Extension, Android App, and supporting AppEngine server that enables users to send links from their Chrome desktop browser to their Android device using Android’s Cloud to Device Messaging service.

You will learn about using C2DM and sharing url from browser by browsing the source code.

Source code Url: http://code.google.com/p/chrometophone/

Android Tetris

It is a Tetris clone for Android.

You will learn about drawing on the canvas by browsing the source code.

Source code Url: http://code.google.com/p/androidtetris/

NFC Tic Tac Toe

This is a sample Tic Tac Toe game implemented using NFC.

You will learn about transfering data using NFC by browsing the source code.

Source code Url: https://github.com/iBadrinath/NFC-TicTacToe

Posted in Android/Java | Tagged , , , | 18 Comments

Conditional code compilation in Java

In C/C++ there is a construct which allows you to conditionally compile code. Yes I am talking about the #ifdef... #endif Java doesn’t have an equivalent. But you can use the following clever trick to achieve the same functionality.

Why conditionally compile code?

So before we proceed, you might ask me, why we need to do conditionally compiling? There are couple of reasons, but the main reason which motivated me to look for the solution is to conditionally enable debug statements. You can say that we can probably use a boolean variable or a function call, to determine whether we need to output the debug statement or not. But if the number of debug statements is high then they could add up.

Setting up final boolean variable

So the trick is simple. You have to create a if statement with a boolean private variable and if you  make the boolean private variable as final and set the value to false, then at compile time, the compiler will be able to determine that these code branches are unreachable and will not include them as part of the compiled code.

Sample Code

The following sample code explains this trick.

Posted in Android/Java | Tagged , | 5 Comments

Right Click on JComboBox [Java Swing component]

Recently I was writing a small UI tool in Java Swing at work and found a strange bug in Java Swing, which was first reported in 1998 but is not fixed yet. I thought of documenting the workaround here so that it is useful for others who are faced with the same problem.

I wanted to trigger a popup menu when a right click event happens on a JComboBox (drop-down) component. I tried adding a MouseListener to the JComboBox, but it was not getting triggered. After spending a couple of hours, I finally came to know that it was because of a bug in Java Swing. The worst part is that the bug was closed as “Will not fix”

Fortunately, there is a workaround for this. Instead of adding the MouseListener to the JComboBox, you have to add it to all its children, like how it is done in the following code snippet.

After implementing this fix, I was able to trigger the popup when the combo box was right-clicked. Hope this helps someone who is affected by this bug.

Posted in Windows/.NET | Tagged , | 4 Comments

Developing Android Apps – Part 2 – Week one – My notes

This week, I was not able to attend the full class due to time constraint, since I returned from a vacation on Tuesday. So my notes will not be as detailed as it used to be. I will try to provide detailed notes next week.

Getting authenticated with oAuth

So basically this week, Tony explained how to setup oAuth authentication, so that we can use the Twitter API without explicitly asking for password from the users.

In order to set it up, we have to first register for an app from Twitter. You can follow the screencast at the end of last week’s notes to do that.

Twitter4J

The next step is to include the Twitter4J jar. You can download it from the project homepage and then you have to copy it to the /lib directory of your android project. If you are using windows, then you can follow the steps given in this forum post to add Twitter4J to your android project.

Source code

You can access the source code for this week’s class from the official github repository.

See you all next week with detailed notes 🙂

Posted in Android/Java | Tagged , , | 5 Comments

Developing Android Apps – Part 2 – Overview – My notes

As I mentioned before, Creative tech is conducting part 2 of the Developing Android App class. Like Part 1, I am planning to post my notes here so that it would be helpful for others too.

Introduction

This week, we had the Overview session by Tony (the presenter). The training started off with a brief explanation of the concepts that were covered in Part 1. Tony then explained briefly about Twitter and the explained what are the things that will be covered in the next 5 classes.

The following is the schedule for the next 5 weeks.

Week one

  • Getting Authenticated with Twitter
  • oAuth
  • Twitter4J
  • Webview and webview Client

Week two

  • Advanced ListView
  • List headers and footers
  • Concurrent Programming with Threads
  • Handlers

Week Three

  • Tweeting from App
  • Creating menus
  • Using AsynTask to run many Concurrent tasks
  • Posting tweets

Week Four

  • Adding style to the App
  • Themes
  • Styles
  • Selectors and XML Graphics
  • Designing for multiple screen dimensions

Week Five

  • Posting photos from the app
  • Getting photos from the library
  • Getting photos from the Camera
  • Posting photos with twitpic4J

Homework

The homework for this week is to register an application and get authentication keys to use oAuth with Twitter. This has to be done before the next week class so that you can use to try next week’s code.

Tony has created the following screencast which explains the process of registering your application with Twitter.

There are lot of really interesting things lined up and I am very excited about this course. See you next week 🙂

You can also subscribe to my blog’s RSS feed or follow me in Twitter to receive updates about my notes for the next sessions.

Posted in Android/Java | Tagged , , | 9 Comments

Free online course on developing android applications using Java – Part 2

CreativeLive who conducted a free 6 weeks course on developing Android applications with Java are back again with a follow up course. The part 2 course is also a 6 weeks course (like part 1) and will be conducted by Tony Hillerson, who also conduced the first course.

In this course, a Twitter client would be created and will cover OAuth, access the Twitter APIs, and also how to post photos to Twitter amount other things.

The part 2 of this course is also free to watch live, like the first course. You can check out the schedule in the official page.

If you have attended the first course or if you are interested in Android, then do register for the course, at the official page and follow it up. 🙂

Posted in Android/Java | Tagged , , | 4 Comments

10 Open Source Android Apps which every Android developer must look into

I used to read code from popular open source projects to see how others implement certain functionalities and also to learn from them. (I am a firm believer of the fact that you have to read good code to write good code)

Recently I have been following up a couple of good open source Android apps and thought of listing them here so that it could be useful for others.

Update: I have updated this list with a new set of apps, which have come out recently. Check out them as well.

Sample Apps by Android Team

Could there be a better way to start without looking at the code of the developers who developed the framework? 😉 These are 15 different android sample apps created by the core developers of the Android framework. These include a couple of games, photostream, time display, home screen shortcuts etc.

url : http://code.google.com/p/apps-for-android/

Remote Droid

RemoteDroid is an android app which turns your phone into a wireless keyboard and mouse with touchpad, using your own wireless network. You can learn lot of things like connecting to a network, controlling user finger movement etc from its source.

url: http://code.google.com/p/remotedroid/

TorProxy and Shadow

TorProxy is an implementation of Tor for Android mobiles. Together with Shadow, it allows you to browse website anonymously from your mobile phone. You can learn about tunnelling socket connections, managing cookies etc by reading its source code.

url: http://www.cl.cam.ac.uk/research/dtg/code/svn/android-tor/ and http://www.cl.cam.ac.uk/research/dtg/android/tor/

Android SMSPopup

It is an Android app that intercepts incoming text messages and displays them in a popup window. Apart from being a time saver, this app also shows us how to interface with the built-in app that manages SMS.

url: http://code.google.com/p/android-smspopup/

Standup Timer

Standup Timer is an Android application that acts as a simple, stand-up meeting stop watch. It can be used to ensure your stand-up meeting finishes on time, and gives all the participants an equal share of time to state their progress. You can learn how to use the timer functionality by reading the source code. Also this apps has clear distinction between view, model etc and has lot of util methods which we can reuse in our app.

url: http://github.com/jwood/standup-timer

Foursquare

It is a four square client for android. This app is basically divided into two components; Reading the source code you can find out how to make

url: http://code.google.com/p/foursquared/

Pedometer

The pedometer app tries to take the number of steps you take every day. Even though the count is not accurate, you can learn different things like interacting with accelerometer, doing voice updates, running background services etc by reading its source code.

url: http://code.google.com/p/pedometer/

opensudoku-android

OpenSudoku is a simple open source sudoku game. You can learn how to display things in a grid in your view and also how to interact with a website by reading its source code.

url: http://code.google.com/p/opensudoku-android/

ConnectBot

ConnectBot is a Secure Shell client for the Android platform. There are lot of good things about this app’s source code. Check it out for yourself 🙂

http://code.google.com/p/connectbot/

WordPress for Android

How can you expect a list of apps from me without mentioning WordPress 😉 This android app is from the official WordPress development team. You can learn how to make XMLRPC calls (in addition to other cool things) by reading its source code.

url: http://android.svn.wordpress.org/trunk/

If you got any good open source android apps from which we can learn something, then do leave a comment and I will add them up here, till then happy reading 🙂

Update: I have updated this list with a new set of apps, which have come out recently. Check out them as well.

Posted in Android/Java | Tagged , , | 118 Comments

Developing Android applications in Java – Session 3 – My notes

This week in the Android course, Tony taught about the storing and retrieving information from database. Android has a bundled SQLite database and your app can store and retrieve information by creating a new database. The database that is created by an application is available only to that application and no other application can access it.

SQLiteOpenHelper

Android SDK provides a class called SQLiteOpenHelper which can be used for interfacing with this SQLite database that is associated with your application.

SQLiteOpenHelper has two methods which can be used for creating/updating the database. They are the following.

onCreate

The onCreate() method gets called when the app gets installed for the first time. The SQL code to create the database should go in this method. In addition to the SQL code we should also specify a version number for the database which will be used subsequently during upgrades.

onUpgrade

The onUpgrade() method gets called when the app is upgraded or if the version number specified in the app is greater than the one which is present in the database. Typically this function contains Alter table SQL code which will be used to upgrade the database.

In addition to the above two methods, the SQLiteOpenHelper also has other methods which can be used to access the database. One such method is getWritableDatabase()

getWritableDatabase

The getWritableDatabase() method will return a SQLiteDatabase object which has reference to the database. You can read more about this method from android documentation.

In addition to these methods, the SQLiteOpenHelper class other methods but the above there are the notable ones. You can read more about the SQLiteOpenHelper class from the android documentation.

Selecting data from the database

To selected data from the database, we have to call the query() method on the SQLiteDatabase object which is returned by the getWritableDatabase() method above.

The query() method returns an object of type Cursor, which can be iterated over to retrieve the resultset. The following code snippet explains the query() method.

private void loadTasks() {
	currentTasks = new ArrayList<Task>();
	Cursor tasksCursor = database.query(TASKS_TABLE,
			new String[] {TASK_ID, TASK_NAME, TASK_COMPLETE},
			null, null, null, null, String.format("%s,%s", TASK_COMPLETE, TASK_NAME));

	tasksCursor.moveToFirst();
	Task t;
	if (! tasksCursor.isAfterLast()) {
		do {
			int id = tasksCursor.getInt(0);
			String name = tasksCursor.getString(1);
			String boolValue = tasksCursor.getString(2);
			boolean complete = Boolean.parseBoolean(boolValue);
			t = new Task(name);
			t.setId(id);
			t.setComplete(complete);
			currentTasks.add(t);
		} while(tasksCursor.moveToNext());
	}
}

Inserting data into the database

In order to insert the data into the database we have to call the insert() method of the SQLiteDatabase. The data that needs to be inserted should be added to a ContextValues object and then passed to the insert() method. The ContextValues object is like a HashMap which contains the key and the value for each column of the row that will be inserted.

The insert() method returns the id of the row that was inserted. The following code snippet explains the insert() method.

public void addTask(Task t) {
	ContentValues values = new ContentValues();
	values.put(TASK_NAME, t.getName());
	values.put(TASK_COMPLETE, Boolean.toString(t.isComplete()));

	t.setId(database.insert(TASKS_TABLE, null, values));
	currentTasks.add(t);
}

Updating data in the database

To update data in the database we have to call the update() method of the SQLiteDatabase object. Like the insert() method, the data that needs to be updated should be passed in a ContextValues object.

The following code snippet explains the update() method.

public void saveTask(Task t) {

	ContentValues values = new ContentValues();
	values.put(TASK_NAME, t.getName());
	values.put(TASK_COMPLETE, Boolean.toString(t.isComplete()));

	long id = t.getId();
	String where = String.format("%s = %d", TASK_ID, id);
	database.update(TASKS_TABLE, values, where, null);
}

Deleting data from the database

To delete data from the database we have to call (guess what 😉 ) the delete() method of the SQLiteDatabase objet. The delete() method takes the where condition based on which the rows will be deleted.

public void deleteTasks(Long[] ids) {
	StringBuffer idList = new StringBuffer();
	for (int i =0; i< ids.length; i++) {
		idList.append(ids[i]);
		if (i < ids.length -1 ) {
			idList.append(",");
		}
	}

	String where = String.format("%s in (%s)", TASK_ID, idList);
	database.delete(TASKS_TABLE, where, null);
}

You can read more about this method from android documentation.

Demo App Sourcecode

The demo TaskManager app that we have been using in the previous classes was modified to store the tasks in the database. You can find the source code from my github page. I am also working in this week’s homework and will be posting the explanation and source code once I am done.

You can also subscribe to my blog’s RSS feed or follow me in Twitter to receive updates about my notes for the next sessions.

Posted in Android/Java | Tagged , , , , | 7 Comments

Using ArrayAdapter and ListView in Android Applications

This week’s homework in the android class was to create a simple ListView using ArrayAdapter instead of generic ListAdapter.

ArrayAdapter

ArrayAdapter is a special kind of ListAdapter which supplies data to ListView. You can refer to my notes for last week to know about ListView and ListAdapter. You can also read about ArrayAdapter in android documentation.

Adding views

First create an empty android project. Then edit the main.xml layout file to add a ListView. Then create another layout xml file which will contain the TextView (or any component) that will be displayed within the ListView.

Editing Activity

The next step is to change the generated activity class to extend from ListActivity. This is very important because only a ListActivity will be able to display the ListView.

Binding the adapter

The next step is to bind the ArrayAdapter to the ListActivity. We can do this by calling the setListAdapter() method.

To this method we have to pass an object of type ArrayAdapter. You can pass an object reference to this method or we can even create a new anonymous method like below.

We have to override the getView() method of the ArrayAdapter class to create the TextView (or any other component) which will be created for each list.

Finishing up

So that’s it, you are done. 🙂 All you have to do now is to save the project and run it in the emulator. If everything is done properly, then you can see the list of items displayed in the ListView like below.

android-listview

Source code

I have uploaded the entire project source code into github and you download it from there and verify it with your code.

Try to complete the homework, before the next session and do come back to view the notes and the homework for the next session too. 🙂

You can also subscribe to my blog’s RSS feed or follow me in Twitter to receive updates about my notes for the next sessions.

Posted in Android/Java | Tagged , , | 34 Comments

Developing Android applications in Java – Session 2 – My notes

Like last week, I attended the session on Developing Android applications in Java by Creative Techs and O’Reilly and here are my notes which I took during the session.

Replace TextView with ListView

In this week’s session, the demo app that was created last week was modified to use the ListView element instead of TextView.

The ListView provides a nice UI for displaying the list of tasks together with a checkbox to indicate whether they were complete or not.

Screenshot

You can see the new UI of the application in the following screenshots.

android-listview

ListView and ListAdapter

ListView is a control which can be used for creating list of scrollable items. The data to the ListView will be provided by ListAdapter.

You can think of ListView as the “view” component in a MVC framework and ListAdapter as the “model”

The class which is going to act as the ListAdapater should implement following methods

  • getCount()
  • getItem()
  • getItemId()
  • getView()

You can read more about ListView and ListAdapter from android documentation.

Adding ListView

To add ListView to any activity, we have to include the <ListView /> tag to the activity’s layout xml. In our sample app, the ListView tag is added to the main.xml file.

The Activity class that uses ListView should implement the ListActivity instead of plain Activity class. In our sample app, the ViewTasksActivity class is derived from the ListActivity class.

Magic ids

Android SDK provides some predefined ids which can be used some specific purposes. One such “magic id” is android:empty.

We can assign this to any element that we want to be displayed when the ListView is empty.

Homework

This week’s homework is to Build a simpler (but less flexible) way to load data into a list using an ArrayAdapter (instead of a BaseAdapter). I would be completing the homework and would be posting the explanation and the source code later this week. So stay tuned 🙂

Source code

I have uploaded source code for yesterday’s session in github and you can download it from there. I am also working in this week’s homework and will be posting the explanation and source code once I am done.

You can also subscribe to my blog’s RSS feed or follow me in Twitter to receive updates about my notes for the next sessions.

Posted in Android/Java | Tagged , , | 8 Comments