Skip to content

Commit 625bb16

Browse files
authored
Add click listeners to multicolumnrow texts (#41)
* Add click listeners to multicolumnrow texts * Improve kotlin-ness of parameter ordering * Remove setTextIsSelectable from MultiColumnRowView
1 parent 346281b commit 625bb16

File tree

5 files changed

+64
-28
lines changed

5 files changed

+64
-28
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ Allowed headings:
1616

1717
## [Unreleased]
1818

19+
### Added
20+
21+
* Added new methods to set click listeners and custom click content descriptions to each text view inside `MultiColumnRowView`:
22+
`setText1ClickAction(clickDescription: CharSequence? = null, listener: OnClickListener)`,
23+
`setText2ClickAction(clickDescription: CharSequence? = null, listener: OnClickListener)`,
24+
`setText3ClickAction(clickDescription: CharSequence? = null, listener: OnClickListener)`
25+
26+
### Changed
27+
28+
* Fixed typo in method name inside `MultiColumnRowView`:
29+
`setTextContentDescription(desc1: CharSequence?, desc2: CharSequence?, desc3: CharSequence?)`
30+
31+
### Removed
32+
33+
* Removed `setTextIsSelectable` method and xml attributes `text1IsSelectable`, `text2IsSelectable` and `text3IsSelectable` from `MultiColumnRowView`
34+
1935
## [3.13.0] - 2021-05-10
2036

2137
### Added

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,6 @@ the xml text attribute
260260
app:text1ContentDescription="@string/text1_desc"
261261
app:text2ContentDescription="@string/text2_desc"
262262
app:text3ContentDescription="@string/text3_desc"
263-
app:text1IsSelectable="false"
264-
app:text2IsSelectable="true"
265-
app:text3IsSelectable="true"
266263
app:textStyle="@style/Text.Body"
267264
app:textStyle2="@style/Text.Body"
268265
app:textStyle3="@style/Text.Bold"
@@ -278,6 +275,17 @@ multi_column_row.setTextStyle(R.style.Text_Bold)
278275
multi_column_row.setText1AsHeading(true)
279276
```
280277

278+
If you need to, you can add a click listener to each column and provide a custom click content description:
279+
```kotlin
280+
multi_column_row.setText1ClickAction("custom click content description") {
281+
// click listener
282+
}
283+
multi_column_row.setText2ClickAction { Toast.makeText(context, "text 2 clicked", LENGTH_LONG).show() }
284+
multi_column_row.setText3ClickAction("do custom action") {
285+
Toast.makeText(context, "text 3 custom action invoked", LENGTH_LONG).show()
286+
}
287+
```
288+
281289
### Text Input View
282290

283291
```xml

components/src/main/java/uk/gov/hmrc/components/molecule/item/MultiColumnRowView.kt

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import android.widget.LinearLayout
2323
import androidx.annotation.StyleRes
2424
import uk.gov.hmrc.components.R
2525
import uk.gov.hmrc.components.databinding.ComponentMultiColumnRowBinding
26+
import uk.gov.hmrc.components.extensions.setAccessibilityMessage
2627
import uk.gov.hmrc.components.extensions.setAsAccessibilityHeading
2728
import uk.gov.hmrc.components.extensions.setStyle
2829

@@ -50,17 +51,13 @@ class MultiColumnRowView @JvmOverloads constructor(
5051
val text1ContentDescription = typedArray.getString(R.styleable.MultiColumnRowView_text1ContentDescription)
5152
val text2ContentDescription = typedArray.getString(R.styleable.MultiColumnRowView_text2ContentDescription)
5253
val text3ContentDescription = typedArray.getString(R.styleable.MultiColumnRowView_text3ContentDescription)
53-
val text1IsSelectable = typedArray.getBoolean(R.styleable.MultiColumnRowView_text1IsSelectable, false)
54-
val text2IsSelectable = typedArray.getBoolean(R.styleable.MultiColumnRowView_text2IsSelectable, false)
55-
val text3IsSelectable = typedArray.getBoolean(R.styleable.MultiColumnRowView_text3IsSelectable, false)
5654
val textStyle = typedArray.getResourceId(R.styleable.MultiColumnRowView_textStyle, R.style.Text_Body)
5755
val textStyle2 = typedArray.getResourceId(R.styleable.MultiColumnRowView_textStyle2, textStyle)
5856
val textStyle3 = typedArray.getResourceId(R.styleable.MultiColumnRowView_textStyle3, textStyle)
5957
val text1Heading = typedArray.getBoolean(R.styleable.MultiColumnRowView_text1Heading, false)
6058

6159
setText(text1, text2, text3)
62-
setTextContentDesription(text1ContentDescription, text2ContentDescription, text3ContentDescription)
63-
setTextIsSelectable(text1IsSelectable, text2IsSelectable, text3IsSelectable)
60+
setTextContentDescription(text1ContentDescription, text2ContentDescription, text3ContentDescription)
6461
setTextStyle(textStyle, textStyle2, textStyle3)
6562
setText1AsHeading(text1Heading)
6663

@@ -143,33 +140,42 @@ class MultiColumnRowView @JvmOverloads constructor(
143140
setText(binding.rowText1.text, binding.rowText2.text, text3)
144141
}
145142

146-
fun setTextContentDesription(desc1: CharSequence?, desc2: CharSequence?, desc3: CharSequence?) {
143+
fun setTextContentDescription(desc1: CharSequence?, desc2: CharSequence?, desc3: CharSequence?) {
147144
desc1?.let { binding.rowText1.contentDescription = it }
148145
desc2?.let { binding.rowText2.contentDescription = it }
149146
desc3?.let { binding.rowText3.contentDescription = it }
150147
}
151148

152149
fun setText1ContentDescription(desc1: CharSequence?) {
153-
setTextContentDesription(desc1, binding.rowText2.contentDescription, binding.rowText3.contentDescription)
150+
setTextContentDescription(desc1, binding.rowText2.contentDescription, binding.rowText3.contentDescription)
154151
}
155152

156153
fun setText2ContentDescription(desc2: CharSequence?) {
157-
setTextContentDesription(binding.rowText1.contentDescription, desc2, binding.rowText3.contentDescription)
154+
setTextContentDescription(binding.rowText1.contentDescription, desc2, binding.rowText3.contentDescription)
158155
}
159156

160157
fun setText3ContentDescription(desc3: CharSequence?) {
161-
setTextContentDesription(binding.rowText1.contentDescription, binding.rowText2.contentDescription, desc3)
158+
setTextContentDescription(binding.rowText1.contentDescription, binding.rowText2.contentDescription, desc3)
162159
}
163160

164-
fun setTextIsSelectable(
165-
text1IsSelectable: Boolean = binding.rowText1.isTextSelectable,
166-
text2IsSelectable: Boolean = binding.rowText2.isTextSelectable,
167-
text3IsSelectable: Boolean = binding.rowText3.isTextSelectable
168-
) {
169-
binding.apply {
170-
rowText1.setTextIsSelectable(text1IsSelectable)
171-
rowText2.setTextIsSelectable(text2IsSelectable)
172-
rowText3.setTextIsSelectable(text3IsSelectable)
161+
fun setText1ClickAction(clickDescription: CharSequence? = null, listener: OnClickListener) {
162+
binding.rowText1.apply {
163+
setOnClickListener(listener)
164+
clickDescription?.let { setAccessibilityMessage(it) }
165+
}
166+
}
167+
168+
fun setText2ClickAction(clickDescription: CharSequence? = null, listener: OnClickListener) {
169+
binding.rowText2.apply {
170+
setOnClickListener(listener)
171+
clickDescription?.let { setAccessibilityMessage(it) }
172+
}
173+
}
174+
175+
fun setText3ClickAction(clickDescription: CharSequence? = null, listener: OnClickListener) {
176+
binding.rowText3.apply {
177+
setOnClickListener(listener)
178+
clickDescription?.let { setAccessibilityMessage(it) }
173179
}
174180
}
175181

components/src/main/res/values/attrs.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,6 @@
106106
<attr name="text2ContentDescription" format="string" />
107107
<!-- Third piece of text content description. -->
108108
<attr name="text3ContentDescription" format="string" />
109-
<!-- Whether or not the first piece of text is selectable. -->
110-
<attr name="text1IsSelectable" format="boolean" />
111-
<!-- Whether or not the second piece of text is selectable. -->
112-
<attr name="text2IsSelectable" format="boolean" />
113-
<!-- Whether or not the third piece of text is selectable. -->
114-
<attr name="text3IsSelectable" format="boolean" />
115109
<!-- Text style for the first piece of text. -->
116110
<attr name="textStyle" format="reference" />
117111
<!-- Text style for the second piece of text. -->

sample/src/main/java/uk/gov/hmrc/components/sample/molecules/MultiColumnRowFragment.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import android.os.Bundle
1919
import android.view.LayoutInflater
2020
import android.view.View
2121
import android.view.ViewGroup
22+
import android.widget.Toast
23+
import android.widget.Toast.LENGTH_LONG
2224
import uk.gov.hmrc.components.sample.R
2325
import uk.gov.hmrc.components.sample.autoCleared
2426
import uk.gov.hmrc.components.sample.base.BaseComponentsFragment
@@ -31,8 +33,18 @@ class MultiColumnRowFragment : BaseComponentsFragment() {
3133

3234
override fun provideToolbar() = ToolbarState(true, R.string.molecules_multi_column_row, true)
3335

34-
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
36+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
3537
binding = FragmentMultiColumnRowBinding.inflate(inflater, container, false)
3638
return binding.root
3739
}
40+
41+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
42+
super.onViewCreated(view, savedInstanceState)
43+
binding.multiColumnRowPlaceholder.apply {
44+
setText2ClickAction { Toast.makeText(context, "text 2 clicked", LENGTH_LONG).show() }
45+
setText3ClickAction("do custom action") {
46+
Toast.makeText(context, "text 3 custom action invoked", LENGTH_LONG).show()
47+
}
48+
}
49+
}
3850
}

0 commit comments

Comments
 (0)