Toast in Android

Smart Android And Trik-Commenting on Andorid indeed never endless, because smart devices this one is often updated every certain amount of time. So that the market can always be garapnya menerinya with pleasure. And it is not denied if this device has become the lifestyle of each society. To not wonder if the 6th business information and many are turning to mobail smartphone. With Android which thoroughly dominated the mobile industry, choosing the best Android smartphone is almost identical to choose the best smartphone, period. But while Android phones have few real opponents on other platforms, internal competition is intense.

From the sleek devices impress with the design premium, up to a full plant furniture features, to a very good device, and affordable mobile phone has a heavy weight, the Android ecosystem inhabited by a diverse range of attractive mobile phone Toast in Android Toast in Android,But "oversize" are subjective, and sometimes pieces of the specification and a list of features is not enough to get an idea of how good a phone. In this roundup, we look at the absolute best-the Android phone you can't go wrong with. The habits of young people or to accentuate trand blindly lifestyle, make this a medoroang this clever device industry vying to do modifications to the device, with a distinctly vitur vitur-tercanggihnya. So it can be received over the counter Toast in Android

Android Toast is used to show sort message. Toast message takes only that amount of space that is required to show message, and during that time activity is visible and user can interact with it.
Toasts automatically disappear after a timeout.
                                                      

Toast Usage :

We can use Toast to show sort message as follows:
Toast.makeText( getApplicationContext(), "Your Message", Toast.LENGTH_LONG ).show();


We need to pass 3 arguments in Toast.makeText(Context context,String message,int length) function as follows:
  1. Context: First argument is Context object
  2. String: Message which you want to display in Toast.
  3. int: third parameter  is duration. It can have values Toast.LENGTH_SHORT or Toast.LENGTH_LONG
Set Position of Toast :

   We can change the position of Toast with the setGravity(int, int, int) method of Toast object. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

For example, To position Toast in the center of screen, we can set the gravity like this:
Toast toast = Toast.makeText(getApplicationContext(), "Your Message", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,
0, 0);
toast.show();               
Create Project :
          a) Open Android Studio
          b) Go to File >New> New Project  > Project Name  > Next > Next > Next > Finish 
Layout Design :
         open activity_main.xml file and replace the given bellow code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:app="http://ift.tt/GEGVYd"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4286f4"
android:orientation="vertical"
tools:context="app.aion.DemoActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Apna Java"
android:textColor="#41f4ca"
android:textSize="20dp" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toast Example" />
</LinearLayout>
 
Code:  
 
 Open MainActivity.java file and replace the given bellow code.

package app.apnajava;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

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

btnToast=(Button)findViewById(R.id.btn);

btnToast.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Toast.makeText( getApplicationContext(), "Hi... This is Apna Java Toast Tutorial.", Toast.LENGTH_LONG ).show();
}
});
}
}
 
       




Find Us : 
        Facebook : @apnaandroid
        Google+   : Apna Java
        Youtube : Android & Java Tutorial

Read:


Subscribe to receive free email updates:

Related Posts :

0 Response to "Toast in Android"

Post a Comment