Set/Show Toast message to a specific time in android

How to set increase toast message display duration in android programmatically.

In this tutorial we are creating TextView and setting up that textview inside Toast message as a View. Now we are using CountDownTimer to start and stop show toast message on a specific time period. So here is the complete step by step tutorial for Set/Show Toast message to a specific time in android.

android-project-download-code-button

How to Set/Show Toast message to a specific time in android.

Code for MainActivity.java file.

 package com.showtoastmessagespecifictime_android_examples.com;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 CountDownTimer countdowntimer = null;
 Button button;
 Toast toastMessage;
 TextView textView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 //Creating toast message.
 toastMessage = new Toast(MainActivity.this);
 
 //Creating TextView.
 textView = new TextView(MainActivity.this);
 
 //Setting up Text Color.
 textView.setTextColor(Color.parseColor("#fafafa"));
 
 //Setting up Text Size.
 textView.setTextSize(17);
 
 //Setting up Toast Message Text.
 textView.setText("Toast message to a specific time");
 
 //Add padding to Toast message.
 textView.setPadding(20, 20, 20, 23);
 
 //Adding TextView into Toast.
 toastMessage.setView(textView);
 
 //Access toast message as View.
 View toastView = toastMessage.getView();

 //Set Custom Background on Toast.
 toastView.setBackgroundResource(R.layout.toast_message_style);
 
 
 countdowntimer = new CountDownTimer(40000, 1000)
 {
 public void onTick(long millisUntilFinished)
 {
 toastMessage.show();
 }
 public void onFinish()
 {
 toastMessage.cancel();
 }

 }.start();
 
 }
 
 });
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout 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"
 tools:context="com.showtoastmessagespecifictime_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click here to Show Toast message to a specific time in android" />

</RelativeLayout>

Code for toast_message_style.xml file.

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
 <shape android:shape="rectangle">
 <solid android:color="#009973"/>
 <corners android:radius="20dp" />
 <stroke
 android:width="4dp"
 android:color="#01ffc0"
 />
 </shape>
</item>
</selector>

Screenshot:

Set/Show Toast message to a specific time in android

 Click here to download Set/Show Toast message to a specific time in android project with source code.

Leave a Reply

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