Sometimes when you show toast messages in your code you might not want the default length SHORT or LONG to display the toast. Sometimes we need to customize the duration of toast as per our requirements. Today in this example I will show how to customize Toast to display it for longer duration, by using CountDownTimer to call toast.show() repeatedly. I will also explain how to make the toast with counting numbers.
Explore my older post on Customize Toast message with an image.
You should follow the steps I will describe below to make an example of customized toast. I will show 4 methods of toasting so that you can compare and decide which one you need next time when you use toast in your code.
Step1: Create a new android project in your android IDE.
Step2: Write following into your main layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical"> <Button android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:textStyle="bold" android:text="Toast 1" /> <Button android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:textStyle="bold" android:text="Toast 2" /> <Button android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:textStyle="bold" android:text="Toast 3" /> <Button android:id="@+id/text4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:textStyle="bold" android:text="Toast 4" /> </LinearLayout>
Step3: Write following code into your activity:
package com.example.customizetoast; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import android.os.Bundle; import android.os.CountDownTimer; public class MainActivity extends ActionBarActivity { Button text1, text2, text3, text4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text1 = (Button)findViewById(R.id.text1); text2 = (Button)findViewById(R.id.text2); text3 = (Button)findViewById(R.id.text3); text4 = (Button)findViewById(R.id.text4); text1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Toast.makeText( MainActivity.this, "LENGTH_SHORT", Toast.LENGTH_SHORT).show(); }}); text2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Toast toast = Toast.makeText( MainActivity.this, "10 second - Not Work!", Toast.LENGTH_SHORT); toast.setDuration(10000); //set duration, not work toast.show(); }}); text3.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { final Toast toast = Toast.makeText( MainActivity.this, "10 second", Toast.LENGTH_SHORT); toast.show(); new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { toast.show(); } public void onFinish() { toast.cancel(); } }.start(); }}); text4.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { final Toast toast = Toast.makeText( MainActivity.this, "10 second", Toast.LENGTH_SHORT); toast.show(); new CountDownTimer(10000, 1000) { int count = 10; public void onTick(long millisUntilFinished) { toast.show(); count--; toast.setText(count + " sec"); } public void onFinish() { toast.cancel(); } }.start(); }}); } }
Step4: Run to see the output.
Here Toast 1 and 2 buttons are for the default toast duration LONG and SHORT. Toast 3 will show the toast for 10 secs and Toast 4 button will show 10 secs message with count down timer.
Checkout my previous blog which is a fun screen crack application HERE. Thanks
Top 10 Android App Development Trends | 2020 Guide
5 Best Resources to Get Started with Android Nougat
Android Studio Introduction
Services – An Android Component
Applying MediaCodec On An Open Source Android Audio Player
5 Most Used Android Testing Frameworks
Android Language Highlights A Developers Perspective
Android KitKat Development