Customizing Toast messages by Crouton library in Android

One way to notify users is to use Toasts. But Toasts have the problem that they might pop up in totally unrelated contexts. They are displayed for a defined duration on the screen no matter what the user does. The user might even have changed the app, with the result, that your Toast simply confuses the user.
But we can use an external library to make a customize Toast messages. In this post, I'll present the way how to implements Crouton library, it can make your messages become more flexible.

1. Download and import library in Eclipse

- Download Crouton at it's Github page.
Crouton is being built using gradle. This means that the source directories are different to the default Eclipse source paths, so we must fix it by following these steps.
- Unzipped the file and copied out the library folder.
- Open eclipse and import the library into the workspace (The name changes to "main").
- Drag and drop the "de" folder from "java" to "src" folder in "main" project:


- Create a "libs" folder in your "main" project and put android-support-v4.jar in it:

- Clean project, now errors in our library have disappeared.

Note: Okey, by other way, you can download lastest version of Crouton jar file  at Maven Center and put it in your libs  folder of your project. So you can ignore all above steps.

2. Starting a new project using Crouton library


After done creating project, remember making preference between it and "main" project. 
Creating layout for activity (activity_crouton.xml):
Some necessary xml file:
- Strings use in project (strings.xml):
- A customize layout use for Toast message later:

Coding in CroutonActivity

- Displaying 3 basic styles of Crouton (INFO, ALERT, CONFIRM) method:
- Displaying customize style of Crouton method:
- Customizing Crouton style with a layout method:

Full CroutonActivity.java code:


Run the application, we'll have some results like this:


pic name pic name pic name pic name pic name

Reference:

- Library in Github page: https://github.com/keyboardsurfer/Crouton
- Author Home page: http://marie-schweiz.de/

(Sorry for having ads)

Share


Previous post
« Prev Post
Next post
Next Post »