How to add a custom styled Toast in Android

How to add a custom styled Toast in Android - Hello friend inabnomaniiyaha, In the article that you read this time with the title How to add a custom styled Toast in Android, we have prepared this article well for you to read and take information in it. hopefully the contents of the post Artikel android-example,what we write you can understand. Alright, happy reading.

Judul : How to add a custom styled Toast in Android
link : How to add a custom styled Toast in Android

Baca juga


How to add a custom styled Toast in Android

How to add a custom styled Toast in Android

This article explains how to create Custom Toast messages, which has custom background, image, icon, etc, which are not provided by the original Toast library.

Add the support Library in your root build.gradle file 

allprojects { 

    repositories { 

        maven { 

            url "https://jitpack.io"  

        } 

    } 

Add the support Library in your module’s build.gradl

dependencies { 

         implementation 'com.github.GrenderG:Toasty:1.4.2' 

Now add the following code in the activity_main.xml file. This code adds five buttons in the activity. Each button is used to call differnt style of toasts.

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

  

    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:layout_gravity="center"

        android:orientation="vertical"> 

  

        <Button

            android:layout_margin="10dp"

            android:id="@+id/button_warning"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="showToast"

            android:text="show warning toast" /> 

  

        <Button

            android:layout_margin="10dp"

            android:id="@+id/button_info"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="showToast"

            android:text="show info toast" /> 

  

        <Button

            android:layout_margin="10dp"

            android:id="@+id/button_success"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="showToast"

            android:text="show success toast" /> 

  

        <Button

            android:layout_margin="10dp"

            android:id="@+id/button_error"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="showToast"

            android:text="show error toast" /> 

  

        <Button

            android:layout_margin="10dp"

            android:id="@+id/button_normal"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="showToast"

            android:text="show normal toast with an icon" /> 

    </LinearLayout> 

</RelativeLayout> 

Now add the following code in the MainActivity.java file. Now on clicking any button the showToast() function is started and corresponding Toast is displayed

public class MainActivity 

    extends AppCompatActivity { 

  

    @Override

    protected void onCreate( 

        Bundle savedInstanceState) 

    { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.activity_main); 

    } 

  

    // We assign this method 

    // on OnClick() method 

    // in activity_main.xml file. 

    public void showToast(View v) 

    { 

        switch (v.getId()) { 

  

        // For Error type Toast 

        case R.id.button_error: 

            Toasty.error(this, 

                         "This is an error Toast", 

                         Toast.LENGTH_SHORT) 

                .show(); 

            break; 

  

        // For Success type Toast 

        case R.id.button_success: 

            Toasty.success(this, 

                           "This is a success Toast", 

                           Toast.LENGTH_SHORT) 

                .show(); 

            break; 

  

        // For Info type Toast 

        case R.id.button_info: 

            Toasty.info(this, 

                        "This is an info Toast", 

                        Toast.LENGTH_SHORT) 

                .show(); 

            break; 

  

        // For Warning type Toast 

        case R.id.button_warning: 

            Toasty.warning(this, 

                           "This is a warning Toast", 

                           Toast.LENGTH_SHORT) 

                .show(); 

            break; 

  

        // For Norma type Toast 

        // with an icon 

        case R.id.button_normal: 

            Toasty.normal( 

                      this, 

                      "This is a normal Toast", 

                      Toast.LENGTH_SHORT, 

                      ContextCompat 

                          .getDrawable( 

                              this, 

                              R.drawable.ic_android_black_24dp)) 

                .show(); 

            break; 

        } 

    } 

Screen output:

How to add a custom styled Toast in Android


That's the articleHow to add a custom styled Toast in Android

That's it for the article How to add a custom styled Toast in Android this time, hopefully can be useful for all of you. well, see you in another article post.

You are now reading the articleHow to add a custom styled Toast in Android with link addresshttps://inabnonapudyawanabing.blogspot.com/2020/12/how-to-add-custom-styled-toast-in.html

0 Response to "How to add a custom styled Toast in Android"

Post a Comment

Tips Tricks for Android Phone

Tips & Tricks for Android Phone is a free android app and Collection of Tips and Tricks related to using your android mobile device lik...