Search code examples
androidxmlkotlinlayouttoast

Android toast message set margin doesn't work


Android toast message set margin is not working. I want to fill horizontal and bottom gravity toast message with 16dp end and start margins. Please help me

custom_toast_message_layout.xml

 <androidx.constraintlayout.widget.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="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/bg_rounded_corner"
    android:backgroundTint="@color/snackBarSuccess"
    android:layout_margin="16dp"
    android:padding="16dp">

    <TextView
        android:id="@+id/comment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/hanken_sans_bold"
        android:maxLines="1"
        android:singleLine="true"
        android:text="Comment is here"
        android:textColor="@color/white"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

        val view : View = LayoutInflater.from(requireContext())
            .inflate(R.layout.custom_toast_message_layout, null)
        val toast = Toast(requireContext())
        toast.view = view
        toast.setGravity(Gravity.BOTTOM or Gravity.FILL_HORIZONTAL, 0, 100)
        toast.setMargin(0.2f, 0f)
        toast.duration = Toast.LENGTH_LONG
        toast.show()

thanks


Solution

  • try changing your custom_toast_message_layout.xml like below

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/toast_root_layout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg"
            android:backgroundTint="@color/snackBarSuccess"
            >
    
            <TextView
                android:id="@+id/toast_message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:padding="8dp"
                android:text="This is a comment"
                android:textColor="@color/white"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Referenced https://stackoverflow.com/a/63746852/9502601