Skip to content

Commit

Permalink
FEAT: Text area element
Browse files Browse the repository at this point in the history
  • Loading branch information
Zdeněk Balák committed May 3, 2019
1 parent 4009c95 commit 3a44b00
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class MainActivity : AppCompatActivity() {

private val validateActionCallback = object : ActionCallback, Form.Callback {
override fun successCallback() {
Toast.makeText(this@MainActivity, "Validate OK", Toast.LENGTH_LONG).show()
Toast.makeText(this@MainActivity, "Validate OK", Toast.LENGTH_SHORT).show()
}

override fun errorCallback(message: String?) {
Toast.makeText(this@MainActivity, "Validate not ok $message", Toast.LENGTH_LONG).show()
Toast.makeText(this@MainActivity, "Validate not ok $message", Toast.LENGTH_SHORT).show()
}

override fun callback() {
Expand All @@ -47,24 +47,33 @@ class MainActivity : AppCompatActivity() {

private val showToastActionCallback = object : ActionCallback {
override fun callback() {
Toast.makeText(this@MainActivity, "Action performed", Toast.LENGTH_LONG).show()
Toast.makeText(this@MainActivity, "Action performed", Toast.LENGTH_SHORT).show()
}
}

private val showToastCheckboxCallback = object : CheckboxCallback {
private var toast: Toast? = null
override fun callback(checked: Boolean) {
Toast.makeText(this@MainActivity, "Selected: $checked", Toast.LENGTH_LONG).show()
toast?.cancel()
toast = Toast.makeText(this@MainActivity, "Selected: $checked", Toast.LENGTH_SHORT)
toast?.show()
}
}

private val showToastStringValueCallback = object : ValueCallback<String> {
private var toast: Toast? = null
override fun callback(value: String) {
Toast.makeText(this@MainActivity, value, Toast.LENGTH_LONG).show()
toast?.cancel()
toast = Toast.makeText(this@MainActivity, value, Toast.LENGTH_SHORT)
toast?.show()
}
}
private val showToastDateTimeValueCallback = object : ValueCallback<DateTime> {
private var toast: Toast? = null
override fun callback(value: DateTime) {
Toast.makeText(this@MainActivity, SimpleDateFormat("dd.MM.yyyy - HH:mm", Locale.getDefault()).format(value.toDate()), Toast.LENGTH_LONG).show()
toast?.cancel()
toast = Toast.makeText(this@MainActivity, SimpleDateFormat("dd.MM.yyyy - HH:mm", Locale.getDefault()).format(value.toDate()), Toast.LENGTH_SHORT)
toast?.show()
}
}

Expand Down Expand Up @@ -99,6 +108,8 @@ class MainActivity : AppCompatActivity() {
addElement(LabelCheckboxElement("LabelCheckboxElement label", true, showToastCheckboxCallback), true)
addElement(LabelDateTimeElement("Date picker", "Vyberte datum...", supportFragmentManager, showToastDateTimeValueCallback, formValidators = arrayListOf(NotInPastValidator("Date cannot be in past"))))
addSpace()
addElement(TextAreaElement("TextArea", null, null, showToastStringValueCallback, maxLength = 320))
addSpace()


//input validable elements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ open class EditTextElement(
textInputLayout?.isErrorEnabled = true
textInputLayout?.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor)
textInputLayout?.setPadding(10, 0, 10, 0)

editText = TextInputEditText(context)
editText?.setTextColorResourceId(context, formStyleBundle.secondaryTextColor)
textInputLayout?.addView(editText)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
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.ValidationException
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
import android.text.InputFilter

class TextAreaElement(
private val title: String,
private val hint: String?,
private var value: String? = null,
private val valueChangeListener: ValueCallback<String>?,
private val numberOfLines: Int = 3,
private val maxLength: Int? = null,
formValidators: MutableList<FormValidator<String>> = ArrayList(),
private val groupComponent: Int = R.layout.form_group_item,
private val headerComponent: Int = R.layout.form_header_item,
private val textAreaComponent: Int = R.layout.form_textarea_item,
private val footerComponent: Int = R.layout.form_footer_text_right,
private var formStyleBundle: FormStyleBundle? = null) : FormElementValidatable<String>(formValidators) {

override fun getVal(): String? {
return textInputEditText?.text.toString()
}

private var headerView: TextView? = null
private var footerView: 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 = prepareHeader(inflater, context, this.formStyleBundle
?: formStyleBundle, view)
val inputView = prepareText(inflater, context, this.formStyleBundle
?: formStyleBundle, view)
val footerView = prepareFooter(inflater, context, this.formStyleBundle
?: formStyleBundle, view)
view.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor)
view.addView(headerView)
view.addView(inputView)
view.addView(footerView)
return view
}

private fun prepareText(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextInputLayout {
val textInputLayout = inflater.inflate(textAreaComponent, root, false) as TextInputLayout
textInputLayout.hint = hint
textInputEditText = TextInputEditText(context)
textInputLayout.addView(textInputEditText)
textInputEditText?.minLines = numberOfLines

if (maxLength != null) {
textInputEditText?.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(maxLength))
}

textInputEditText?.setTextColorResourceId(context, formStyleBundle.secondaryTextColor)
textInputEditText?.setText(value)
textInputEditText?.addTextChangedListener(
object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
val length = getVal()?.length?:0
if (maxLength != null) {
footerView?.text = "$length/$maxLength"
} else {
footerView?.text = "$length"
}
positiveValidation()
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
}

private fun prepareFooter(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextView {
val footerView = inflater.inflate(footerComponent, root, false) as TextView
if (maxLength != null) {
footerView.text = "0/$maxLength"
} else {
footerView.text = "0"
}
footerView.setTextColorResourceId(context, formStyleBundle.secondaryTextColor)
this.footerView = footerView
return footerView

}

fun updateTitle(label: String) {
headerView?.text = label
}

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

private fun positiveValidation() {
try {
super.validate()
textInputLayout?.error = null
} catch (e: ValidationException) {
}
}

override fun validate() {
try {
super.validate()
textInputLayout?.error = null
} catch (e: ValidationException) {
textInputLayout?.error = e.message
throw e
}
}

private fun prepareHeader(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextView {
val headerView = inflater.inflate(headerComponent, root, false) as TextView
headerView.setTextColorResourceId(context, formStyleBundle.primaryTextColor)
headerView.setBackgroundColorResourceId(context, formStyleBundle.primaryBackgroundColor)
headerView.text = title
this.headerView = headerView
return headerView
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="textEnd"
android:gravity="end"
tools:text="Field text" />
5 changes: 5 additions & 0 deletions formbuilderlibrary/src/main/res/layout/form_textarea_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

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

0 comments on commit 3a44b00

Please sign in to comment.