Skip to content

Commit

Permalink
add text appearance option to A3SeekBar.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
am3n committed Mar 9, 2023
1 parent 6ec036a commit 2d44ba8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
5 changes: 4 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/baseline_access_time_24"
android:text="text 1"
android:drawablePadding="8dp"
android:text="text 1"
app:a3_direction="locale" />

<ir.am3n.needtool.views.A3TextView
Expand Down Expand Up @@ -70,9 +70,12 @@
android:layout_width="180dp"
android:layout_height="80dp"
android:layout_margin="16dp"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:a3sb_bar_background="#ff0"
app:a3sb_bar_corner_radius="21dp"
app:a3sb_bar_progress="@color/colorPrimary"
app:a3sb_mid_placeholder_text="100%"
app:a3sb_mid_placeholder_text_color="#f00"
app:a3sb_max_value="1000"
app:a3sb_min_value="0"
app:a3sb_orientation="horizontal_ltr"
Expand Down
35 changes: 25 additions & 10 deletions needtool/src/main/java/ir/am3n/needtool/views/A3SeekBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.core.content.res.getDrawableOrThrow
import androidx.core.graphics.drawable.toBitmap
import androidx.core.view.ViewCompat
import androidx.core.widget.ImageViewCompat
import androidx.core.widget.TextViewCompat
import ir.am3n.needtool.R
import ir.am3n.needtool.asStateList
import ir.am3n.needtool.isDark
Expand Down Expand Up @@ -70,6 +71,12 @@ open class A3SeekBar @JvmOverloads constructor(
applyAttributes()
}

var textAppearance: Int = 0
set(value) {
field = value
applyAttributes()
}

var progressOnTouch = true
set(value) {
field = value
Expand Down Expand Up @@ -244,6 +251,11 @@ open class A3SeekBar @JvmOverloads constructor(

orientation = Orientation.values()[attributes.getInt(R.styleable.A3SeekBar_a3sb_orientation, 0)]

//try {
textAppearance = attributes.getResourceId(R.styleable.A3SeekBar_android_textAppearance, 0)
//} catch (_: Throwable) {
//}

progressOnTouch = attributes.getBoolean(
R.styleable.A3SeekBar_a3sb_progress_on_touch,
progressOnTouch
Expand All @@ -255,32 +267,32 @@ open class A3SeekBar @JvmOverloads constructor(

try {
barBackgroundDrawable = attributes.getDrawableOrThrow(R.styleable.A3SeekBar_a3sb_bar_background)
} catch (t: Throwable) {
} catch (_: Throwable) {
}
try {
barBackgroundStartColor =
attributes.getColorOrThrow(R.styleable.A3SeekBar_a3sb_bar_background_gradient_start)
} catch (t: Throwable) {
} catch (_: Throwable) {
}
try {
barBackgroundEndColor =
attributes.getColorOrThrow(R.styleable.A3SeekBar_a3sb_bar_background_gradient_end)
} catch (t: Throwable) {
} catch (_: Throwable) {
}

try {
barProgressDrawable = attributes.getDrawableOrThrow(R.styleable.A3SeekBar_a3sb_bar_progress)
} catch (t: Throwable) {
} catch (_: Throwable) {
}
try {
barProgressStartColor =
attributes.getColorOrThrow(R.styleable.A3SeekBar_a3sb_bar_progress_gradient_start)
} catch (t: Throwable) {
} catch (_: Throwable) {
}
try {
barProgressEndColor =
attributes.getColorOrThrow(R.styleable.A3SeekBar_a3sb_bar_progress_gradient_end)
} catch (t: Throwable) {
} catch (_: Throwable) {
}

barStrokeWidth = attributes.getDimensionPixelSize(R.styleable.A3SeekBar_a3sb_bar_stroke_width, 0)
Expand Down Expand Up @@ -374,14 +386,17 @@ open class A3SeekBar @JvmOverloads constructor(

imgMaxPlaceholder.setImageDrawable(maxPlaceholderDrawable)
txtMaxPlaceholder.text = maxPlaceholderText
TextViewCompat.setTextAppearance(txtMaxPlaceholder, textAppearance)
txtMaxPlaceholder.setTextColor(maxPlaceholderTextColor ?: Color.WHITE)

imgMidPlaceholder.setImageDrawable(midPlaceholderDrawable)
txtMidPlaceholder.text = midPlaceholderText
TextViewCompat.setTextAppearance(txtMidPlaceholder, textAppearance)
txtMidPlaceholder.setTextColor(midPlaceholderTextColor ?: Color.WHITE)

imgMinPlaceholder.setImageDrawable(minPlaceholderDrawable)
txtMinPlaceholder.text = minPlaceholderText
TextViewCompat.setTextAppearance(txtMinPlaceholder, textAppearance)
txtMinPlaceholder.setTextColor(minPlaceholderTextColor ?: Color.WHITE)

val maxPlaceholderLayoutParams = (imgMaxPlaceholder.layoutParams as LayoutParams)
Expand All @@ -400,15 +415,15 @@ open class A3SeekBar @JvmOverloads constructor(
if (progressOnTouch && !touchListening) {
val action: (View, Int) -> Unit = { bar, position ->
val fill = if (isVertical) bar.measuredHeight else bar.measuredWidth
when {
progress = when {
position in 1 until fill -> {
progress = if (isReverse)
if (isReverse)
maxValue - (position * maxValue / fill)
else
minValue + (position * maxValue / fill)
}
position <= 0 -> progress = if (isReverse) maxValue else minValue
position >= fill -> progress = if (isReverse) minValue else maxValue
position < 1 -> if (isReverse) maxValue else minValue
else -> if (isReverse) minValue else maxValue
}
}
barCardView.setOnTouchListener { bar, event ->
Expand Down
6 changes: 3 additions & 3 deletions needtool/src/main/res/layout/layout_a3seekbar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
android:layout_gravity="center"
android:contentDescription="@null" />

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtMaxPlaceholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -61,7 +61,7 @@
android:layout_gravity="center_horizontal"
android:contentDescription="@null" />

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtMidPlaceholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -83,7 +83,7 @@
android:layout_gravity="center_horizontal"
android:contentDescription="@null" />

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtMinPlaceholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions needtool/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@

<attr name="android:layout_width" />
<attr name="android:layout_height" />
<attr name="android:textAppearance" />

</declare-styleable>

Expand Down

0 comments on commit 2d44ba8

Please sign in to comment.