Reading
Add Comment
Android Toast Example Code
The android.widget.Toast class is the subclass of java.lang.Object class.
You can also create custom toast as well for example toast displaying image. You can visit next page to see the code for custom toast.
Toast class
Toast class is utilized to show warning for a specific interim of time. After at some point it vanishes. It doesn't hinder the client cooperation.
Constants of Toast class
There are just 2 constants of Toast class which are given underneath.
Constant
|
Description
|
---|---|
public static final int LENGTH_LONG | displays view for the long duration of time. |
public static final int LENGTH_SHORT | displays view for the short duration of time. |
Methods of Toast class
The broadly utilized techniques for Toast class are given underneath.
Method
|
Description
|
---|---|
public static Toast makeText(Context context, CharSequence text, int duration) | makes the toast containing text and duration. |
public void show() | displays toast. |
public void setMargin (float horizontalMargin, float verticalMargin) | changes the horizontal and vertical margin difference. |
Android Toast Example
Toast.makeText(getApplicationContext(),"Hi Dheeru",Toast.LENGTH_SHORT).show();
Another Way:
Toast toast=Toast.makeText(getApplicationContext(),"Hello Dheeru",Toast.LENGTH_SHORT);
toast.setMargin(50,50);
toast.show();
toast.setMargin(50,50);
toast.show();
Full code of activity class displaying Toast
How about we see the code to show the toast.
package com.example.toast;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Displaying Toast with Hello Javatpoint message
Toast.makeText(getApplicationContext(),"Hello Dheeru",Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Displaying Toast with Hello Javatpoint message
Toast.makeText(getApplicationContext(),"Hello Dheeru",Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
0 comments:
Post a Comment