• Have Any Question?
  • contact@eitworld.com

write an application that show a toast for long time.





Normally toast is displayed max 3.5 seconds but here we have used some logic to display the toast for long time, the xml file is as follows:

<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=”.MainActivity” ><TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”28dp”
android:text=”Toast Example” />

<Button
android:id=”@+id/btnToast”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView1″
android:layout_centerVertical=”true”
android:text=”Show Toast” />

</RelativeLayout>

In this layout file, we have used relative layout.
android:id=”@+id/textView1″ is used to define the id of the message that is shown on the activity.
android:id=”@+id/btnToast” is used to define the id of the button so that it can be identified in the java file easily.
The java file is as follows:

package com.example.androidtoast;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
// create button instance
Button tost;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// register button
tost = (Button) findViewById(R.id.btnToast);

//perform action on button click

tost.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Context context = getApplicationContext();
CharSequence text = “This is example of the Toast”;
int duration = Toast.LENGTH_LONG;
final Toast toast = Toast.makeText(context, text, duration);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 500000);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

In this java file first we have to import some packages which are as follows:

android

In this android.widget.Toast package is used to define the toast related functionality.
After importing the package, we have to create the instance of the button so that we can register the button as follows:
android
Here we declare an instance variable tost of type Button.
Now we have to register the button as follows:
android
Here we are registering the button using the id of the button which is defined in the xml layout file.
Now we have to perform action on the button as follows:
android
Here we have used setOnClickListener() method which takes OnClickListener object and creates an anonyms method.
Inside this method, we must override the onClick() method which takes View type parameter to view.
Inside method, we have used Context type of instance to assign the current context of the application.
After that we have used CharSequence type variable to hold the message.
After that we have to specify the time duration for the toast so we have used duration variable of int type.
Now we have used the following method and we have passed these variables to the method as parameters as follows:
android
Now we have to show the toast using the show() method as follows:
android
In side this method we have used the Handler class object and using the posyDelay() method we can show the toast on the screen for the long time.
Here we have used the multithreading concept of java. Inside this, we have used the run() method and in the end of the method we have defined the time in the milliseconds, so user can increase the time.
The output is as follows:
android
When launched
android
when clicked on button



Real Time Web Analytics