HOW TO ADD TOAST & CUSTO TOAST IN ANDROID STUDIO: - Andropro

Andropro

androidtutorialro

Breaking

Home Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

Sunday, July 1, 2018

HOW TO ADD TOAST & CUSTO TOAST IN ANDROID STUDIO:

ADDING AN TOAST IMAGE IN ANDROID STUDIO:

In this tutorial we are going to see how to add an toast message in android studio project
toast is used to display information for a period of time. It contains a message to be displayed quickly and disappears after specified period of time. It does not block the user interaction. toast is a subclass of Object class. In this we use two constants for setting the duration for the Toast. Toast notification in android always appears near the bottom of the screen. We can also create our custom toast by using custom layout(xml file).



STEPS TO ADD AN TOAST MESSAGE IN ANDROID STUDIO:

1. makeText(Context context, CharSequence text, int duration): This method is used to initiate the Toast. This method take three parameters First is for the application Context, Second is text message and last one is duration for the Toast.

FOLLOWING ARE THE CONSTANT ATTRIBUTES:

1. LENGTH_LONG: It is used to display the Toast for a long period of time. When we set this duration the Toast will be displayed for a long duration.

2. LENGTH_SHORT: It is used to display the Toast for short period of time. When we set this duration the Toast will be displayed for short duration.


FOR EXAMPLE:
Toast toast = Toast.makeText(getApplicationContext(), "Simple Toast", Toast.LENGTH_LONG);
// initiate the Toast with context, message and duration



2. show(): This method is used to display the Toast on the screen. This method is display the text which we create using makeText() method of Toast.


Below we Firstly initiate the Toast and then display it using show() method.

Toast toast = Toast.makeText(getApplicationContext(), "Simple Toast In Android", Toast.LENGTH_LONG);
toast.show();


3. setGravity(int,int,int): This method is used to set the gravity for the Toast. This method accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.Below we Firstly initiate the Toast, set top and left gravity and then display it using show() method.

toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);

4. setText(CharSequence s): This method is used to set the text for the Toast. If we use makeText() method and then we want to change the text value for the Toast then we use this method.Below we firstly create a new Toast using makeText() method and then set the text for the Toast.


toast.setText("ALTERNATE Text"

); 5. setDuration(int duration): This method is used to set the duration for the Toast. If we use makeText() method and then we want to change the duration for the Toast then we use this method.

toast.setDuration(Toast.LENGTH_SHORT);
 


6. inflate(int, ViewGroup): This method is used to inflate the layout from the xml. In this method first parameter is the layout resource ID and the second is the root View. LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
7. setView(View): This method is used to set the view for the Toast. In this method we pass the inflated layout which we inflate using inflate() method.
Below we firstly retrieve the layout inflater and then inflate the layout and finally create a new Toast and pass the inflated layout in the setView() method.

// Retrieve the Layout Inflater and inflate the layout from xml LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
// create a new Toast using context
Toast toast = new Toast(getApplicationContext());
toast
.setDuration(Toast.LENGTH_LONG); // set the duration for the Toast
toast
.setView(layout); // set the inflated layout
toast
.show();
// display the custom Toast

SAMPLE CODING: THE GIVEN EXAMPLE SHOWS TOAST MESSAGE WHEN AN IMAGE IS CLICKED

                           DOWNLOAD SOURCE CODE


ACTIVITY_MAN.XML:

<?xml version="1.0" encoding="utf-8"?>

<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"

 tools:context=".MainActivity">

<TextView android:layout_width="wrap_content"

 android:layout_height="wrap_content" 

android:layout_alignParentTop="true" 

android:layout_centerHorizontal="true" 

android:layout_marginTop="26dp" 

android:text="ASTIN SALVI"

 android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

<ImageButton android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:id="@+id/imageButton"

 android:layout_centerVertical="true"

 android:layout_centerHorizontal="true" 

android:src="@drawable/jas" 

tools:ignore="ContentDescription"/>

</RelativeLayout>









MAINACTIVITY.JAVA:





public class MainActivity extends AppCompatActivity {

    ImageButton imgButton;


            @Override            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                imgButton =(ImageButton)findViewById(R.id.imageButton);
                imgButton.setOnClickListener(new View.OnClickListener() {
                    @Override                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(),"astin salvi",Toast.LENGTH_LONG).show();
                    }
                });
            }
}



OUTPUT:











Post Bottom Ad

Responsive Ads Here

Pages