Skip to content

Add a toggle row #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.github.androiddevfr.form.rows

import android.content.Context
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.ToggleButton
import com.github.androiddevfr.form.core.DimensionUtils

class ToggleRow(context: Context): AbstractTitleRow<Boolean>(context) {

var toggleView: ToggleButton? = null
var textOff: String = ""
var textOn: String = ""
private var value: Boolean? = null

override fun value(): Boolean? = value

var customizeToggleView: ((ToggleRow, ToggleButton) -> Unit) = { _, _ -> }

init {
onCreateView<ToggleRow> {
val layout = RelativeLayout(context)

//Generated the Toggle
createToggle()
val toggleLayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
toggleLayoutParams.leftMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_LEFT)
toggleLayoutParams.topMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_TOP)
toggleLayoutParams.bottomMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_BOTTOM)
toggleLayoutParams.rightMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_RIGHT)
toggleLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL)
toggleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
toggleView?.layoutParams = toggleLayoutParams
layout.addView(toggleView)


//Generated the Title
createTitleView(TITLE_VIEW_ID)
val titleLayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
titleLayoutParams.leftMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_LEFT)
titleLayoutParams.topMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_TOP)
titleLayoutParams.bottomMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_BOTTOM)
titleLayoutParams.rightMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_RIGHT)
titleLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL)
titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
titleView?.layoutParams = titleLayoutParams
layout.addView(titleView)

layout
}
}

/**
* Use this lambda to change the visual aspect of the ToggleView
*/
private fun createToggle(): ToggleButton {
toggleView = ToggleButton(context)
toggleView?.text = textOff
toggleView?.textOn = textOn
toggleView?.textOff = textOff
customizeToggleView.invoke(this, toggleView as ToggleButton)
toggleView?.setOnCheckedChangeListener{ _, isChecked -> value = isChecked }
return toggleView as ToggleButton
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ class Section(private val context: Context, var title: String) {
return row(SeekBarRow(context), block)
}

/**
* Add a row with title and a Toggle
*
* ----------------------------------------
* | |
* | TITLE Toggle (On/Off) |
* | |
* ----------------------------------------
*/
fun toggleRow(block: (ToggleRow.() -> Unit)): Section {
return row(ToggleRow(context), block)
}

/**
* Add a single/multi choice row)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.util.Log
import com.github.androiddevfr.form.Form
import com.github.androiddevfr.form.rows.DateRow
import com.github.androiddevfr.form.rows.TextRow
import com.github.androiddevfr.form.rows.ToggleRow
import kotlinx.android.synthetic.main.activity_main.getValues
import java.util.Date

Expand Down Expand Up @@ -78,6 +79,15 @@ class MainActivity : AppCompatActivity() {
value = 33
}
}
section("Section 4") {
id = 7
toggleRow {
id = 8
title = "Toggle row"
textOff = "Off"
textOn = "On"
}
}
}

getValues.setOnClickListener {
Expand Down