diff --git a/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt b/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt index 5c4826b..3089087 100644 --- a/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt +++ b/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt @@ -19,7 +19,7 @@ class MainActivity : Activity() { private lateinit var form: Form - private val charPool : List = ('a'..'z') + ('A'..'Z') + private val charPool: List = ('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) } @@ -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{ + addElement(EditTextElement("Hint", "Edit text element text", object : ValueCallback { override fun callback(value: String) { Toast.makeText(applicationContext, value, Toast.LENGTH_SHORT).show() } }, arrayListOf(maxLengthValidator, notEmptyValidator))) + addDivider() + addElement(LabelInputElement("Email", "test@test.cz","", object : ValueCallback { + override fun callback(value: String) { + Toast.makeText(applicationContext, value, Toast.LENGTH_SHORT).show() + } + }, arrayListOf(notEmptyValidator))) addSpace() //action elements @@ -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()) } diff --git a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelInputElement.kt b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelInputElement.kt new file mode 100644 index 0000000..8e049d3 --- /dev/null +++ b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/element/LabelInputElement.kt @@ -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, + formValidators: MutableList> = 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(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 + } +} \ No newline at end of file diff --git a/formbuilderlibrary/src/main/res/layout/form_inline_input.xml b/formbuilderlibrary/src/main/res/layout/form_inline_input.xml new file mode 100644 index 0000000..0c6346c --- /dev/null +++ b/formbuilderlibrary/src/main/res/layout/form_inline_input.xml @@ -0,0 +1,15 @@ + + diff --git a/formbuilderlibrary/src/main/res/layout/form_text_input_layout.xml b/formbuilderlibrary/src/main/res/layout/form_text_input_layout.xml new file mode 100644 index 0000000..d46e8b1 --- /dev/null +++ b/formbuilderlibrary/src/main/res/layout/form_text_input_layout.xml @@ -0,0 +1,10 @@ + + + \ No newline at end of file