Skip to content

Commit

Permalink
FEAT: inline label input element
Browse files Browse the repository at this point in the history
  • Loading branch information
Zdeněk Balák committed Apr 29, 2019
1 parent 3080721 commit 48902e4
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MainActivity : Activity() {

private lateinit var form: Form

private val charPool : List<Char> = ('a'..'z') + ('A'..'Z')
private val charPool: List<Char> = ('a'..'z') + ('A'..'Z')
private val stringValues = arrayOf("one", "two", "three", "four", "five").map { "Option $it" }.toList()
private val longerStringValues = (1..5).map { randomString(25) }

Expand Down Expand Up @@ -92,12 +92,18 @@ class MainActivity : Activity() {

//input validable elements
addElement(HeaderElement("Input validable elements"), true)
addElement(EditTextElement("Hint", "Edit text element text", object:ValueCallback<String>{
addElement(EditTextElement("Hint", "Edit text element text", object : ValueCallback<String> {
override fun callback(value: String) {
Toast.makeText(applicationContext, value, Toast.LENGTH_SHORT).show()
}

}, arrayListOf(maxLengthValidator, notEmptyValidator)))
addDivider()
addElement(LabelInputElement("Email", "[email protected]","", object : ValueCallback<String> {
override fun callback(value: String) {
Toast.makeText(applicationContext, value, Toast.LENGTH_SHORT).show()
}
}, arrayListOf(notEmptyValidator)))
addSpace()

//action elements
Expand All @@ -108,7 +114,7 @@ class MainActivity : Activity() {
addElement(ActionElement(validateActionCallback, "Validate all elements"))
addSpace()

return@with buildForm(this@MainActivity, formWrapper, FormStyleBundle.colorBundleOne())
return@with buildForm(this@MainActivity, formWrapper, FormStyleBundle.colorBundleThree())
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cz.qase.android.formbuilderlibrary.element

import android.content.Context
import android.support.design.widget.TextInputEditText
import android.support.design.widget.TextInputLayout
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import cz.qase.android.formbuilderlibrary.FormStyleBundle
import cz.qase.android.formbuilderlibrary.R
import cz.qase.android.formbuilderlibrary.common.setBackgroundColorResourceId
import cz.qase.android.formbuilderlibrary.common.setTextColorResourceId
import cz.qase.android.formbuilderlibrary.element.generic.FormElementValidatable
import cz.qase.android.formbuilderlibrary.element.generic.ValueCallback
import cz.qase.android.formbuilderlibrary.validator.FormValidator


class LabelInputElement(private val label: String,
private var hint: String,
private var value: String,
private val valueChangeListener: ValueCallback<String>,
formValidators: MutableList<FormValidator<String>> = ArrayList(),
private val groupComponent: Int = R.layout.form_group_item_inline,
private val headerComponent: Int = R.layout.form_inline_label,
private val inputComponent: Int = R.layout.form_text_input_layout,
private val formStyleBundle: FormStyleBundle? = null) : FormElementValidatable<String>(formValidators) {
override fun getVal(): String? {
return value
}

private var labelView: TextView? = null
private var textInputEditText: TextInputEditText? = null
private var textInputLayout: TextInputLayout? = null

override fun createView(context: Context, formStyleBundle: FormStyleBundle): View {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(groupComponent, null) as ViewGroup
val headerView = prepareLabel(inflater, context, this.formStyleBundle
?: formStyleBundle, view)
val inputView = prepareText(inflater, context, this.formStyleBundle ?: formStyleBundle, view)
view.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor)
view.addView(headerView)
view.addView(inputView)
return view
}

private fun prepareText(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextInputLayout {
val textInputLayout = inflater.inflate(inputComponent, root, false) as TextInputLayout
textInputLayout.hint = hint
textInputEditText = TextInputEditText(context)
textInputLayout.addView(textInputEditText)
textInputEditText?.setTextColorResourceId(context, formStyleBundle.secondaryTextColor)
textInputEditText?.setText(value)
textInputEditText?.addTextChangedListener(
object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
value = s.toString()
validate()
if (!invalid) {
valueChangeListener.callback(s.toString())
}
}

override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}

override fun afterTextChanged(s: Editable) {
}
}
)
this.textInputLayout = textInputLayout
return textInputLayout
}

fun updateLabel(label: String) {
labelView?.text = label
}

fun updateText(text: String) {
textInputEditText?.setText(text)
}

override fun validate() {
super.validate()
if (invalid) {
textInputLayout?.error = invalidMessage
} else {
textInputLayout?.error = null
}
}

private fun prepareLabel(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextView {
val headerView = inflater.inflate(headerComponent, root, false) as TextView
headerView.setTextColorResourceId(context, formStyleBundle.primaryTextColor)
headerView.text = label
labelView = headerView
return headerView
}
}
15 changes: 15 additions & 0 deletions formbuilderlibrary/src/main/res/layout/form_inline_input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<EditText 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:id="@+id/inputText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:minWidth="150dp"
android:gravity="end"
android:textAlignment="gravity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Value" />
10 changes: 10 additions & 0 deletions formbuilderlibrary/src/main/res/layout/form_text_input_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<android.support.design.widget.TextInputLayout android:layout_width="wrap_content"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:minWidth="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">

</android.support.design.widget.TextInputLayout>

0 comments on commit 48902e4

Please sign in to comment.