Skip to content

Commit

Permalink
add cross fade anim and other future features
Browse files Browse the repository at this point in the history
  • Loading branch information
Damercy committed Feb 9, 2022
1 parent b0bab74 commit ef62322
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 12 deletions.
7 changes: 3 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

<dev.dayaonweb.incrementdecrementbutton.IncrementDecrementButton
android:id="@+id/btn_inc_dec"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:incrementText="+"
app:middleText="Hey"
app:decrementText="-"
android:elevation="100dp"
android:background="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package dev.dayaonweb.incrementdecrementbutton

import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.annotation.ColorRes
import androidx.annotation.Dimension
import androidx.annotation.DrawableRes
import androidx.annotation.FontRes
import androidx.core.content.res.ResourcesCompat
import androidx.core.text.isDigitsOnly
import androidx.core.view.ViewCompat
import com.google.android.material.button.MaterialButton
import com.google.android.material.shape.CornerFamily
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import com.google.android.material.textview.MaterialTextView

class IncrementDecrementButton @JvmOverloads constructor(
Expand All @@ -22,11 +31,15 @@ class IncrementDecrementButton @JvmOverloads constructor(
private var decrementText: String
private var incrementText: String
private var middleText: String
private var cornerRadius: Float
private var shape: ShapeAppearanceModel
private var enableRipple: Boolean
private var value = 0

private lateinit var btnIncrement: MaterialButton
private lateinit var btnDecrement: MaterialButton
private lateinit var btnText: MaterialTextView
private lateinit var btnRoot: LinearLayout


init {
Expand All @@ -44,6 +57,9 @@ class IncrementDecrementButton @JvmOverloads constructor(
?: DEFAULT_INCREMENT_TEXT
middleText =
getString(R.styleable.IncrementDecrementButton_middleText) ?: DEFAULT_MIDDLE_TEXT
cornerRadius = getDimension(R.styleable.IncrementDecrementButton_cornerRadius, 24.0f)
enableRipple = getBoolean(R.styleable.IncrementDecrementButton_enableRipple, true)
shape = getDefaultShape()
}
LayoutInflater.from(context).inflate(R.layout.increment_decrement_button_layout, this, true)
initializeIncDecButton()
Expand Down Expand Up @@ -75,18 +91,76 @@ class IncrementDecrementButton @JvmOverloads constructor(
setResourceText(btnText, middleText)
}

fun setCornerRadius(@Dimension radius: Float) {
cornerRadius = radius
val shapeAppearanceModel = getDefaultShape().withCornerSize(cornerRadius)
setShape(shapeAppearanceModel)
}

fun setShape(shapeAppearanceModel: ShapeAppearanceModel) {
// shape = shapeAppearanceModel
// val shapeDrawable = MaterialShapeDrawable(shape)
// ViewCompat.setBackground(btnRoot, shapeDrawable)
// invalidateLayout()
}


/**
* ****************************For release v1.2*******************************
*/
fun setColor(@ColorRes color: Int) {

}

fun setIncrementButtonColor(@ColorRes color: Int) {

}

fun setDecrementButtonColor(@ColorRes color: Int) {

}

fun setMiddleColor(@ColorRes color: Int) {

}

fun setIncrementButtonDrawable(@DrawableRes drawableRes: Int) {

}

fun setDecrementButtonDrawable(@DrawableRes drawableRes: Int) {

}

fun toggleRipple(isEnabled: Boolean) {
enableRipple = isEnabled

}

/**
* ****************************For release v1.2*******************************
*/

// public getters
fun getCurrentValue() = value

fun getDefaultShape(): ShapeAppearanceModel {
return ShapeAppearanceModel.Builder()
.setAllCorners(CornerFamily.ROUNDED, cornerRadius)
.build()
}


private fun initializeIncDecButton() {
btnIncrement = findViewById(R.id.btn_increment)
btnDecrement = findViewById(R.id.btn_decrement)
btnText = findViewById(R.id.btn_text)
btnRoot = findViewById(R.id.btn_root)
setFontFamily(fontFamily)
setIncrementButtonText(incrementText)
setDecrementButtonText(decrementText)
setMiddleText(middleText)
setShape(shape)
attachListeners()
}

Expand All @@ -100,6 +174,8 @@ class IncrementDecrementButton @JvmOverloads constructor(
onDecrementRangeCheck()
}
btnText.setOnClickListener {
if (btnText.text.isDigitsOnly())
return@setOnClickListener
value++
onIncrement()
}
Expand All @@ -108,12 +184,13 @@ class IncrementDecrementButton @JvmOverloads constructor(
private fun onIncrement() {
if (value > 0)
setResourceText(btnText, value.toString())

}

private fun onDecrementRangeCheck() {
if (value <= 0) {
setResourceText(btnText, middleText)
value = 0
setResourceText(btnText, middleText)
} else
setResourceText(btnText, value.toString())
}
Expand All @@ -126,16 +203,34 @@ class IncrementDecrementButton @JvmOverloads constructor(
private fun setResourceText(view: View, text: String) {
when (view) {
is MaterialButton -> view.text = text
is MaterialTextView -> view.text = text
is MaterialTextView -> {
view.text = text
if (value != 0)
crossFade(btnText)
}
}
invalidateLayout()
}

private fun crossFade(view: View) {
view.apply {
alpha = 0f
animate()
.alpha(1.0f)
.setDuration(500)
.start()
}
}

private fun updateRipple(view: MaterialButton, isEnabled: Boolean) {
// view.rippleColor = // TODO: Use transparent color
}


companion object {
private const val DEFAULT_FONT_SIZE = 24
private const val DEFAULT_DECREMENT_TEXT = "-"
private const val DEFAULT_INCREMENT_TEXT = "+"
private const val DEFAULT_MIDDLE_TEXT = "Add"
private const val DEFAULT_MIDDLE_TEXT = "ADD"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M19,13H5v-2h14v2z"/>
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools">

<LinearLayout
android:id="@+id/btn_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
Expand All @@ -15,9 +16,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="-"
android:textColor="@android:color/black"
android:textSize="32sp" />
android:textSize="32sp"
app:rippleColor="@android:color/transparent"
tools:text="-" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/btn_text"
Expand All @@ -27,7 +29,6 @@
android:layout_weight="0.5"
android:gravity="center"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="24sp"
tools:text="Add" />

Expand All @@ -37,9 +38,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="+"
android:textColor="@android:color/black"
android:textSize="32sp" />
android:textSize="32sp"
app:rippleColor="@android:color/transparent"
tools:text="+" />


</LinearLayout>
Expand Down
2 changes: 2 additions & 0 deletions incrementdecrementbutton/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
<attr name="decrementText" format="string"/>
<attr name="incrementText" format="string"/>
<attr name="middleText" format="string"/>
<attr name="cornerRadius" format="dimension"/>
<attr name="enableRipple" format="boolean"/>
</declare-styleable>
</resources>
Binary file added screenshots/Screenshot_20220209_224432.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/btn_basic.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ef62322

Please sign in to comment.