Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Saturday 10 September 2016

Android Tutorial : Toast Example

Android Toast is used to display small pop up information for short time. It contain message to display some information.In this example , we will show that how to display message using toast.

android.widget.Toast class is the subclass of the java.lang.Object class. We can also create our custom toast like we can add image and text in toast and display on the top of the activity or bottom or center can also.

There are some constant of the toast class :

  • LENGTH_SHORT : display toast for the short time.
  • LENGTH_LONG : display toast for the long time.


There are some methods of the toast class :


  • public static Toast makeText(Context context, CharSequence text, int duration) :
    • used for set message of the toast and duration of the toast to display on view.

  • public void show() :
    • used for display toast on activity.

  • public void setMargin (float horizontalMargin, float verticalMargin) :
    • used to set horizontal and vertical margin.




File Name : MainActivity.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.example.toastexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

  @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  Toast.makeText(getApplicationContext(),"Welcome to Desire Code",Toast.LENGTH_SHORT).show();
 }

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

}
Output:




Toast

0 comments:

Post a Comment

Like us on Facebook

Site Visitor

Powered by Blogger.