@@ -10,35 +10,37 @@ Create a new XML layout file. We'll name it form_element_custom.xml.
1010 xmlns : tools =" http://schemas.android.com/tools"
1111 android : layout_width =" match_parent"
1212 android : layout_height =" wrap_content"
13- android : background =" @android: color/black "
13+ android : background =" @color/colorFormMasterElementBackground "
1414 android : orientation =" vertical"
1515 android : paddingBottom =" 16dp" >
1616
1717 <View
18+ android : id =" @+id/formElementDivider"
1819 android : layout_width =" match_parent"
1920 android : layout_height =" 0.5dp"
2021 android : layout_marginLeft =" 16dp"
2122 android : layout_marginRight =" 16dp"
2223 android : background =" @color/colorFormMasterDivider" />
2324
2425 <LinearLayout
26+ android : id =" @+id/formElementMainLayout"
2527 android : layout_width =" match_parent"
2628 android : layout_height =" wrap_content"
2729 android : layout_marginLeft =" 16dp"
2830 android : layout_marginRight =" 16dp"
2931 android : layout_marginTop =" 16dp"
3032 android : orientation =" horizontal" >
3133
32- <android .support.v7 .widget.AppCompatTextView
34+ <com .thejuki.kformmaster .widget.IconTextView
3335 android : id =" @+id/formElementTitle"
3436 android : layout_width =" 0dp"
3537 android : layout_height =" wrap_content"
3638 android : layout_weight =" 2"
37- android : textColor =" @android: color/white "
39+ android : textColor =" @color/colorFormMasterElementTextTitle "
3840 android : textSize =" @dimen/elementTextTitleSize"
39- tools : text =" Test Title" />
41+ tools : text =" Custom Title" />
4042
41- <android .support.v7 .widget.AppCompatEditText
43+ <com .thejuki.kformmaster .widget.ClearableEditText
4244 android : id =" @+id/formElementValue"
4345 android : layout_width =" 0dp"
4446 android : layout_height =" wrap_content"
@@ -48,13 +50,14 @@ Create a new XML layout file. We'll name it form_element_custom.xml.
4850 android : imeOptions =" actionNext"
4951 android : inputType =" textNoSuggestions"
5052 android : maxLines =" 1"
51- android : textColor =" @color/colorFormMasterElementFocusedTitle"
52- android : textSize =" 20sp"
53- tools : text =" Test Value" />
53+ android : textColor =" @drawable/edit_text_selector"
54+ android : textColorHint =" @color/colorFormMasterElementHint"
55+ android : textSize =" @dimen/elementTextValueSize"
56+ tools : text =" Custom Value" />
5457
5558 </LinearLayout >
5659
57- <android .support.v7 .widget.AppCompatTextView
60+ <androidx .appcompat .widget.AppCompatTextView
5861 android : id =" @+id/formElementError"
5962 android : layout_width =" match_parent"
6063 android : layout_height =" 0dp"
@@ -65,7 +68,7 @@ Create a new XML layout file. We'll name it form_element_custom.xml.
6568 android : textColor =" @color/colorFormMasterElementErrorTitle"
6669 android : textSize =" @dimen/elementErrorTitleSize"
6770 android : visibility =" gone"
68- tools : text =" Test Error " />
71+ tools : text =" Personal Info " />
6972
7073</LinearLayout >
7174```
@@ -75,39 +78,17 @@ Create a new XML layout file. We'll name it form_element_custom.xml.
7578Note that a new model does not need to contain a body if BaseFormElement provides everything you need.
7679
7780``` kotlin
78- class FormCustomElement : BaseFormElement <String > {
79- constructor () : super ()
80- constructor (tag: Int ) : super (tag)
81- }
81+ class FormCustomElement (tag : Int = -1 ) : BaseFormElement<String>(tag)
8282```
8383
8484### Optional: Form Builder Extension
8585
86- Create a FormBuildHelper DSL method and builder class for your custom form model.
86+ Create a FormBuildHelper DSL method for your custom form model.
8787
8888``` kotlin
89- /* * Builder method to add a CustomElement */
90- class CustomElementBuilder (tag : Int = -1 ) : BaseElementBuilder<String>(tag) {
91- override fun build () =
92- FormCustomElement (tag).apply {
93- this @CustomElementBuilder.let {
94- title = it.title.orEmpty()
95- value = it.value
96- hint = it.hint
97- rightToLeft = it.rightToLeft
98- maxLines = it.maxLines
99- error = it.error
100- required = it.required
101- enabled = it.enabled
102- visible = it.visible
103- valueObservers.addAll(it.valueObservers)
104- }
105- }
106- }
107-
10889/* * FormBuildHelper extension to add a CustomElement */
109- fun FormBuildHelper.customEx (tag : Int = -1, init : CustomElementBuilder .() -> Unit ): FormCustomElement {
110- return addFormElement(CustomElementBuilder (tag).apply (init ))
90+ fun FormBuildHelper.customEx (tag : Int = -1, init : FormCustomElement .() -> Unit ): FormCustomElement {
91+ return addFormElement(FormCustomElement (tag).apply (init ))
11192}
11293```
11394
@@ -146,59 +127,38 @@ Create a view binder for your custom form element.
146127 * viewStateProvider parameter - Form element view state provider
147128
148129``` kotlin
149- class CustomViewBinder (private val context : Context ,
150- private val formBuilder : FormBuildHelper ) : BaseFormViewBinder() {
151- var viewBinder = ViewBinder (R .layout.form_element_custom,
152- FormCustomElement ::class .java, { model, finder, _ ->
130+ class CustomViewBinder (private val context : Context , private val formBuilder : FormBuildHelper ,
131+ @LayoutRes private val layoutID : Int? ) : BaseFormViewBinder() {
132+ var viewBinder = ViewBinder (layoutID
133+ ? : R .layout.form_element_custom, FormCustomElement ::class .java, { model, finder, _ ->
153134 val textViewTitle = finder.find(R .id.formElementTitle) as AppCompatTextView
154135 val mainViewLayout = finder.find(R .id.formElementMainLayout) as ? LinearLayout
155136 val textViewError = finder.find(R .id.formElementError) as AppCompatTextView
137+ val dividerView = finder.find(R .id.formElementDivider) as ? View
156138 val itemView = finder.getRootView() as View
157- val editTextValue = finder.find(R .id.formElementValue) as AppCompatEditText
158- baseSetup(model, textViewTitle, textViewError, itemView, mainViewLayout, editTextValue)
139+ val editTextValue = finder.find(R .id.formElementValue) as com.thejuki.kformmaster.widget. ClearableEditText
140+ baseSetup(model, dividerView, textViewTitle, textViewError, itemView, mainViewLayout, editTextValue)
159141
160142 editTextValue.setText(model.valueAsString)
161143 editTextValue.hint = model.hint ? : " "
162144
163- setEditTextFocusEnabled(editTextValue, itemView)
164-
165- editTextValue.setOnFocusChangeListener { _, hasFocus ->
166- if (hasFocus) {
167- textViewTitle.setTextColor(ContextCompat .getColor(context,
168- R .color.colorFormMasterElementFocusedTitle))
169- } else {
170- textViewTitle.setTextColor(ContextCompat .getColor(context,
171- R .color.colorFormMasterElementTextTitle))
172- }
173- }
174-
175145 // Initially use 4 lines
176146 // unless a different number was provided
177147 if (model.maxLines == 1 ) {
178148 model.maxLines = 4
179149 }
180150
181- editTextValue.addTextChangedListener( object : TextWatcher {
182- override fun beforeTextChanged ( charSequence : CharSequence , i : Int , i2 : Int , i3 : Int ) { }
151+ // If an InputType is provided, use it instead
152+ model.inputType?. let { editTextValue.setRawInputType(it) }
183153
184- override fun onTextChanged (charSequence : CharSequence , i : Int , i2 : Int , i3 : Int ) {
154+ // If imeOptions are provided, use them instead of creating a new line
155+ model.imeOptions?.let { editTextValue.imeOptions = it }
185156
186- // get current form element, existing value and new value
187- val currentValue = model.valueAsString
188- val newValue = charSequence.toString()
189-
190- // trigger event only if the value is changed
191- if (currentValue != newValue) {
192- // NOTE: Use setValue()
193- // as this will suppress the unchecked cast
194- model.setValue(newValue)
195- model.error = null
196- formBuilder.onValueChanged(model)
197- }
198- }
157+ setEditTextFocusEnabled(editTextValue, itemView)
158+ setOnFocusChangeListener(context, model, formBuilder)
159+ addTextChangedListener(model, formBuilder)
160+ setOnEditorActionListener(model, formBuilder)
199161
200- override fun afterTextChanged (editable : Editable ) {}
201- })
202162 }, object : ViewStateProvider <FormCustomElement , ViewHolder > {
203163 override fun createViewStateID (model : FormCustomElement ): Int {
204164 return model.id
@@ -213,7 +173,7 @@ private val formBuilder: FormBuildHelper) : BaseFormViewBinder() {
213173 itemView.setOnClickListener {
214174 editTextValue.requestFocus()
215175 val imm = context.getSystemService(Context .INPUT_METHOD_SERVICE ) as InputMethodManager
216- editTextValue.setSelection(editTextValue.text.length)
176+ editTextValue.setSelection(editTextValue.text? .length ? : 0 )
217177 imm.showSoftInput(editTextValue, InputMethodManager .SHOW_IMPLICIT )
218178 }
219179 }
0 commit comments