11package com.maliotis.iosalert
22
3+ import android.animation.ValueAnimator
34import android.app.Activity
4- import android.app.Dialog
5- import android.content.res.Resources
65import android.graphics.Color
76import android.graphics.Typeface
87import android.graphics.drawable.ColorDrawable
98import android.graphics.drawable.GradientDrawable
109import android.os.Bundle
11- import android.util.Log
12- import android.util.TypedValue
13- import android.view.LayoutInflater
14- import android.view.View
15- import android.view.ViewGroup
10+ import android.view.*
1611import android.view.ViewGroup.LayoutParams.MATCH_PARENT
17- import android.view.ViewOutlineProvider
1812import android.widget.Button
1913import android.widget.LinearLayout
2014import android.widget.TextView
2115import androidx.annotation.FloatRange
2216import androidx.annotation.IntRange
2317import androidx.appcompat.app.AppCompatActivity
24- import androidx.core.graphics.toColor
2518import androidx.fragment.app.DialogFragment
2619import com.maliotis.iosalert.blurview.BlurView
2720import com.maliotis.iosalert.blurview.RenderScriptBlur
@@ -42,7 +35,11 @@ class IOSAlert private constructor(private val activity1: Activity,
4235 var iosNegativeClickListener : IOSClickListener ? ): DialogFragment() {
4336
4437 var blurRadius: Float = 25f
45- var backgroundColor: Int? = null
38+ var backgroundColor: Int = 0xDFFFFFFF .toInt()
39+ var tintButtons: Boolean = true
40+ var tintButtonsColor: Int = 0
41+ var cornerRadius: Float = 10f
42+
4643
4744 /* *
4845 * Builder class to build a dialog
@@ -54,8 +51,11 @@ class IOSAlert private constructor(private val activity1: Activity,
5451 var typeface: Typeface ? = null
5552 var positiveText: String? = null
5653 var negativeText: String? = null
57- var backgroundColor: Int? = null
54+ var backgroundColor: Int = 0xDFFFFFFF .toInt()
5855 var isCancellable: Boolean = true
56+ var tintButtons: Boolean = true
57+ var tintButtonsColor: Int = 0x38D3D3D3
58+ var cornerRadius: Float = 10f
5959 var iosPositiveClickListener: IOSClickListener = object : IOSClickListener {}
6060 var iosNegativeClickListener: IOSClickListener ? = null
6161 private var blurRadius: Float = 25f
@@ -107,6 +107,16 @@ class IOSAlert private constructor(private val activity1: Activity,
107107 */
108108 fun backgroundColor (color : Int ) = apply { backgroundColor = color }
109109
110+ /* *
111+ * Set tintButtons
112+ */
113+ fun tintButtons (tintButtons : Boolean ) = apply { this .tintButtons = tintButtons }
114+
115+ /* *
116+ * Set tintButtonsColor
117+ */
118+ fun tintButtonsColor (tintButtonsColor : Int ) = apply { this .tintButtonsColor = tintButtonsColor }
119+
110120 /* *
111121 * Sets the fragments isCancellable attribute to true or false
112122 * [DialogFragment.isCancelable]
@@ -121,6 +131,8 @@ class IOSAlert private constructor(private val activity1: Activity,
121131 blurRadius = radius
122132 }
123133
134+ fun cornerRadius (cornerRadius : Float ) = apply { this .cornerRadius = cornerRadius }
135+
124136 /* *
125137 * Will build and return the iOSAlert instance with the parameters specified in the Builder
126138 */
@@ -129,6 +141,9 @@ class IOSAlert private constructor(private val activity1: Activity,
129141 iosAlert!! .blurRadius = blurRadius
130142 iosAlert!! .backgroundColor = backgroundColor
131143 iosAlert!! .isCancelable = isCancellable
144+ iosAlert!! .tintButtons = tintButtons
145+ iosAlert!! .tintButtonsColor = tintButtonsColor
146+ iosAlert!! .cornerRadius = cornerRadius
132147 return iosAlert!!
133148 }
134149
@@ -151,7 +166,7 @@ class IOSAlert private constructor(private val activity1: Activity,
151166 val blurView: BlurView = v.findViewById(R .id.blurView)
152167 val titleTextView: TextView = v.findViewById(R .id.popUpTitle)
153168 val bodyTextView: TextView = v.findViewById(R .id.popUpBody)
154- val okButton : Button = v.findViewById(R .id.popUpOkButton)
169+ val positiveButton : Button = v.findViewById(R .id.popUpOkButton)
155170 var negativeButton: Button ? = null
156171
157172 if (body.isNullOrEmpty()) blurView.findViewById<LinearLayout >(R .id.layoutInsideBlurView).removeView(bodyTextView)
@@ -161,7 +176,7 @@ class IOSAlert private constructor(private val activity1: Activity,
161176 titleTextView.text = title
162177 positiveText = positiveText ? : " OK"
163178
164- val positiveClickListener = getOkButtonClickListener ()
179+ val positiveClickListener = getPositiveButtonClickListener ()
165180 if (negativeText != null || iosNegativeClickListener != null ) {
166181 // Add a negative button
167182 negativeButton = getNegativeButton()
@@ -170,8 +185,11 @@ class IOSAlert private constructor(private val activity1: Activity,
170185 linearLayout.addView(negativeButton, 0 )
171186
172187 }
173- okButton.setOnClickListener(positiveClickListener)
188+ positiveButton.setOnClickListener(positiveClickListener)
189+ if (tintButtons)
190+ positiveButton.setOnTouchListener(getOnTouchTintListener())
174191 // set typeface
192+ setTypefaceSettings(titleTextView, bodyTextView, positiveButton, negativeButton)
175193
176194 dialog?.window?.setBackgroundDrawable(ColorDrawable (Color .TRANSPARENT ))
177195
@@ -181,12 +199,12 @@ class IOSAlert private constructor(private val activity1: Activity,
181199 val rootView = decorView.findViewById<View >(android.R .id.content) as ViewGroup
182200 val windowBackground = decorView.background
183201
184- if (backgroundColor != null ) {
185- val backgroundDrawable = GradientDrawable ()
186- backgroundDrawable.cornerRadius = dpToPixel(10f )
187- backgroundDrawable.setColor(backgroundColor!! )
188- blurView.background = backgroundDrawable
189- }
202+
203+ val backgroundDrawable = GradientDrawable ()
204+ backgroundDrawable.cornerRadius = dpToPixel(cornerRadius, resources )
205+ backgroundDrawable.setColor(backgroundColor)
206+ blurView.background = backgroundDrawable
207+
190208
191209 blurView.setupWith(rootView)
192210 .setFrameClearDrawable(windowBackground)
@@ -200,7 +218,20 @@ class IOSAlert private constructor(private val activity1: Activity,
200218 return v
201219 }
202220
203- private fun getOkButtonClickListener (): View .OnClickListener {
221+ /* *
222+ * Check if the view exists and if it is of type TextView or Subclass of that assign the typeface
223+ */
224+ private fun setTypefaceSettings (vararg views : View ? ) {
225+ for (v in views) {
226+ if (v != null ) {
227+ if (v is TextView ) {
228+ v.typeface = typeface
229+ }
230+ }
231+ }
232+ }
233+
234+ private fun getPositiveButtonClickListener (): View .OnClickListener {
204235 return View .OnClickListener {
205236 iosPositiveClickListener.onClick(dialog)
206237 }
@@ -214,41 +245,59 @@ class IOSAlert private constructor(private val activity1: Activity,
214245 }
215246 }
216247
248+ private fun getOnTouchTintListener (): View .OnTouchListener {
249+ return View .OnTouchListener { v, event ->
250+ if (event.action == MotionEvent .ACTION_DOWN ) {
251+ val background = ColorDrawable ()
252+ val anim = ValueAnimator .ofArgb(Color .TRANSPARENT , tintButtonsColor)
253+ anim.addUpdateListener {
254+ val value = it.animatedValue as Int
255+ background.color = value
256+ v.background = background
257+
258+ }
259+ anim.duration = 150
260+ anim.start()
261+
262+ } else if (event.action == MotionEvent .ACTION_UP ) {
263+
264+ val background = ColorDrawable ()
265+ val anim = ValueAnimator .ofArgb(tintButtonsColor, Color .TRANSPARENT )
266+ anim.addUpdateListener {
267+ val value = it.animatedValue as Int
268+ background.color = value
269+ v.background = background
270+
271+ }
272+ anim.duration = 150
273+ anim.start()
274+ }
275+
276+ false
277+ }
278+ }
279+
217280 private fun getNegativeButton (): Button {
218281 return Button (activity1).apply {
219282 setTextColor(Color .RED )
220283 text = negativeText ? : " CANCEL"
221284 if (this @IOSAlert.typeface != null )
222285 typeface = this @IOSAlert.typeface
223286
224- layoutParams = LinearLayout .LayoutParams (MATCH_PARENT , dpToPixel( 44f ).toInt() , 0.5f )
225- setBackgroundColor (Color .TRANSPARENT )
287+ layoutParams = LinearLayout .LayoutParams (MATCH_PARENT , MATCH_PARENT , 0.5f )
288+ background = ColorDrawable (Color .TRANSPARENT )
226289 setOnClickListener(getNegativeButtonClickListener())
290+ if (tintButtons)
291+ setOnTouchListener(getOnTouchTintListener())
227292 }
228293 }
229294
230295 private fun getLineSeparator (): View {
231296 return View (activity1).apply {
232- layoutParams = LinearLayout .LayoutParams (dpToPixel(0.5f ).toInt(), MATCH_PARENT )
297+ layoutParams = LinearLayout .LayoutParams (dpToPixel(0.5f , resources ).toInt(), MATCH_PARENT )
233298 setBackgroundColor(Color .parseColor(" #9FAAAAAA" ))
234299
235300 }
236301 }
237302
238- private fun dpToPixel (dp : Float ): Float {
239- val r: Resources = resources
240- return TypedValue .applyDimension(
241- TypedValue .COMPLEX_UNIT_DIP ,
242- dp,
243- r.displayMetrics
244- )
245-
246- }
247-
248- }
249-
250- interface IOSClickListener {
251- fun onClick (dialog : Dialog ? ) {
252- dialog?.dismiss()
253- }
254303}
0 commit comments