It is official : AdMob is now part of Google !
This is kind of funny :
Google is the giant Leader of virtual ads.
But on its own OS, its ads solution is still not ready ( it is still in beta state for the moment, and only avalaible for people with 100 000 pages views / day -sic- )...
So it just bough the current leader on mobiles !
A blog from an Android developer.
Developing since the start of the platform, and constantly improving my applications, I share my experience developping with Google OS for smartphones.
Monday, November 9, 2009
Wednesday, October 28, 2009
Tutorial : How to customize your toasts !
Toast, despite their strange name that an average french developer like me doesn't understand, are really usefull in Android world ! Specially for developers !
The Standard Toast with butter :
It is obviously a easy and quick way to send text feedback to the user and the developer ( as it is a nice tool to give feedback ).
This line has been used such a number of times in my programs, and in many others !
The Toast with orange jam :
But the standard 'look and feel' of your toast does not necessarily fit your application's look. And the default position can be a issue too.
In Word Prospector, I give feedback on whether the word is correct with a toast. But at first this toast was over my game board, preventing the player to click on some letters !
Then enters the toast customisation !!

Here's I changed the position of my toast, in order to have it in the right - bottom corner, and its background, so it's like the others gui element of the page !
To change the toast position, you have to use the setGravity Toast method. You have to provide the gravity of your toast ( this strange Android beast that indicates where your gui element will end up being ), and you can refine this position by some offsets.
To change the style of the toast is also easy :
The default toast is made of a textView.
You can actually access this textView, and change anything you want inside ( in my case, the background image, but you can imagine changing the font, or the text color / size, ... )
Here's my code to change the pos and the background :
The toast with butter, and orange jam, ... and some chocolate...
Do you want more customisation ?
You can actually change the textView inside the toast by any view you want... Any view.... Including a custom view declared in a Xml file, and that you can inflate inside your toast...
In this case, you can easily add several lines, or images, or whatever you want !
No Toast with orange jam and chicken wings allowed !!
Despite being a really nice to tool to master, the toast is limited in two ways :
* You don't have control on the toast duration. Actually there are only two states LENGTH_LONG and LENGTH_SHORT. Note that I imagine you can force the closure of a toast with the cancel method ( I didn't test it )
* More important : you can't have several toasts at a time ! So if you need to display a new toast and old one is already present, the new one will be queued by the system, and wait for the old one to disappear. At some times, it can be a important limitation, as it means you can't control the feedback your giving will apppear at the correct moment !
You can try to close the first one in order to display the new one ( with the cancel method... Remember ? The one I didn't test ), but in some cases the two toasts should be at different places, and you want the user to see and have time to read both of them !
The Standard Toast with butter :
It is obviously a easy and quick way to send text feedback to the user and the developer ( as it is a nice tool to give feedback ).
Toast.makeText(mCurActivity, text, Toast.LENGTH_SHORT).show();
This line has been used such a number of times in my programs, and in many others !
The Toast with orange jam :
But the standard 'look and feel' of your toast does not necessarily fit your application's look. And the default position can be a issue too.
In Word Prospector, I give feedback on whether the word is correct with a toast. But at first this toast was over my game board, preventing the player to click on some letters !
Then enters the toast customisation !!

Here's I changed the position of my toast, in order to have it in the right - bottom corner, and its background, so it's like the others gui element of the page !
To change the toast position, you have to use the setGravity Toast method. You have to provide the gravity of your toast ( this strange Android beast that indicates where your gui element will end up being ), and you can refine this position by some offsets.
To change the style of the toast is also easy :
The default toast is made of a textView.
You can actually access this textView, and change anything you want inside ( in my case, the background image, but you can imagine changing the font, or the text color / size, ... )
Here's my code to change the pos and the background :
protected void ToastMessage( CharSequence text, int ToastPos )
{
Toast toast = Toast.makeText(mCurActivity, text, Toast.LENGTH_SHORT);
View textView = toast.getView();
switch ( ToastPos )
{
case TOAST_LEFT_POS:
toast.setGravity( android.view.Gravity.BOTTOM | android.view.Gravity.LEFT, 00, 0);
break;
case TOAST_CENTER_POS:
toast.setGravity( android.view.Gravity.BOTTOM | android.view.Gravity.CENTER, 00, 0);
break;
case TOAST_RIGHT_POS:
toast.setGravity( android.view.Gravity.BOTTOM | android.view.Gravity.RIGHT, 00, 0);
break;
}
textView.setBackgroundResource( R.drawable.textview);
toast.show();
}
{
Toast toast = Toast.makeText(mCurActivity, text, Toast.LENGTH_SHORT);
View textView = toast.getView();
switch ( ToastPos )
{
case TOAST_LEFT_POS:
toast.setGravity( android.view.Gravity.BOTTOM | android.view.Gravity.LEFT, 00, 0);
break;
case TOAST_CENTER_POS:
toast.setGravity( android.view.Gravity.BOTTOM | android.view.Gravity.CENTER, 00, 0);
break;
case TOAST_RIGHT_POS:
toast.setGravity( android.view.Gravity.BOTTOM | android.view.Gravity.RIGHT, 00, 0);
break;
}
textView.setBackgroundResource( R.drawable.textview);
toast.show();
}
The toast with butter, and orange jam, ... and some chocolate...
Do you want more customisation ?
You can actually change the textView inside the toast by any view you want... Any view.... Including a custom view declared in a Xml file, and that you can inflate inside your toast...
In this case, you can easily add several lines, or images, or whatever you want !
No Toast with orange jam and chicken wings allowed !!
Despite being a really nice to tool to master, the toast is limited in two ways :
* You don't have control on the toast duration. Actually there are only two states LENGTH_LONG and LENGTH_SHORT. Note that I imagine you can force the closure of a toast with the cancel method ( I didn't test it )
* More important : you can't have several toasts at a time ! So if you need to display a new toast and old one is already present, the new one will be queued by the system, and wait for the old one to disappear. At some times, it can be a important limitation, as it means you can't control the feedback your giving will apppear at the correct moment !
You can try to close the first one in order to display the new one ( with the cancel method... Remember ? The one I didn't test ), but in some cases the two toasts should be at different places, and you want the user to see and have time to read both of them !
Monday, September 14, 2009
Tutorial : how to use the LED with Android phone
Some android phones have a nice LED ( but not all of them: I don't think the Galaxy has one ).
This LED can show lots of different colors, and adding this feature in your application is a nice little "plus" !
And it's really easy...
So how to do it ?
You just have to get the notification manager, and use it to send a flash Light notification.
The notification contains the color light, the duration with the LED On, and the duration with the LED off.
Here's the code :
private void RedFlashLight()
{
NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
Notification notif = new Notification();
notif.ledARGB = 0xFFff0000;
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100;
notif.ledOffMS = 100;
nm.notify(LED_NOTIFICATION_ID, notif);
}
LED_NOTIFICATION_ID is just a value to register this allocation. In my case, it is just 0.
Here, the LED will flash with the color given by the parameter ledARGB, in this case 0xFFff0000 (red ). It will be ON for 100 milliseconds, then OFF for 100 milliseconds, and then loop back ON, etc...
This is nice, but wait... How do I STOP it ?
This code will let the LED flash for ever !
To stop it, we have two solutions :
1) decide to only have the LED flash once, so in this case, you have to add the FLAG_ONLY_ALERT_ONCE flag :
notif.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONLY_ALERT_ONCE;
2) Let the LED flash for as long as you want, then cancel the notification. In my case, I stop it after a certain amount of time by posting a message with a delay.
Here is the full code for this :
private void ClearLED()
{
NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
nm.cancel( LED_NOTIFICATION_ID );
}
private Runnable mClearLED_Task = new Runnable()
{
public void run()
{
synchronized( LetterGame.this)
{
ClearLED();
}
}
};
private void RedFlashLight()
{
NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
Notification notif = new Notification();
notif.ledARGB = 0xFFff0000;
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100;
notif.ledOffMS = 100;
nm.notify(LED_NOTIFICATION_ID, notif);
// Program the end of the light :
mCleanLedHandler.postDelayed(mClearLED_Task, LED_TIME_ON );
}
With mCleanLedHandler being just a handler, and LED_TIME_ON the flash duration.
Some notes :
1) Don't let a handler alive when changing activity ! On the OnPause of your activity, clear the handler :
mCleanLedHandler.removeCallbacks( mClearLED_Task );
2) programming the LED suffers one issue : the emulator does not emulate the LED at all... So you have to use a real device to see what it looks like ( but anyway, you always use real device at some point, don't you ? :) )
3) programming the LED then suffers a Second issue : when the real device is connected to your PC in order for you to develop / test / debug, the device is also charging the battery. And in this case, the LED is orange. Whatever notification you send ! So you have to test without your device plugged !
That's all !
With all that, you can code some nice LED flashes !!!
This LED can show lots of different colors, and adding this feature in your application is a nice little "plus" !
And it's really easy...
So how to do it ?
You just have to get the notification manager, and use it to send a flash Light notification.
The notification contains the color light, the duration with the LED On, and the duration with the LED off.
Here's the code :
private void RedFlashLight()
{
NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
Notification notif = new Notification();
notif.ledARGB = 0xFFff0000;
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100;
notif.ledOffMS = 100;
nm.notify(LED_NOTIFICATION_ID, notif);
}
LED_NOTIFICATION_ID is just a value to register this allocation. In my case, it is just 0.
Here, the LED will flash with the color given by the parameter ledARGB, in this case 0xFFff0000 (red ). It will be ON for 100 milliseconds, then OFF for 100 milliseconds, and then loop back ON, etc...
This is nice, but wait... How do I STOP it ?
This code will let the LED flash for ever !
To stop it, we have two solutions :
1) decide to only have the LED flash once, so in this case, you have to add the FLAG_ONLY_ALERT_ONCE flag :
notif.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONLY_ALERT_ONCE;
2) Let the LED flash for as long as you want, then cancel the notification. In my case, I stop it after a certain amount of time by posting a message with a delay.
Here is the full code for this :
private void ClearLED()
{
NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
nm.cancel( LED_NOTIFICATION_ID );
}
private Runnable mClearLED_Task = new Runnable()
{
public void run()
{
synchronized( LetterGame.this)
{
ClearLED();
}
}
};
private void RedFlashLight()
{
NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
Notification notif = new Notification();
notif.ledARGB = 0xFFff0000;
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100;
notif.ledOffMS = 100;
nm.notify(LED_NOTIFICATION_ID, notif);
// Program the end of the light :
mCleanLedHandler.postDelayed(mClearLED_Task, LED_TIME_ON );
}
With mCleanLedHandler being just a handler, and LED_TIME_ON the flash duration.
Some notes :
1) Don't let a handler alive when changing activity ! On the OnPause of your activity, clear the handler :
mCleanLedHandler.removeCallbacks( mClearLED_Task );
2) programming the LED suffers one issue : the emulator does not emulate the LED at all... So you have to use a real device to see what it looks like ( but anyway, you always use real device at some point, don't you ? :) )
3) programming the LED then suffers a Second issue : when the real device is connected to your PC in order for you to develop / test / debug, the device is also charging the battery. And in this case, the LED is orange. Whatever notification you send ! So you have to test without your device plugged !
That's all !
With all that, you can code some nice LED flashes !!!
Thursday, August 20, 2009
Cupcake finally in France with Orange !
As you may already know from my previous article ( here ), here in France, the HTC Dream ( G1 ) was still running with a 1.1 version of Android.
And now an update to cupcake has finally been released by Orange !
So now, I have access to video recording, to the widgets, ... and above all, to all the applications that used the Cupcake API, and that we couldn't use till now ! ( it also means I will be able to try Python on my phone ! ).
That's not all !
This is a version coming from HTC, and in the package, we could also find :
* a PDF viewer ( it's nice to have a PDF viewer ! It's far from being uber performant, but, still it's here, and that a good point !!
* QuickOffice ( a office document reader : I tried it on several Word files, and it was OK, but for a big one - big being about 600 ko in my case, so not THAT big. Once again the performances are far from being stellar, but at least we can read some docs ! )
* A microsoft exchange mail reader and synchronization tool. Actually, I didn't tried it, but I'm sure a lot of people out there will be very happy with this !
But still I can't understand all that about this version :
* The installation is done via a PC, and we lose all of our data... That's not nice...
* We don't have any virtual keyboard ! That's a strange one... We still have the physical one, and I'm sure it is better. But in some cases, it's painful to open the physical keyboard just to type one word. And there are a lot of applications that don't deal that nicely with opening the keyboard...
* The last point is, in my opinion the worst of all : a lot of application are missing from the Android market.
To be sure to really understand what was happening, I created two void applications, one called OrangeTry, and the second called OrangeTry2.
I protected only the first one, and I released them ( for 5 minutes only )...
And guess what : only the second one appeared :

So this new version filters the protected applications !
I can't understand why they decided that ( actually, what I really think is that it is just a mistake ).
From what I understood from Google deal with the carriers, carriers are getting some money from the sell of Android applications !
As most paid applications are protected, this configuration will make Orange make less money than with the previous version ! How strange !
And I'm missing some free applications that were, for some reasons, protected ( this is a mistake : a protected version is about twice as big as the unprotected one ! ).
I'm particularly missing Dizzler !
A little question to finish this post :
Is there any carrier / country left where Cupcake has not been released ?
Tuesday, August 11, 2009
Tutorial : How to have two buttons on each side of the screen

Sometimes, things are quite confusing in the Android World.
I wanted to have one two buttons, on each side of the screen ( the famous next and previous buttons ).
I though it would be really easy to do.
I was wrong...
I had already done something quite similar for multiple column list view ( here ), but the trick I actually used for the multiple column view was not applicable here.
For the multiple column view, I could use a text view with a left aligned text, and another big text view taking all the remaining space with right aligned text.
As the textviews are invisibles ( only the text inside was visible ), the trick was unnoticable.
So now, my first try was to put my buttons in a linear layout, and have one with a right gravity, and another with a left gravity.
But it just failed : the two buttons were on the right, or the two buttons were on the left, but I couldn't obtain the result I wanted.
Finally, after some hours struggling with the documentations, some blogs, and the samples from the 1.1 SDK ( and my cat, but that is another story ), I found a way :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button_previous"
android:layout_height="wrap_content"
android:layout_width="110px"
android:layout_alignParentLeft="true"
android:textSize="15sp"
android:typeface="monospace"
android:textColor="#ffFFffFF"
android:background="@drawable/textview"
android:text="@string/PreviousButton"
>
</Button>
<Button
android:id="@+id/button_next"
android:layout_height="wrap_content"
android:layout_width="110px"
android:layout_alignParentRight="true"
android:textSize="15sp"
android:typeface="monospace"
android:textColor="#ffFFffFF"
android:background="@drawable/textview"
android:text="@string/NextButton"
>
</Button>
</RelativeLayout>
So I discovered the alignParentRight / alignParentLeft tags !
What are the lessons learned :
* Android GUI is not always as easy as planned
* The linear Layout, that is often the first choice, is often the wrong choice. Relative Layout is much more flexible.
* Where are the API Demos samples in the 1.5 SDK ?
Subscribe to:
Posts (Atom)