[Android] Beginner Tutorials – Lesson 12. Logs and Toast messages.

In this lesson we will:

– look through application logs and Toast messages

Translated by Taras Leskiv (http://android-by-example.blogspot.com/)

Create a project:

Project name: P0121_LogAndMess
Build Target: Android 2.3.3
Application name: LogAndMess
Package name: ru.startandroid.develop.logandmess
Create Activity: MainActivity

Create a screen in main.xml that we are already familiar with from the lesson about listeners

The application algorithm will be the same. We will change text when the button is clicked. Activity is a listener.

Save and run the application. Make sure everything works fine

Application logs

When testing your application, you can see working logs. They are displayed in the LogCat window. To open LogCat go to Window > Show View > Other … In the window that appeared choose Android > LogCat.

L0012_010The LogCat tab should appear


Let’s observe this tab in more detail. Logs have different importance levels: ERROR, WARN, INFO, DEBUG, VERBOSE (descending). Items in dropdown list (verbose, debug, …) are filters and they correspond to log types. Test them and note that filter shows not only logs of its level, but also logs of higher importance. You can also create, edit and delete your own filters – we will examine this later on.

Let’s see how to write logs. It is really simple and it can be done using Log class and its Log.v(), Log.d(), Log.i(), Log.w() and Log.e() methods. Method names correspond to log levels which they write.

Let’s change MainActivity.java code. Take all the comments from code and add them as DEBUG-logs using log.d method. Method takes a tag and text of the message as parameters. Tag is something like a label, so you could later find your messages in the pile of system logs. Let’s add the tag description (TAG) and write all text from comments into logs.

Eclipse will complain that it does not know Log class. Update imports (CTRL + SHIFT + O) and choose android.util.Log if asked. Run the application, click some buttons and check the logs


You can see that everything has been successfully written. And Eclipse has created filter (ru.startandroid.develop.logandmess) for our application. Let’s create our own filter. Click the + icon


Choose any name you wish, “My logs” for example. Log Tag – this is a value of the TAG constant, which is described in our code and was used in Log.d method. In our case it is “myLogs“. Set debug log level. And other fields leave empty.


and click OK. The new tab My Logs appeared, where logs are displayed, which correspond only to our newly created filter.

We put text into the log, but it is clear that you can write values of variables that you need (their String representation).

Sometimes happens, that logs are not displayed in the LogCat tab, although AVD is launched and application works without problems. In this case the following should help: in Eclipse go to Window > Open Perspective > Other > DDMS. A little bit different than usually set of windows will open. Find the Devices tab and your AVD-device should be visible  there, click on it and logs should appear. To get back to development: Window > Open Perspective > Java.

Toast messages

Application can display messages using Toast class. Let’s edit onClick method. We will make Toast message appear indicating which button was clicked.

Let’s look through the syntax. Static method makeText creates a Toast View-element. Method parameters:

context – we will not look through what is it in detail, just use the current Activity for now
text – text that we need to show
duration – duration of displaying a message (Toast.LENGTH_LONG – long, Toast.LENGTH_SHORT – short)

Toast has been created and to display it on the screen, we need to invoke show() method. Save, run the application and check.


I guess, if you have an Android smartphone, you have seen such messages before. Now you know how it is done )



Share this Post :

No comments yet.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *