Skip to content

Commit ce0adc0

Browse files
committed
Renamed module, added selected by default
1 parent 7cd075b commit ce0adc0

36 files changed

+46
-40
lines changed

.idea/modules.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 11 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<manifest package="uk.co.onemandan.checkboxtextviews"/>

app/src/main/java/uk/co/onemandan/checkboxtextviews/CheckBoxTextViews.kt renamed to checkboxtextviews/src/main/java/uk/co/onemandan/checkboxtextviews/CheckBoxTextViews.kt

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,21 @@ class CheckBoxTextViews(context: Context, attrs: AttributeSet): LinearLayout(con
2828
private var _selectedBackgroundColour = 0
2929
private var _selectedTextColour = 0
3030

31-
private val _selectedItems: MutableList<String> = ArrayList()
31+
private var _defaultSelected = false
32+
33+
private var _selectedItems: MutableList<String>? = null
3234
private var _selectedItemListener: CheckBoxTextViewsListener? = null
3335

3436
override fun onClick(v: View?) {
35-
//Update whether or not the item has been selected via the tag
36-
if(v!!.tag == null){
37-
v.tag = true
38-
} else {
39-
v.tag = !(v.tag as Boolean)
40-
}
41-
42-
val title = v.itemTextView
43-
val root = v.rootClipView
44-
45-
if(v.tag as Boolean){
46-
title.setTextColor(_selectedTextColour)
47-
setBackgroundColour(root, _selectedBackgroundColour)
48-
49-
_selectedItems.add(title.text.toString())
50-
} else {
51-
title.setTextColor(_defaultTextColour)
52-
setBackgroundColour(root, _defaultBackgroundColour)
53-
54-
_selectedItems.remove(title.text.toString())
55-
}
56-
57-
_selectedItemListener?.onItemSelected(_selectedItems)
37+
selectView(v)
5838
}
5939

6040
private fun init(context: Context, attrs: AttributeSet){
6141
val root = inflate(context, R.layout.checkbox_item_host, this)
6242
_flexbox = root.rootFlexboxLayout
6343

44+
_selectedItems = ArrayList()
45+
6446
handleDefaultAttributes(context)
6547
handleAttributes(context, attrs)
6648
}
@@ -89,6 +71,8 @@ class CheckBoxTextViews(context: Context, attrs: AttributeSet): LinearLayout(con
8971
R.styleable.CheckBoxTextViews_cbtv_selected_background_color, _selectedBackgroundColour)
9072
_selectedTextColour = ta.getColor(
9173
R.styleable.CheckBoxTextViews_cbtv_selected_text_color, _selectedTextColour)
74+
_defaultSelected = ta.getBoolean(
75+
R.styleable.CheckBoxTextViews_cbtv_selected, false)
9276

9377
val itemsId = ta.getResourceId(R.styleable.CheckBoxTextViews_cbtv_items, 0)
9478
if(itemsId != 0){
@@ -117,15 +101,45 @@ class CheckBoxTextViews(context: Context, attrs: AttributeSet): LinearLayout(con
117101
checkBoxItem.setOnClickListener(this)
118102

119103
_flexbox!!.addView(checkBoxItem)
104+
105+
if(_defaultSelected){
106+
selectView(checkBoxItem)
107+
}
120108
}
121109

122110
private fun setBackgroundColour(view: View, colour: Int){
123111
view.background.colorFilter = PorterDuffColorFilter(colour,
124112
PorterDuff.Mode.SRC_IN)
125113
}
126114

115+
private fun selectView(v: View?){
116+
//Update whether or not the item has been selected via the tag
117+
if(v!!.tag == null){
118+
v.tag = true
119+
} else {
120+
v.tag = !(v.tag as Boolean)
121+
}
122+
123+
val title = v.itemTextView
124+
val root = v.rootClipView
125+
126+
if(v.tag as Boolean){
127+
title.setTextColor(_selectedTextColour)
128+
setBackgroundColour(root, _selectedBackgroundColour)
129+
130+
_selectedItems!!.add(title.text.toString())
131+
} else {
132+
title.setTextColor(_defaultTextColour)
133+
setBackgroundColour(root, _defaultBackgroundColour)
134+
135+
_selectedItems!!.remove(title.text.toString())
136+
}
137+
138+
_selectedItemListener?.onItemSelected(_selectedItems!!)
139+
}
140+
127141
fun getSelectedItems(): List<String> {
128-
return _selectedItems
142+
return _selectedItems!!
129143
}
130144

131145
fun setSelectedItemListener(listener: CheckBoxTextViewsListener){

app/src/main/res/values/attr.xml renamed to checkboxtextviews/src/main/res/values/attr.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<attr name="cbtv_selected_background_color" format="color|reference" />
88
<attr name="cbtv_selected_text_color" format="color|reference" />
99
<attr name="cbtv_items" format="reference" />
10+
<attr name="cbtv_selected" format="boolean" />
1011
</declare-styleable>
1112

1213
</resources>

sample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ android {
2525
}
2626

2727
dependencies {
28-
implementation project(':app')
28+
implementation project(':checkboxtextviews')
2929
implementation fileTree(include: ['*.jar'], dir: 'libs')
3030
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
3131
implementation 'com.android.support:appcompat-v7:27.1.1'

sample/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
android:id="@+id/itemsCheckBoxTextViews"
1212
android:layout_width="match_parent"
1313
android:layout_height="wrap_content"
14-
app:cbtv_items="@array/TestArray" />
14+
app:cbtv_items="@array/TestArray"
15+
app:cbtv_selected="true"/>
1516

1617
<TextView
1718
android:id="@+id/selectedItemTextView"

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':app', ':sample'
1+
include ':checkboxtextviews', ':sample'

0 commit comments

Comments
 (0)