diff --git a/androidform/src/main/java/com/github/androiddevfr/form/rows/SwitchRow.kt b/androidform/src/main/java/com/github/androiddevfr/form/rows/SwitchRow.kt new file mode 100644 index 0000000..28aee24 --- /dev/null +++ b/androidform/src/main/java/com/github/androiddevfr/form/rows/SwitchRow.kt @@ -0,0 +1,60 @@ +package com.github.androiddevfr.form.rows + +import android.content.Context +import android.view.ViewGroup +import android.widget.RelativeLayout +import android.widget.Switch +import com.github.androiddevfr.form.core.DimensionUtils + +class SwitchRow (context: Context): AbstractTitleRow(context) { + + var switch: Switch? = null + private var value: Boolean? = null + + override fun value(): Boolean? = value + + var customizeSwitchView: ((SwitchRow, Switch) -> Unit) = { _, _ -> } + + init { + onCreateView { + val layout = RelativeLayout(context) + + //Generated the Switch + createSwitch() + val switchLayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) + switchLayoutParams.leftMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_LEFT) + switchLayoutParams.topMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_TOP) + switchLayoutParams.bottomMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_BOTTOM) + switchLayoutParams.rightMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_RIGHT) + switchLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL) + switchLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT) + switch?.layoutParams = switchLayoutParams + layout.addView(switch) + + //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 SwitchView + */ + private fun createSwitch(): Switch { + switch = Switch(context) + customizeSwitchView.invoke(this, switch as Switch) + switch?.setOnCheckedChangeListener{ _, isChecked -> value = isChecked } + return switch as Switch + } + +} \ No newline at end of file diff --git a/androidform/src/main/java/com/github/androiddevfr/form/section/Section.kt b/androidform/src/main/java/com/github/androiddevfr/form/section/Section.kt index d93e5a2..3bc84af 100644 --- a/androidform/src/main/java/com/github/androiddevfr/form/section/Section.kt +++ b/androidform/src/main/java/com/github/androiddevfr/form/section/Section.kt @@ -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 Switch + * + * ---------------------------------------- + * | | + * | TITLE Switch (On/Off) | + * | | + * ---------------------------------------- + */ + fun switchRow(block: (SwitchRow.() -> Unit)): Section { + return row(SwitchRow(context), block) + } + /** * Add a single/multi choice row) * diff --git a/sample-kt/src/main/java/com/github/androiddevfr/form/kotlin/sample/MainActivity.kt b/sample-kt/src/main/java/com/github/androiddevfr/form/kotlin/sample/MainActivity.kt index 6e4dbc9..558cc81 100644 --- a/sample-kt/src/main/java/com/github/androiddevfr/form/kotlin/sample/MainActivity.kt +++ b/sample-kt/src/main/java/com/github/androiddevfr/form/kotlin/sample/MainActivity.kt @@ -78,6 +78,14 @@ class MainActivity : AppCompatActivity() { value = 33 } } + + section("Section 4") { + id = 8 + switchRow { + id = 9 + title = "Switch row" + } + } } getValues.setOnClickListener {