
Basic Toast
Creating toasts requires only one line as shown below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Toast.makeText(this, TOAST_SHORT_MSG, Toast.LENGTH_SHORT).show(); |
Display Toast Even Longer
Currently, I was only able to find one way to display Toasts longer than Toast.LENGTH_LONG. What I did was basically invoke show() on the same Toast object more than once consecutively. This was simply done by using a for loop and looping it as many times as you like. Depending on how many times you loop, it will display it for Toast.LENGTH_LONG times the number of times you loop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int i = 1; i <= 4; i++) { | |
Toast.makeText(this, TOAST_4_LONG_MSG, Toast.LENGTH_LONG).show(); | |
} |
Custom Toast Background
If you happen to not like the default background, you create your own custom background and display Toasts messages using that background. To do this, you will first need to create a 9-patch image that you want to use for your Toast background. One tool that you can use to create 9-patch images from an existing image is Nine-patch Generator. Once you have placed the 9-patch images to their corresponding folders in your Android project, you need to then create a separate layout for your Toast and set that 9-patch image as background value for your root layout. Like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="@drawable/toast_bubble" | |
android:orientation="vertical" > | |
<TextView | |
android:id="@+id/toast_textview" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textIsSelectable="false" | |
android:textAppearance="?android:attr/textAppearanceSmall" | |
android:textColor="@android:color/black" > | |
</TextView> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the layout you want and set the values inside that layout. | |
View toastRoot = getLayoutInflater().inflate(R.layout.activity_toast_custom_toast, null); | |
TextView tvToast = (TextView) toastRoot.findViewById(R.id.toast_textview); | |
tvToast.setText(TOAST_CUSTOM_BG_MSG); | |
// Create new Toast object and set the view to the one you want. | |
Toast toast = new Toast(getApplicationContext()); | |
toast.setView(toastRoot); | |
toast.show(); |
You can view my Android Toast example HERE.
Relevant files are:
src/com/choiboi/apiexamples/toast/ToastMainActivity.java
res/layout/activity_toast.xml
res/layout/activity_toast_custom_toast.xml
Happy Coding!