Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize toast position #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions motiontoast/src/main/java/www/sanju/motiontoast/MotionToast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.Typeface
import android.os.CountDownTimer
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.animation.AnimationUtils
Expand Down Expand Up @@ -43,6 +44,9 @@ class MotionToast {
private var infoBackgroundToastColor: Int = R.color.info_bg_color
private var deleteBackgroundToastColor: Int = R.color.delete_bg_color

private var xOffset: Int = 0
private var yOffset: Int = 0

fun resetToastColors() {
successToastColor = R.color.success_color
errorToastColor = R.color.error_color
Expand All @@ -55,6 +59,9 @@ class MotionToast {
warningBackgroundToastColor = R.color.warning_bg_color
infoBackgroundToastColor = R.color.info_bg_color
deleteBackgroundToastColor = R.color.delete_bg_color

xOffset = 0
yOffset = 0
}

fun setSuccessColor(color: Int) {
Expand Down Expand Up @@ -97,6 +104,14 @@ class MotionToast {
deleteBackgroundToastColor = color
}

fun setXOffset(offset: Int) {
xOffset = offset
}

fun setYOffset(offset: Int) {
yOffset = offset
}

// all toast CTA
fun createToast(
context: Activity,
Expand Down Expand Up @@ -1326,10 +1341,18 @@ class MotionToast {
}

private fun setGravity(position: Int, toast: Toast) {
if (position == GRAVITY_BOTTOM) {
toast.setGravity(position, 0, 100)
} else {
toast.setGravity(position, 0, 0)
when (position) {
GRAVITY_BOTTOM -> {
toast.setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, xOffset, yOffset)
}

GRAVITY_TOP -> {
toast.setGravity(Gravity.TOP or Gravity.CENTER_HORIZONTAL, xOffset, yOffset)
}

else -> {
toast.setGravity(Gravity.CENTER, xOffset, yOffset)
}
}
}

Expand Down