Skip to content
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

Add a switch row #9

Open
wants to merge 1 commit 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,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<Boolean>(context) {

var switch: Switch? = null
private var value: Boolean? = null

override fun value(): Boolean? = value

var customizeSwitchView: ((SwitchRow, Switch) -> Unit) = { _, _ -> }

init {
onCreateView<SwitchRow> {
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
}

}
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 Switch
*
* ----------------------------------------
* | |
* | TITLE Switch (On/Off) |
* | |
* ----------------------------------------
*/
fun switchRow(block: (SwitchRow.() -> Unit)): Section {
return row(SwitchRow(context), block)
}

/**
* Add a single/multi choice row)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ class MainActivity : AppCompatActivity() {
value = 33
}
}

section("Section 4") {
id = 8
switchRow {
id = 9
title = "Switch row"
}
}
}

getValues.setOnClickListener {
Expand Down