Search code examples
react-nativetoast

how to add image to toast in react native,is it possible?


import Toast from 'react-native-View-Toast'

here i am trying to add an image to toast to see how it works and i want to know types of toast libraries and there uses in react-native


Solution

  • You can use the setView() method to add the imageview or any view to the Toast notification, use this method to customize the Toast as needed.

    Here i have created a Custom layout file to be inflated into the Toast notification, and then i have used this layout in Toast by using setView() method.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/relativeLayout1"
      android:background="@android:color/white">
    
        <TextView
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/textView1" android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="PM is here"
            android:gravity="center"
            android:textColor="@android:color/black">
        </TextView>
    
        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:src="@drawable/new_logo"
            android:layout_below="@+id/textView1"
            android:layout_margin="5dip"
            android:id="@+id/imageView1">
        </ImageView>
    
        <TextView
            android:id="@+id/textView2"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="This is the demo of Custom Toast Notification"
            android:gravity="center"
            android:layout_below="@+id/imageView1"
            android:textColor="@android:color/black">
        </TextView>
    
    </RelativeLayout>

    for the libraries you can check at openbase.io