androidtoastandroid-toast

custom toast not showing message


I've created some custom toast layout and have a problem with it !
My custom toast doesn't show my message and shows This Image
It's my layout code :

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="24dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/selector_custom_toast">

<TextView
    android:id="@+id/tv_toast_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center_vertical|center_horizontal"
    android:lineSpacingExtra="4sp"
    android:singleLine="false"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/iv_toast"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast " />

<ImageView
    android:id="@+id/iv_toast"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="8dp"
    android:adjustViewBounds="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_error_outline_red_24dp"
    tools:ignore="ContentDescription" />

and it's my toaster function code :

    public void toaster(String message, int length, int form) {
    Toast toast = new Toast(context);
    if (toastLayout == null) {
        toastLayout = LayoutInflater.from(context).inflate(R.layout.custom_toast, null);
    }

    TextView toastMessage = toastLayout.findViewById(R.id.tv_toast_message);
    toastMessage.setText(message);
    toastMessage.setTypeface(MyApplication.getIranianSansFont(context));
    ImageView toastImage = toastLayout.findViewById(R.id.iv_toast);
    if (form == SUCCESS) {
        toastImage.setImageResource(R.drawable.ic_check_circle_green_24dp);
        toastMessage.setTextColor(context.getResources().getColor(R.color.greenSuccess));
    } else if (form == ERROR) {
        toastImage.setImageResource(R.drawable.ic_error_outline_red_24dp);
        toastMessage.setTextColor(context.getResources().getColor(R.color.redError));
    }

    toast.setDuration(length);
    toast.setView(toastLayout);
    toast.show();
}

My text view height should be wrap content and width is match constraint but when I set both of them to wrap content the problem goes away and everything work just fine except the vision of the toast ! toast layout stick around the screen and it's margins get removed and message text get under the image view screenshot
can anyone help me fix my problem ?


Solution

  • I've Just Used Linear Layout instead of Constraint Layout , it was something wrong with Constraint Layout !
    My Linear Layout Code :

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginBottom="24dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="@drawable/selector_custom_toast">
    
    <TextView
        android:id="@+id/tv_toast_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start|center_vertical"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal"
        android:lineSpacingExtra="4sp"
        android:singleLine="false"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textDirection="rtl"
        tools:text="Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast " />
    
    <ImageView
        android:id="@+id/iv_toast"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="end|center_vertical"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:adjustViewBounds="true"
        app:srcCompat="@drawable/ic_error_outline_red_24dp"
        tools:ignore="ContentDescription" />