This example show how to customize Toast to show longer duration, by using CountDownTimer to call toast.show() repeatly. And also show how to make the toast counting.
package com.example.androidtoastduration;
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();
}});
}
}
<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"
tools:context="com.example.androidtoastduration.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lesapplication.blogspot.com" />
<Button
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:textStyle="bold"
android:text="LENGTH_SHORT" />
<Button
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:textStyle="bold"
android:text="10 sec Toast - NOT Work!" />
<Button
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:textStyle="bold"
android:text="10 sec Toast" />
<Button
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:textStyle="bold"
android:text="10 sec counting Toast" />
</LinearLayout>
0 Response to "Customize Toast to show longer duration"
Posting Komentar