Archives 2012

What do you want for beakfast ? Just toasts...

Written by Xavier Gouchet - 24 february 2012 - no comments

Toasts ?

Toasts are the usual way to give feedbacks to the user without impacting his experience. The message pops, without having to click on a button to close it.

Usually, a toast can be created using a single line, looking a bit like this, either using a string or with a string resource id (which is better if you want localization).

Toast.makeText(context, "Hello toasts", Toast.LENGTH_SHORT).show();
Toast.makeText(context, R.string.hello_toasts, Toast.LENGTH_SHORT).show();

Android toast with default layout

Custom Toasts

Android also provides a way to have a completely custom view instead, and have all you want, text, images, etc...

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout, null);

// here customize the content of the layout

Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

Android toast with custom layout

Anything else ?

Both methods are quite straightforward, but the second method requires a lot of work. And when you only want to change a tiny details in the default toast, for example the text color, how can you do ?

The idea is to get the TextView created by the default Toast and edit its parameters, like this

Toast toast = Toast.makeText(this, resId, Toast.LENGTH_SHORT);
TextView v = (TextView) toast.getView().findViewById(;


Android toast with customized default layout

More ...

You can find more information on toasts in the Android documentation.

A robot with a brush : using Paint on Android (Part 1)

Written by Xavier Gouchet - 08 february 2012 -

It's rather common to work with the Canvas when building an app or a Live Wallpaper on Android. The canvas can seem very simple at first, but the Paint class can offer a lot more than what you may think.

The first thing one uses the Paint for is to draw primitives. The Canvas class comes with a limited number of primitives (which you can draw using the drawXXX(...) method). These primitives are shown in the image below : Arcs, Circles, Ovals, Lines, Points, Rectangles (with or with round corners) and text.


When drawing each primitive, a Paint instance is require, to define the color, the style (do we paint the outline, the filling, or both), and severall basic options like antialising and image filtering. But we can also add some effects on the Paint to enhance the basic primitives. For instance, blur the primitives a little, using one of the following lines (corresponding images below).

paint.setMaskFilter(new BlurMaskFilter(5, Blur.NORMAL)); 
paint.setMaskFilter(new BlurMaskFilter(5, Blur.SOLID));
paint.setMaskFilter(new BlurMaskFilter(5, Blur.INNER));
paint.setMaskFilter(new BlurMaskFilter(5, Blur.OUTER));


This effect was really trendy in the 1990s internet, and still in most Powerpoint slideshows. The emboss will make anything look like a 3D object, with simulated highlight and shadow on the edges (like on the image below).

paint.setMaskFilter(new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.5f, 4.0f, 10.0f));


Another trendy effect is the shadow, to simulate a ... well you guessed, a shadow on the canvas behind the primitive being drawn.

paint.setShadowLayer(5, 10, 5, Color.LTGRAY);