Skip to content
Open
Show file tree
Hide file tree
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
37 changes: 37 additions & 0 deletions alerter/src/main/java/com/tapadoo/alerter/Alert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.view.*
import android.view.animation.AccelerateInterpolator
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Button
Expand Down Expand Up @@ -52,6 +53,7 @@ class Alert @JvmOverloads constructor(context: Context,
private var enableIconPulse = true
private var enableInfiniteDuration: Boolean = false
private var enableProgress: Boolean = false
private var enableOverlay = false

private var runningAnimation: Runnable? = null

Expand Down Expand Up @@ -239,6 +241,15 @@ class Alert @JvmOverloads constructor(context: Context,
} else {
flIconContainer?.visibility = View.GONE
}

if (enableOverlay) {
overlayView.visibility = View.VISIBLE
overlayView.animate().alpha(1f)
.setDuration(ALERT_OVERLAY_FADE_IN_DURATION)
.setInterpolator(AccelerateInterpolator())
} else {
overlayView.visibility = View.GONE
}
}
}

Expand Down Expand Up @@ -269,6 +280,12 @@ class Alert @JvmOverloads constructor(context: Context,
*/
private fun hide() {
try {
if (enableOverlay) {
overlayView.animate().alpha(0f)
.setDuration(ALERT_OVERLAY_FADE_OUT_DURATION)
.setInterpolator(AccelerateInterpolator())
}

exitAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation) {
llAlertBackground?.setOnClickListener(null)
Expand Down Expand Up @@ -656,6 +673,24 @@ class Alert @JvmOverloads constructor(context: Context,
}
}

/**
* Enable or disable overlay background
*
* @param True to enable, False to disable
* */
fun enableOverlay(enable: Boolean) {
this.enableOverlay = enable
}

/**
* Set the Overlay background color from a color resource
*
* @param color The color resource
* */
fun setOverlayBackgroundColor(@ColorInt color: Int) {
overlayView.setBackgroundColor(color)
}

override fun canDismiss(): Boolean {
return isDismissible
}
Expand All @@ -681,5 +716,7 @@ class Alert @JvmOverloads constructor(context: Context,
*/
private const val DISPLAY_TIME_IN_SECONDS: Long = 3000
private const val MUL = -0x1000000
private const val ALERT_OVERLAY_FADE_IN_DURATION: Long = 700
private const val ALERT_OVERLAY_FADE_OUT_DURATION: Long = 300
}
}
25 changes: 25 additions & 0 deletions alerter/src/main/java/com/tapadoo/alerter/Alerter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,31 @@ class Alerter private constructor() {
activityWeakReference = WeakReference(activity)
}


/**
* Set the overlay background color from a color resource
*
* @param color The color resource
* @return This Alerter
*/
fun setOverlayBackground(@ColorRes colorResId: Int): Alerter {
activityWeakReference?.get()?.let {
alert?.setOverlayBackgroundColor(ContextCompat.getColor(it, colorResId))
}
return this
}

/**
* Enable or disable overlay background
*
* @param enable True to enable, False to disable
* @return This Alerter
*/
fun enableOverlay(enable: Boolean): Alerter {
alert?.enableOverlay(enable)
return this
}

companion object {

private var activityWeakReference: WeakReference<Activity>? = null
Expand Down
10 changes: 10 additions & 0 deletions alerter/src/main/res/layout/alerter_alert_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
android:background="@android:color/transparent"
android:clickable="false">

<View
android:id="@+id/overlayView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"
android:background="@color/alert_default_overlay_color"
android:clickable="true"
android:focusable="true"
android:visibility="gone" />

<LinearLayout
android:id="@+id/llAlertBackground"
style="@style/AlertStyle"
Expand Down
1 change: 1 addition & 0 deletions alerter/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<color name="alert_default_error_background">#F44336</color>
<color name="alert_default_text_color">@android:color/white</color>
<color name="alert_default_icon_color">@android:color/white</color>
<color name="alert_default_overlay_color">#4D000000</color>
</resources>
26 changes: 20 additions & 6 deletions app/src/main/java/com/tapadoo/alerter/demo/JavaDemoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.graphics.Typeface;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

Expand Down Expand Up @@ -41,6 +42,7 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(com.tapadoo.alerter.demo.R.id.btnAlertWithCustomFont).setOnClickListener(this);
findViewById(com.tapadoo.alerter.demo.R.id.btnAlertSwipeToDismissEnabled).setOnClickListener(this);
findViewById(com.tapadoo.alerter.demo.R.id.btnAlertSound).setOnClickListener(this);
findViewById(com.tapadoo.alerter.demo.R.id.btnShowAlertWithOverlay).setOnClickListener(this);
}

@Override
Expand Down Expand Up @@ -73,12 +75,14 @@ public void onClick(View view) {
showAlertWithCustomFont();
} else if (i == com.tapadoo.alerter.demo.R.id.btnAlertSwipeToDismissEnabled) {
showAlertSwipeToDismissEnabled();
} else if (i == com.tapadoo.alerter.demo.R.id.btnAlertSound){
} else if (i == com.tapadoo.alerter.demo.R.id.btnAlertSound) {
showAlertSound();
}else if (i == com.tapadoo.alerter.demo.R.id.btnCenterAlert){
} else if (i == com.tapadoo.alerter.demo.R.id.btnCenterAlert) {
showAlertFromCenter();
}else if (i == com.tapadoo.alerter.demo.R.id.btnBottomAlert){
} else if (i == com.tapadoo.alerter.demo.R.id.btnBottomAlert) {
showAlertFromBottom();
} else if (i == com.tapadoo.alerter.demo.R.id.btnShowAlertWithOverlay) {
showAlerterWithOverlay();
} else {
showAlertDefault();
}
Expand Down Expand Up @@ -132,12 +136,12 @@ private void showAlertVerbose() {
Alerter.create(JavaDemoActivity.this)
.setTitle("Alert Title")
.setText("The alert scales to accommodate larger bodies of text. " +
"The alert scales to accommodate larger bodies of text. " +
"The alert scales to accommodate larger bodies of text.")
"The alert scales to accommodate larger bodies of text. " +
"The alert scales to accommodate larger bodies of text.")
.show();
}

private void showAlertCallbacks(){
private void showAlertCallbacks() {
Alerter.create(JavaDemoActivity.this)
.setTitle("Alert Title")
.setText("Alert text...")
Expand Down Expand Up @@ -245,4 +249,14 @@ private void showAlertFromBottom() {
.setLayoutGravity(Gravity.BOTTOM)
.show();
}

private void showAlerterWithOverlay() {
Alerter.create(JavaDemoActivity.this)
.setTitle("Alert Title")
.setText("Alert text...")
.enableOverlay(true)
.setOverlayBackground(R.color.alert_overlay_color)
.setBackgroundColorRes(com.tapadoo.alerter.demo.R.color.colorAccent)
.show();
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/tapadoo/alerter/demo/KotlinDemoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ class KotlinDemoActivity : AppCompatActivity() {
.show()
}

private fun showAlerterWithOverlay() {
Alerter.create(this)
.setTitle("Alert Title")
.setText("Alert text...")
.enableOverlay(true)
.setOverlayBackground(R.color.alert_overlay_color)
.setBackgroundColorRes(R.color.colorAccent)
.show()
}

private fun setupOnClickListeners() {
btnAlertDefault.setOnClickListener {
showAlertDefault()
Expand Down Expand Up @@ -259,5 +269,9 @@ class KotlinDemoActivity : AppCompatActivity() {
btnBottomAlert.setOnClickListener {
showAlertFromBottom()
}

btnShowAlertWithOverlay.setOnClickListener {
showAlerterWithOverlay()
}
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/layout/content_example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@
android:text="@string/default_bottom_alert"
android:textColor="@android:color/white" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnShowAlertWithOverlay"
style="@style/ExampleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/with_overlay" />
</LinearLayout>

</ScrollView>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<color name="colorPrimary">#63C9D7</color>
<color name="colorPrimaryDark">#2c9cac</color>
<color name="colorAccent">#F99143</color>
<color name="alert_overlay_color">#80000000</color>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
<string name="with_custom_layout">With Custom Layout</string>
<string name="default_bottom_alert">Default Bottom Alert</string>
<string name="default_center_alert">Default Center Alert</string>
<string name="with_overlay">With overlay</string>
</resources>