Skip to content

Commit

Permalink
Adds sample for duration, background, overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
Aritra Roy committed Apr 19, 2018
1 parent eba2e30 commit 29be965
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 21 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ Flashbar.Builder(this)
.build()
```

You can specify the duration (in millis) for which you want the flashbar to be displayed. The default duration is infinite, i.e. it won't dismiss automatically if you do not specify any duration.

```
Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.duration(500)
.message("This is a flashbar with duration")
.build()
```

## Gravity
You can show the flashbar either at the top or at the bottom of the screen using the gravity property. By default it is shown at the bottom.

Expand Down Expand Up @@ -105,4 +115,50 @@ Flashbar.Builder(this)
.build()
```

## Background & Overlay
You can change the background color of the flashbar and add a modal overlay as well.

#### Background
```
Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("The background color can be changed to any color of your choice.")
.backgroundColorRes(R.color.colorPrimaryDark)
.build()
```
You can also change the background using drawables, like have a cool gradient effect.

```
Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("You can have gradients by setting background drawable.")
.backgroundDrawable(R.drawable.bg_gradient)
.build()
```
#### Overlay

```
Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("You can show a modal overlay to give a dim effect in the entire screen.")
.backgroundColorRes(R.color.colorPrimaryDark)
.showOverlay()
.build()
```
You can also customize the overlay color using `overlayColor()` and also make the overlay consume click/touch events using `overlayBlockable()`.

```
Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("You can show a modal overlay to give a dim effect in the entire screen.")
.backgroundColorRes(R.color.colorPrimaryDark)
.showOverlay()
.overlayColorRes(R.color.modal)
.overlayBlockable()
.build()
```

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
@Override
public void onClick(View v) {
if (flashbar == null) {
flashbar = messageAdvanced();
flashbar = overlayAdvanced();
}
flashbar.show();
}
Expand All @@ -50,6 +50,14 @@ private Flashbar basic() {
.build();
}

private Flashbar basicDuration() {
return new Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.duration(500)
.message("This is a flashbar with duration")
.build();
}

private Flashbar gravityTop() {
return new Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
Expand Down Expand Up @@ -100,4 +108,44 @@ private Flashbar messageAdvanced() {
Typeface.createFromAsset(getAssets(), "BeautifulAndOpenHearted.ttf"))
.build();
}

private Flashbar background() {
return new Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("The background color can be changed to any color of your choice.")
.backgroundColorRes(R.color.colorPrimaryDark)
.build();
}

private Flashbar backgroundDrawable() {
return new Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("The background color can be changed to any color of your choice.")
.backgroundDrawable(R.drawable.bg_gradient)
.build();
}

private Flashbar overlay() {
return new Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("You can show a modal overlay to give a dim effect in the entire screen.")
.backgroundColorRes(R.color.colorPrimaryDark)
.showOverlay()
.build();
}

private Flashbar overlayAdvanced() {
return new Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("You can show a modal overlay to give a dim effect in the entire screen.")
.backgroundColorRes(R.color.colorPrimaryDark)
.showOverlay()
.overlayColorRes(R.color.modal)
.overlayBlockable()
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ class KotlinSampleActivity : AppCompatActivity() {
.build()
}

private fun basicDuration(): Flashbar {
return Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.duration(500)
.message("This is a flashbar with duration")
.build()
}

private fun gravityTop(): Flashbar {
return Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
Expand Down Expand Up @@ -82,4 +90,44 @@ class KotlinSampleActivity : AppCompatActivity() {
.messageTypeface(Typeface.createFromAsset(assets, "BeautifulAndOpenHearted.ttf"))
.build()
}

private fun background(): Flashbar {
return Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("The background color can be changed to any color of your choice.")
.backgroundColorRes(R.color.colorPrimaryDark)
.build()
}

private fun backgroundDrawable(): Flashbar {
return Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("You can have gradients by setting background drawable.")
.backgroundDrawable(R.drawable.bg_gradient)
.build()
}

private fun overlay(): Flashbar {
return Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("You can show a modal overlay to give a dim effect in the entire screen.")
.backgroundColorRes(R.color.colorPrimaryDark)
.showOverlay()
.build()
}

private fun overlayAdvanced(): Flashbar {
return Flashbar.Builder(this)
.gravity(Flashbar.Gravity.TOP)
.title("Hello World!")
.message("You can show a modal overlay to give a dim effect in the entire screen.")
.backgroundColorRes(R.color.colorPrimaryDark)
.showOverlay()
.overlayColorRes(R.color.modal)
.overlayBlockable()
.build()
}
}
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/bg_gradient.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<gradient
android:angle="-90"
android:endColor="@color/colorPrimaryLight"
android:startColor="@color/colorPrimaryDark"
android:type="linear" />

</shape>
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 @@ -4,4 +4,5 @@
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="modal">#80272727</color>
<color name="colorPrimaryLight">#5C6BC0</color>
</resources>
29 changes: 18 additions & 11 deletions flashbar/src/main/java/com/andrognito/flashbar/Flashbar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ class Flashbar private constructor(private var builder: Builder) {
setBarDismissListener(builder.onBarDismissListener)
setBarDismissOnTapOutside(builder.barDismissOnTapOutside)
setOnTapOutsideListener(builder.onTapOutsideListener)
setModalOverlayColor(builder.modalOverlayColor)
setModalOverlayBlockable(builder.modalOverlayBlockable)
setOverlay(builder.overlay)
setOverlayColor(builder.overlayColor)
setOverlayBlockable(builder.overlayBlockable)
setVibrationTargets(builder.vibrationTargets)
setIconAnimator(builder.iconAnimator)

Expand Down Expand Up @@ -148,13 +149,13 @@ class Flashbar private constructor(private var builder: Builder) {
internal var onBarDismissListener: OnBarDismissListener? = null
internal var barDismissOnTapOutside: Boolean = false
internal var onTapOutsideListener: OnTapOutsideListener? = null
internal var modalOverlayColor: Int? = null
internal var modalOverlayBlockable: Boolean = false
internal var overlay: Boolean = false
internal var overlayColor: Int = ContextCompat.getColor(activity, R.color.modal)
internal var overlayBlockable: Boolean = false
internal var castShadow: Boolean = true
internal var shadowStrength: Int? = null
internal var enableSwipeToDismiss: Boolean = false
internal var vibrationTargets: List<Vibration> = emptyList()
internal var progressPosition: ProgressPosition? = null

internal var title: String? = null
internal var titleSpanned: Spanned? = null
Expand Down Expand Up @@ -205,6 +206,8 @@ class Flashbar private constructor(private var builder: Builder) {
internal var iconColorFilter: Int? = null
internal var iconColorFilterMode: PorterDuff.Mode? = null
internal var iconAnimator: Animator? = null

internal var progressPosition: ProgressPosition? = null
internal var progressTint: Int? = null
internal var progressTintMode: PorterDuff.Mode? = null

Expand Down Expand Up @@ -285,25 +288,29 @@ class Flashbar private constructor(private var builder: Builder) {
this.barDismissOnTapOutside = true
}

/**
* Specifies if modal overlay should be shown
*/
fun showOverlay() = apply { this.overlay = true }

/**
* Specifies modal overlay color
* Modal overlay is automatically shown if color is set
*/
fun modalOverlayColor(@ColorInt color: Int) = apply { this.modalOverlayColor = color }
fun overlayColor(@ColorInt color: Int) = apply { this.overlayColor = color }

/**
* Specifies modal overlay color resource
* Modal overlay is automatically shown if color is set
*/
fun modalOverlayColorRes(@ColorRes colorId: Int) = apply {
this.modalOverlayColor = ContextCompat.getColor(activity, colorId)
fun overlayColorRes(@ColorRes colorId: Int) = apply {
this.overlayColor = ContextCompat.getColor(activity, colorId)
}

/**
* Specifies if modal overlay is blockable and should comsume touch events
*/
fun modalOverlayBlockable(blockable: Boolean) = apply {
this.modalOverlayBlockable = blockable
fun overlayBlockable() = apply {
this.overlayBlockable = true
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ internal class FlashbarContainerView(context: Context)
private var onBarShowListener: Flashbar.OnBarShowListener? = null
private var onBarDismissListener: Flashbar.OnBarDismissListener? = null
private var onTapOutsideListener: Flashbar.OnTapOutsideListener? = null
private var modalOverlayColor: Int? = null
private var overlayColor: Int? = null
private var iconAnimator: Animator? = null

private var duration = DURATION_INDEFINITE
private var isBarShowing = false
private var isBarShown = false
private var isBarDismissing = false
private var barDismissOnTapOutside: Boolean = false
private var modalOverlayBlockable: Boolean = false
private var showOverlay: Boolean = false
private var overlayBlockable: Boolean = false

override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
Expand Down Expand Up @@ -97,10 +98,10 @@ internal class FlashbarContainerView(context: Context)
internal fun construct() {
isHapticFeedbackEnabled = true

if (modalOverlayColor != null) {
setBackgroundColor(modalOverlayColor!!)
if (showOverlay) {
setBackgroundColor(overlayColor!!)

if (modalOverlayBlockable) {
if (overlayBlockable) {
isClickable = true
isFocusable = true
}
Expand Down Expand Up @@ -195,12 +196,16 @@ internal class FlashbarContainerView(context: Context)
this.onTapOutsideListener = listener
}

internal fun setModalOverlayColor(color: Int?) {
this.modalOverlayColor = color
internal fun setOverlay(overlay: Boolean) {
this.showOverlay = overlay
}

internal fun setModalOverlayBlockable(blockable: Boolean) {
this.modalOverlayBlockable = blockable
internal fun setOverlayColor(color: Int) {
this.overlayColor = color
}

internal fun setOverlayBlockable(blockable: Boolean) {
this.overlayBlockable = blockable
}

internal fun setEnterAnim(builder: FlashAnimBuilder) {
Expand Down
1 change: 1 addition & 0 deletions flashbar/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<color name="white">#fff</color>
<color name="shadow_color">#94444444</color>
<color name="translucent_black">#22000000</color>
<color name="modal">#80272727</color>
</resources>

0 comments on commit 29be965

Please sign in to comment.