An open API service indexing awesome lists of open source software.

https://github.com/suryakarmakar/androidjava-toast

how to use toast different way in android java
https://github.com/suryakarmakar/androidjava-toast

Last synced: 7 months ago
JSON representation

how to use toast different way in android java

Awesome Lists containing this project

README

          

# Android Java Toast Message

## Toasts overview :

A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts automatically disappear after a timeout.


## Create and Show Toast :


  • First create a Toast object
  • ```java
    Toast toast = Toast.makeText(getApplicationContext(), "Simple Toast Message", Toast.LENGTH_SHORT);
    ```

  • Then create a toast message, use the makeText() method which takes the following parameters:

    1. The application Context

    2. The text that should appear to the user.

    3. The duration that the toast should remain on the screen.

    ```java
    Toast toast = Toast.makeText(getApplicationContext(), "Simple Toast Message", Toast.LENGTH_SHORT);
    ```

  • And the last to display toast message in screen call the show() function
  • ```java
    toast.show();
    ```

## Chain your toast method calls :

You can chain your methods to avoid holding on to the Toast object, as shown in the following code snippet:

```java
Toast.makeText(getApplicationContext(), "Toast with java method chaining", Toast.LENGTH_SHORT).show();
```

## Constants of Toast class (duration):

There are only 2 constants of Toast class which are given below.


1. LENGTH_LONG : displays toast message view for the long duration of time.


2. LENGTH_SHORT : displays toast message view for the short duration of time.

## Toast Display Position :

A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the
setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.


For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this:

```java
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
```
## Creating a Custom Toast View :


  1. To create a custom toast view, you need to create a custom layout file in XML(custom_toast.xml) and pass the root View object to the setView(View) method.
  2. ```xml

    ```

  3. And if you want to create a custom toast shape then you need to create a new shape XML(custom_toast_shape.xml) also
  4. ```xml


    ```

  5. Then, retrieve the LayoutInflater with getLayoutInflater() and then inflate the layout from XML using inflate(int, ViewGroup). The first parameter is the layout resource ID and the second is the root View.
    You can use this inflated layout to find more View objects in the layout, so now capture and define the content for the ImageView and TextView elements. Finally, create a new Toast with Toast(Context) and set some properties of the toast,
    such as the gravity and duration. Then call setView(View) and pass it the inflated layout. You can now display the toast with your custom layout by calling show().
  6. ```java
    //Creating the LayoutInflater instance
    LayoutInflater li = getLayoutInflater();
    //Getting the View object as defined in the custom_toast.xml file
    View layout = li.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.customtoast));

    btn4.setOnClickListener(view -> {
    Toast toast = Toast.makeText(getApplicationContext(), "Set Toast Position", Toast.LENGTH_SHORT);
    toast.setView(layout);
    toast.show();
    });
    ```