AndroidAppDev: Codebits Talk Proposals

If you remember my last post I’ve started coding an app to display the talk proposals for this year’s Codebits event.

I’ve finished version 0.1 of the app, which allows you to view the list of proposed talks with title, speaker and number of votes. By clicking in a talk in the list you can see it’s description.

For the next version, which will be available soon, I’m thinking about adding two features:

  • Store the talks in the SD card to avoid always downloading the list (which is big)
  • Add an option to refresh the list, in case you’re using a Wi-Fi connection
  • Be able to vote for the talks

Feel free to download the app here: Codebits Talk Proposals.
Source code is available here under a GPLv3 license.

AndroidAppDev: Codebits

I came across a problem that I want to solve. There is “geek” event called Codebits that has some talk proposals. Since I want to comment/vote for the talk proposals I need a computer with a browser, but reading all those abstracts takes time and sometimes I find myself with no time, or more often, not wanting to “waste” time on that. What if I could solve this problem my moving this to a smartphone? Of course I could use the phone’s browser, but since I want to learn using JSON from Android, I think it would be a good programming problem to solve.

Source code is available here: https://github.com/hmrocha/Codebits

AndroidAppDev: Days between two dates in Java

One problem that I came across while coding my Codebits Countdown widget was calculate the number of days between two dates. I’m sure there are a lot of solutions out there but since there is no method in the standard library to do this, I came across this simple solution:

public static int daysBetween(Calendar start, Calendar end) {
  int result = 0;
  Calendar c = (Calendar) start.clone();
  while (c.before(end)) {
    c.add(Calendar.DAY_OF_MONTH, 1);
    result++;
  }
  return result;
}

AndroidAppDev: PiMenta 0.4 Released

I’ve never added my PiMenta widget here at AndroidAppDev because I thought it wasn’t worth it due to the few people that actually use this widget, but anyway, here it is.

For the last couple of months that version 0.3 of my PiMenta widget was not working. For those who don’t know, this is a widget that displays on the home screen of your Android smartphone, the lunch menu of the cafeteria of the University of Lisbon. Since that cafeteria is now closed for vacations, it stopped working.

Yesterday a colleague of mine told me that he used my widget and that it stopped working. So, since there is someone actually complaining about it, I took the time to fix it and, since the only open cafeteria in August in at my faculty, I’ve changed the widget to display the lunch menu of that cafeteria.

I’ll update the widget again in September when all cafeterias of the university re-open.

 

AndroidAppDev: Radio Zero 0.1

Yesterday I released a new application for AndroidAppDev.

Since I’m a DJ for Radio Zero, I tried to create a simple application that would allow me to listen to this radio in my Android smartphone.

I suppose I won’t use this app most of the time because I’m normally behind a computer and obviously, draining battery by listening on the phone instead of listening with the headphones connected to the computer doesn’t make sense.

The main purpose of creating this app was to learn more about Android application development so that I could write a tutorial on how to create this kind of applications because that’s the main purpose of this website.

I’ve licensed the app with the open source GPLv3 license and I’m preparing a tutorial to explain how the app works.

I’ll create an “Apps” page on this blog to post the app here.

 

AndroidAppDev: Battery Widget 0.5

Today I’ve launched version 0.5 of my Android Battery Widget here at AndroidAppDev.

I haven’t been updating the blog because sometimes I have no time to do so but I’ll try to catch up soon.

I’ve learned a lot in this version of the widget and I’ll try to find time to write a complete tutorial on the subjects that I’ve learned. The main subject I learned was coding configuration activities for widgets, which is something really useful for all widgets.

I haven’t released the source code for this version yet but I’ll do so soon.

Here is a screenshot of the new version. Much prettier than version 0.4.1.

Battery Widget Screenshot

In this version that are a lot of visual improvements.

The widget has dots that change color accordingly to battery level and status. If it’s charging, dots change to blue, otherwise they are green. If battery level drops below 30, they change to orange and to red if it drops below 15.

I’m hoping that you prefer this version in comparison to the previous one.

Please let me know what you think.

You can download this version of the widget at the Android Market here.

AndroidAppDev at Bazaar

In a previous post that I just deleted because it’s no longer true, I wrote that I was going to upload my widgets to the apps repository of Bazaar. But one thing that I really need is statistics and Bazaar doesn’t provide me with statistics on the apps repository. What I decided to do is create a personal repository for androidappdev.org called androidappdev.bazaarandroid.com and upload my widgets there. If anyone cares, they can easily add that repository to their Aptoide client and install my widgets. I will no longer upload to the official apps repository.

I’ve uploaded my Battery Widget and my Wi-Fi Widget 20 minutes ago and I have 0 downloads. I’m curious to see how many downloads I get in a month.

 

AndroidAppDev: WiFi Widget PRO 0.1

Today I’ve release a new widget and I’m sharing this with you here at AndroidAppDev.

Here is the Android Market link for this app: Wi-Fi Widget PRO

This time, a donate version for my free/open source WiFi Widget. If you use my free widget, feel free do donate to this “PRO” version to help me develop it.

This week I’ll try to post a tutorial on how I developed this widget so that you can learn a bit of the Wi-Fi API. Since I’m obviously a user of this widget, yes, I believe in my own products, I noticed that there are some features missing. For example, be able to connect/disconnect Wi-Fi and list all networks available.

I’ll try to implement those features before I finish the tutorial so that you can clean how to implement those also.

I know that there are a lot of Wi-Fi widgets out there, but the purpose of this widget is to teach Android application development. Since I’m really new to Android development, learning to implement this widgets is really useful because it serves as a basis for more complex apps.

 

AndroidAppDev: Battery Widget 0.1

For my first tutorial here at AndroidAppDev I’d like to start with a very useful battery widget to display current battery level.

I’m a new Android developer and I’ve been suffering a lot while learning how to develop widgets for this platform.

In my opinion, the currently available tutorials are really bad, and most of the time they don’t explain stuff, they just say, “do this and it will work.”

Frustrated by the lack of good tutorials, I decided to started a collection of tutorials myself.

This will be, hopefully, a good place for you to look for hints on how to code a widget or application for the open Android platform.

All of the code that I’ll develop for my tutorials will be open source, so that you can learn how to develop your own widgets. All of my code will be using GPLv3 license. This means that you can use all of my code, change it, redistribute it, sell it, as long as you keep your changes open source also. For details about this license please read about it here. This approach guarantees that everyone will benefit back from your changes and that they can still learn from them.

I’ll be assuming that you’re already a Java developer and that you’re familiar with the XML language and the Eclipse development environment, and also that you already have the Android SDK installed and ready to use. Google has a really good tutorial here on how to set up the SDK so it doesn’t make sense to waste time writing another.

If you want to learn how to write the usual “Hello, world!” program, please checkout Google’s tutorial here.

Our application will be called Batteroid, and we’ll start with version 0.1. This will be a really basic widget with a very basic interface and your feedback to this tutorial will guide me to teach you how to make it better.

The goal of this application, in this case, a widget, will be to just display a simple number. The difference between this widget and a simple “Hello, world” widget is that we’re not going to hard code the number, we’re going to get the number from the Android platform. Also, we need to be notified every time the battery level changes so, besides being very simple graphically, it will have a lot of concepts that will be helpful in future developments.

One of the most important files in an Android application or widget is the AndroidManifest.xml file. This is where we declare most of the stuff that our widget will need or use. For example, this is where we define the version number of our widget, the widget icon, the name that will appear in the widget list and the events that we want to listen to.

The main difference between applications and widgets is that a widget is like a small application that stays on your home screen and waits for things to happen. Usually they don’t get much interaction, they just display stuff. In our case, the current battery level. A widget also doesn’t appear in the applications list. You have to press your home screen until a list of options appears and choose the Widgets option to see the list of available widgets. Its really sad to see people rating widgets 1 star in the Android Market just because they can’t run it because they’re trying to find it in the applications list.

In this sense, a widget is like a receiver of events that are broadcasted by other widgets, applications or services and get updated accordingly. A widget itself extends the AppWidgetProvider class which extends BroadcastReceiver, so you can see that the class hierarchy reflects well the behaviour of this widgets.

We need to declare our widget in the AndroidManifest.xml file so that when we install the apk on our Android phone, the system will be able to list our widget name in the widget list.

Widgets are declared with the <receiver> tag, inside the <application> tag. Inside <receiver> we will have to declare the events we want to listen to, called intents, and also the meta information about our widget, for example, widget dimensions and the update interval.

We have to declare which Java class will represent our widget and this is defined in the android:name attribute.

<receiver android:name=".BatteroidAppWidgetProvider">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
<meta-data android:name="android.appwidget.provider"
        android:resource="@xml/appwidget_info" />
</receiver>

The APPWIDGET_UPDATE intent is main intent for widgets and should always be declared. It is sent to your widget the first time you put the widget on your home screen and each time the update interval is reached (defined in the meta data xml, in this case the appwidget_info.xml file).

To create this appwidget_info.xml file you must create a folder called xml inside your res folder and create the xml file inside it. For this example it should look like this:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"   
    android:minWidth="72dip"   
    android:minHeight="72dip"   
    android:updatePeriodMillis="0"   
    android:initialLayout="@layout/main">
</appwidget-provider>

Lets have a look at the details of our BatteroidAppWidgetProvider class.

All Java classes that are supposed to represent widgets should extend the class AppWidgetProvider. From this class, we must override the onUpdate method to insert the functionality we want. In this case, show the battery level.

If we run the application with an empty class the code runs anyway. It opens the emulator, we press the home screen to insert a new widget. Our Batteroid shows up in the list and it shows “Hello World!” in the home screen.

Lets type in the code to register our intent to get the battery level on the onUpdate method, which we must override.

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
	int[] appWidgetIds) {

	context.getApplicationContext().registerReceiver(this,
		new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

	ComponentName cn =
		new ComponentName(context, BatteroidAppWidgetProvider.class);
	appWidgetManager.updateAppWidget(cn, this.views);
}

The crucial line of code here is

context.getApplicationContext().registerReceiver(this,
		new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

First of all, we can’t register the ACTION_BATTERY_CHANGED inside the manifest file because the documentation says:

You can not receive this through components declared in manifests, only by explicitly registering for it withContext.registerReceiver(). See ACTION_BATTERY_LOWACTION_BATTERY_OKAYACTION_POWER_CONNECTED, andACTION_POWER_DISCONNECTED for distinct battery-related broadcasts that are sent and can be received through manifest receivers.

Yes, I have lost a lot of time with that one, so I’m hoping that you won’t.

And, since registerReceiver is expecting a BroadcastReceiver and and AppWidgetProvider is already a BroadcastReceiver, you might be tempted to just write context.registerReceiver(this, new IntentFilter… but it won’t work either because you can’t register a receiver inside a BroadcastReceiver, that’s why you have to use the getApplicationContext method.

I have also lost A LOT of time on that one.

Let’s look now at our onReceive method:

@Override
public void onReceive(Context context, Intent intent) {
	String action = intent.getAction();

	if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
		Integer level = intent.getIntExtra("level", -1);
		this.views.setTextViewText(R.id.battery_level, level.toString());
		ComponentName cn =
			new ComponentName(context, BatteroidAppWidgetProvider.class);
		AppWidgetManager.getInstance(context).updateAppWidget(cn, this.views);
	}
	super.onReceive(context, intent);
}

The code is not complicated. We receive the intent and get the action associated with it. If the action is the action we’re expecting, in this case, an ACTION_BATTERY_CHANGED e get the integer extra stored inside the intent which has the level of the battery. With it we just set the TextView to display our number and update widget.

Here is the full code for our class:

package net.henriquerocha.android.batteroid;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.RemoteViews;

public class BatteroidAppWidgetProvider extends AppWidgetProvider {
	private RemoteViews views =
		new RemoteViews("net.henriquerocha.android.batteroid", R.layout.main);

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		context.getApplicationContext().registerReceiver(this,
				new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

		ComponentName cn =
			new ComponentName(context, BatteroidAppWidgetProvider.class);
		appWidgetManager.updateAppWidget(cn, this.views);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();

		if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
			Integer level = intent.getIntExtra("level", -1);
			this.views.setTextViewText(R.id.battery_level, level.toString());
			ComponentName cn =
				new ComponentName(context, BatteroidAppWidgetProvider.class);
			AppWidgetManager.getInstance(context).updateAppWidget(cn, this.views);
		}
		super.onReceive(context, intent);
	}
}

 

AndroidAppDev: Goal of this blog

AndroidAppDev will be, hopefully, a blog of reference for developers wanting to develop for the Android platform.

I’ll be doing tutorials on subjects that interest me and I’m sure will interest other developers also.

Someone once wrote that the best way to learn something is to teach it, and that’s exactly what I’m going to do. I’m really new to this platform but I’m really enjoying developing for it so I already have things to teach, specially to help others avoid the mistakes I’ve made and countless hours searching for solutions for simple problems that most tutorials out there don’t explain.

All applications developed from the tutorials will be open source with a GPLv3 license, so that you can develop your own open source programs and give back your changes back to the Android open source community.

If you want to develop closed source programs, this is not the place for you. Feel free to search for other sources of information.