Skip to content

Instantly share code, notes, and snippets.

View lukelorusso's full-sized avatar
🤖
var droidMood = true

Luke Lorusso lukelorusso

🤖
var droidMood = true
View GitHub Profile
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
...>
<androidx.cardview.widget.CardView
android:id="@+id/barCardView"
...
android:layout_gravity="center_horizontal"
app:cardElevation="0dp">
open class VerticalSeekBar constructor(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {
companion object {
private const val DEFAULT_MAX_VALUE = 100
private const val DEFAULT_DRAWABLE_BACKGROUND: String = "#f6f6f6"
private const val DEFAULT_DRAWABLE_PROGRESS_START: String = "#4D88E1"
private const val DEFAULT_DRAWABLE_PROGRESS_END: String = "#7BA1DB"
}
private var onProgressChangeListener: ((Int) -> Unit)? = null
@lukelorusso
lukelorusso / VerticalSeekBar.kt
Last active June 28, 2019 15:30
applyAttributes() -> thumb.setOnTouchListener
// [...]
private fun applyAttributes() {
// [...]
// here we intercept the click on the thumb
thumb.setOnTouchListener { thumb, event ->
val rawY = event.rawY.roundToInt()
when (event.action and MotionEvent.ACTION_MASK) {
MotionEvent.ACTION_DOWN -> { // here we get the max top y coordinate (yDelta)
yDelta = rawY +
(barCardView.layoutParams as LayoutParams).topMargin -
@lukelorusso
lukelorusso / VerticalSeekBar.kt
Last active June 28, 2019 15:31
applyAttributes() -> barCardView.setOnTouchListener
// [...]
private fun applyAttributes() {
// [...]
// here we intercept the click on the bar
barCardView.setOnTouchListener { drawable, event ->
val positionY = event.y.roundToInt()
val action = {
val fillHeight = bar.measuredHeight
when { // here we update progress
positionY in 1 until fillHeight -> {
@lukelorusso
lukelorusso / VerticalSeekBar.kt
Last active June 28, 2019 15:32
updateViews()
// [...]
private fun updateViews() {
post {
val barCardViewLayoutParams = barCardView.layoutParams as LayoutParams
val fillHeight = height - barCardViewLayoutParams.topMargin - barCardViewLayoutParams.bottomMargin
val marginByProgress = fillHeight - (progress * fillHeight / maxValue)
thumb.layoutParams = (thumb.layoutParams as LayoutParams).apply {
topMargin = marginByProgress
val thumbCardViewHalfHeight = if (showThumb) thumbCardView.measuredHeight / 2 else 0
if (barCardViewLayoutParams.topMargin > thumbCardViewHalfHeight) {
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editCodeReal"
android:layout_width="1dp"
android:layout_height="1dp"
@lukelorusso
lukelorusso / CodeEditText.kt
Last active August 27, 2019 11:14
onAttachedToWindow()
var text: Editable = "".toEditable()
set(value) {
field = value
renderCode() // let's keep this for later
}
...
override fun onAttachedToWindow() {
super.onAttachedToWindow()
...
@lukelorusso
lukelorusso / CodeEditText.kt
Created August 27, 2019 15:21
renderCode()
private fun renderCode() {
for (i in 0 until llCodeWrapper.childCount) {
val itemContainer = llCodeWrapper.getChildAt(i)
itemContainer.findViewById<TextView>(R.id.tvCode).text =
if (text.length > i)
(if (maskTheCode) codeMaskChar else text[i])
.toString()
else ""
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"><!-- vertical scrolling purpose -->
<LinearLayout
android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="item_fixed_width">70dp</dimen>
</resources>