diff --git a/README.md b/README.md index 7cf2be6..ebbe0b7 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,44 @@ # Smart Filter Library -This is a library for creating and managing .... +This library provides a set of customizable filter components for Android applications. It supports both single and multi-selection filters in various formats such as radio groups, chip groups, and list views. The filters can be displayed either vertically or horizontally. ## Features - - Single Line Radio Group(Verticle/Horizontal) - - Multi Line Radio Group - - Row Item Radio Group(Verticle/Horizontal) - - Range Selection - - Chip Group Single Selection(Verticle/Horizontal) - - Chip Group Multi Selection(Verticle/Horizontal) - - Checked Single Selection(Verticle/Horizontal) - - Checked multi Selection(Verticle/Horizontal) - +- Single Line Radio Group (Vertical/Horizontal) +- Multi Line Radio Group +- Row Item Radio Group (Vertical/Horizontal) +- Range Selection +- Chip Group Single Selection (Vertical/Horizontal) +- Chip Group Multi Selection (Vertical/Horizontal) +- Checked Single Selection (Vertical/Horizontal) +- Checked Multi Selection (Vertical/Horizontal) + ## Getting Started - These instructions will get you a copy of the project up and running on your local machine +These instructions will guide you on how to get a copy of the project up and running on your local machine. ## Prerequisites - +Before you can run the project, you need to have Android Studio installed on your machine. You also need to have a suitable Android device or emulator for running the application. ## Installing -- **Clone the repository: `git clone https://github.com/smartSenseSolutions/smart-filter-library.git`** -- **Open the project in Android Studio.** -- **Build and run the project on an emulator or device.** +1. Clone the repository: `git clone https://github.com/smartSenseSolutions/smart-filter-library.git` +2. Open the project in Android Studio. +3. Build and run the project on an emulator or device. + +## Usage + +The library provides a set of classes and methods for creating and managing filter components. Each filter type has its own class and configuration methods. The filters can be added to any ViewGroup in your layout. + +The library also provides a set of default styles for the filters. You can customize these styles by providing your own drawable and color resources. + +## Dependencies + +The library depends on the following libraries: + +- `androidx.appcompat:appcompat:1.3.1` +- `androidx.core:core-ktx:1.6.0` ## SingleSelection @@ -117,16 +130,16 @@ This is a library for creating and managing .... - **Verticle** - SmartFilter.addListViewSingleSelection { - addSingleSelectionListVertical(binding.root) { radioGroupData -> - toast("id: " + radioGroupData) } } + SmartFilter.addListViewSingleSelection { + addSingleSelectionListVertical(binding.root) { radioGroupData -> + toast("name: " + radioGroupData.name) } } - **Horizontal** SmartFilter.addListViewSingleSelection { addSingleSelectionListHorizontal(binding.root) { radioGroupData -> - toast("id: " + radioGroupData) } } + toast("name: " + radioGroupData.name) } } @@ -135,9 +148,9 @@ This is a library for creating and managing .... - **Verticle** - SmartFilter.addListViewMultiSelection{ - addMultiSelectionList(binding.root) { radioGroupData -> - toast("id: " + radioGroupData) } } + SmartFilter.addListViewMultiSelection{ + addMultiSelectionListVertical(binding.root) { radioGroupData -> + toast("name: " + radioGroupData) } } @@ -145,7 +158,7 @@ This is a library for creating and managing .... SmartFilter.addListViewMultiSelection{ addMultiSelectionListHorizontal(binding.root) { radioGroupData -> - toast("id: " + radioGroupData) } } + toast("name: " + radioGroupData) } } diff --git a/app/src/main/java/com/ss/smartfilter/MainActivity.kt b/app/src/main/java/com/ss/smartfilter/MainActivity.kt index af603e3..562428c 100644 --- a/app/src/main/java/com/ss/smartfilter/MainActivity.kt +++ b/app/src/main/java/com/ss/smartfilter/MainActivity.kt @@ -16,9 +16,9 @@ class MainActivity : ComponentActivity() { setContentView(binding.root) - SmartFilter.addRadioRawItemSingleSelection { - addRadioGroupRowItemVertical(binding.root) { radioGroupData -> - toast("name: ${radioGroupData.name} ") } } + SmartFilter.addRadioGroupSingleSelection { + addRadioGroupSingleLineVertical(binding.root) { radioGroupData -> + toast("name: ${radioGroupData.name} ") }} } diff --git a/app/src/main/java/com/ss/smartfilter/Utils.kt b/app/src/main/java/com/ss/smartfilter/Utils.kt index e41fd10..85b88c2 100644 --- a/app/src/main/java/com/ss/smartfilter/Utils.kt +++ b/app/src/main/java/com/ss/smartfilter/Utils.kt @@ -23,8 +23,8 @@ fun addRadioGroupSingleLineVertical(rootView: ViewGroup,onItemSelected: (Data) - orientation = Orientation.VERTICAL, mData = mData(), onItemSelected = onItemSelected, - bgSelector = R.drawable.singleline_rb_selector, - textSelector = R.color.single_text_color_selector + bgSelector = R.drawable.rb_selector, + textSelector = R.color.text_color_selector ) ) @@ -36,8 +36,8 @@ fun addRadioGroupSingleLineHorizontal(rootView: ViewGroup,onItemSelected: (Data) orientation = Orientation.HORIZONTAL, mData = mData(), onItemSelected = onItemSelected, - bgSelector = R.drawable.singleline_rb_selector, - textSelector = R.color.single_text_color_selector + bgSelector = R.drawable.rb_selector, + textSelector = R.color.text_color_selector ) ) @@ -64,7 +64,7 @@ fun addRadioGroupRowItemHorizontal(rootView: ViewGroup,onItemSelected: (Data) -> mData = mData(), onItemSelected = onItemSelected, bgSelector = R.drawable.row_item_selector, - textSelector = R.color.single_text_color_selector + textSelector = R.color.text_color_selector ) ) @@ -77,7 +77,7 @@ fun addRadioGroupRowItemVertical(rootView: ViewGroup,onItemSelected: (Data) -> U mData = mData(), onItemSelected = onItemSelected, bgSelector = R.drawable.row_item_selector, - textSelector = R.color.single_text_color_selector + textSelector = R.color.text_color_selector ) ) @@ -86,12 +86,12 @@ fun addSingleSelectionChipGroupVertical(rootView: ViewGroup, onItemSelected: (Da SingleSelectionParams( rootView = rootView, singleGroupSubType = SingleGroupSubType.CHIP_GROUP, - chipType = SingleChipType.ENTRY_CHIP, + chipType = SingleChipType.ACTION_CHIP, orientation = Orientation.VERTICAL, mData = mData(), onItemSelected = onItemSelected, bgSelector = R.color.chip_bg_selector, - textSelector = R.color.multiline_text_selector + textSelector = R.color.chip_text_selector ) ) fun addSingleSelectionChipGroupHorizontal(rootView: ViewGroup, onItemSelected: (Data) -> Unit) = Params.SingleSelection( @@ -109,7 +109,7 @@ fun addSingleSelectionChipGroupHorizontal(rootView: ViewGroup, onItemSelected: ( fun addMultiSelectionChipGroupVertical(rootView: ViewGroup, onItemsSelected: (List) -> Unit) = Params.MultiSelection( MultiSelectionParams( rootView = rootView, - chipType = MultiChipType.FILTER_CHIP, + chipType = MultiChipType.ACTION_CHIP, orientation = Orientation.VERTICAL, mData = mData(), onItemsSelected = onItemsSelected, @@ -139,7 +139,7 @@ fun addSingleSelectionListHorizontal(rootView: ViewGroup, onItemselected: (Data) mData = mData(), onItemSelected = onItemselected, bgSelector = R.drawable.ic_check_selector, - textSelector = R.color.chip_text_selector + textSelector = R.color.text_color_selector ) ) fun addSingleSelectionListVertical(rootView: ViewGroup, onItemselected: (Data) -> Unit) = Params.SingleSelection( @@ -151,7 +151,7 @@ fun addSingleSelectionListVertical(rootView: ViewGroup, onItemselected: (Data) - mData = mData(), onItemSelected = onItemselected, bgSelector = R.drawable.ic_check_selector, - textSelector = R.color.chip_text_selector + textSelector = R.color.text_color_selector ) ) fun addMultiSelectionListVertical(rootView: ViewGroup, onItemsSelected: (List) -> Unit) = Params.MultiSelection( @@ -162,8 +162,8 @@ fun addMultiSelectionListVertical(rootView: ViewGroup, onItemsSelected: (List) -> Unit) = Params.MultiSelection( @@ -174,7 +174,7 @@ fun addMultiSelectionListHorizontal(rootView: ViewGroup, onItemsSelected: (List orientation = Orientation.HORIZONTAL, mData = mData(), onItemsSelected = onItemsSelected, - bgSelector = R.color.chip_bg_selector, - textSelector = R.color.chip_text_selector + bgSelector = R.drawable.ic_check_selector, + textSelector = R.color.text_color_selector ) ) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 90c886e..2f0efe7 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,6 +1,5 @@ 8dp diff --git a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/data/SingleSelectionParams.kt b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/data/SingleSelectionParams.kt index 091ac73..a36673c 100644 --- a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/data/SingleSelectionParams.kt +++ b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/data/SingleSelectionParams.kt @@ -17,13 +17,7 @@ data class SingleSelectionParams( val orientation: Int, val mData: ArrayList, val onItemSelected: (Data) -> Unit, - @DrawableRes val bgSelector: Int =android.R.color.darker_gray, + @DrawableRes val bgSelector: Int = R.drawable.abc_btn_radio_material, @ColorRes val textSelector: Int = android.R.color.black ) -/* -@DrawableRes val bgSelector: Int = com.ss.smartfilterlib.R.drawable.multiline_default, -@ColorRes val textSelector: Int = com.ss.smartfilterlib.R.color.black*/ -/* -@ColorRes val bgSelector: Int = android.R.color.darker_gray, -@ColorRes val textSelector: Int = android.R.color.black*/ diff --git a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/multiselection/MultiSelectionListView.kt b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/multiselection/MultiSelectionListView.kt index 8810484..2a47b36 100644 --- a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/multiselection/MultiSelectionListView.kt +++ b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/multiselection/MultiSelectionListView.kt @@ -13,11 +13,19 @@ import com.ss.smartfilterlib.utils.BaseRecycleView /** - * created by Mala Ruparel ON 02/05/24 + * MultiSelectionListView is a custom view that allows multiple selection from a list of options. + * The options are presented in a RecyclerView. + * + * @property smartOrientation The orientation of the RecyclerView, can be either vertical or horizontal. + * @property viewTextSelector The ColorStateList for the text color of the list items. + * @property checkSelector The resource ID for the drawable to use when an item is checked. + * @property dataFromXml The resource ID of the string array containing the list items. + * @property onMultiSelectionClicked The listener for checked change events of the list items. + * + * @constructor Creates a new MultiSelectionListView with the given context, attrs, and defStyle. */ class MultiSelectionListView @JvmOverloads constructor( context: Context,attrs: AttributeSet? = null,defStyle: Int = 0) : BaseRecycleView(context, attrs, defStyle){ - init { initAttributes(attrs=attrs) initializeView() @@ -25,7 +33,6 @@ class MultiSelectionListView @JvmOverloads constructor( context: Context,attrs: } - override fun initializeView() { layoutManager = when (smartOrientation) { SmartOrientation.VERTICAL -> LinearLayoutManager(context, VERTICAL, false) @@ -48,14 +55,11 @@ class MultiSelectionListView @JvmOverloads constructor( context: Context,attrs: } private fun populateDataFromAttributes() { if (dataFromXml != 0) { - val mData = resources.getStringArray(dataFromXml); - mData.forEach { - val data = Data(name = it) - mList.add(data) + resources.getStringArray(dataFromXml).forEach { + mList.add(Data(name = it)) } setItems(mList) } - } fun configureView(data: ArrayList, orientation: Int, checkSelector: Int, primaryTextColor: Int, onCheckedChangeListener: ((List) -> Unit)?) { diff --git a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/multiselection/MultiselectionChipGroup.kt b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/multiselection/MultiselectionChipGroup.kt index 4f17087..7842ba1 100644 --- a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/multiselection/MultiselectionChipGroup.kt +++ b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/multiselection/MultiselectionChipGroup.kt @@ -2,6 +2,7 @@ package com.ss.smartfilterlib.multiselection import android.content.Context import android.util.AttributeSet +import android.view.View import android.widget.HorizontalScrollView import android.widget.ScrollView import androidx.core.content.ContextCompat @@ -39,19 +40,17 @@ class MultiselectionChipGroup @JvmOverloads constructor(context: Context, attrs: } private fun populateDataFromAttributes() { if (dataFromXml != 0) { - val mData = resources.getStringArray(dataFromXml) - val data = mData.map { Data(name = it) } as ArrayList setOrientation() - setItems(data, MultiChipType.ENTRY_CHIP) + setItems(resources.getStringArray(dataFromXml).map { Data(name = it) }, MultiChipType.ENTRY_CHIP) } } override fun initializeView() { - containerScrollView = ScrollView(context) - containerHorizontalScrollView = HorizontalScrollView(context) - - val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) - containerScrollView.layoutParams = layoutParams - containerHorizontalScrollView.layoutParams = layoutParams + containerScrollView = ScrollView(context).apply { + layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) + } + containerHorizontalScrollView = HorizontalScrollView(context).apply { + layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) + } chipGroup = ChipGroup(context) } @@ -106,6 +105,7 @@ class MultiselectionChipGroup @JvmOverloads constructor(context: Context, attrs: return Chip(context, null, R.style.EntryChipStyle).apply { isClickable = true isCheckedIconVisible = false + textAlignment = View.TEXT_ALIGNMENT_CENTER } } @@ -137,6 +137,7 @@ class MultiselectionChipGroup @JvmOverloads constructor(context: Context, attrs: return Chip(context, null, R.style.FilterChipStyle).apply { isCloseIconVisible = false isChipIconVisible = true + textAlignment = View.TEXT_ALIGNMENT_CENTER } } @@ -174,6 +175,11 @@ class MultiselectionChipGroup @JvmOverloads constructor(context: Context, attrs: multiCheckedChangeListener?.invoke( checkedChipIds.toList()) } + chip.setOnCloseIconClickListener { + chipGroup.removeView(chip) + checkedChipIds.remove(chip.id) + multiCheckedChangeListener?.invoke(checkedChipIds.toList()) + } } diff --git a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionChipGroup.kt b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionChipGroup.kt index c3fe865..cc9dbb9 100644 --- a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionChipGroup.kt +++ b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionChipGroup.kt @@ -45,22 +45,19 @@ class SingleSelectionChipGroup @JvmOverloads constructor(context: Context, attrs } override fun initializeView() { - - containerScrollView = ScrollView(context) - containerHorizontalScrollView = HorizontalScrollView(context) - - val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) - containerScrollView.layoutParams = layoutParams - containerHorizontalScrollView.layoutParams = layoutParams + containerScrollView = ScrollView(context).apply { + layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) + } + containerHorizontalScrollView = HorizontalScrollView(context).apply { + layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) + } chipGroup = ChipGroup(context) } private fun populateDataFromAttributes() { if (dataFromXml != 0) { - val mData = resources.getStringArray(dataFromXml); - val data = mData.map { Data(name = it) } as ArrayList setOrientation() - setItems(data, SingleChipType.ENTRY_CHIP) + setItems(resources.getStringArray(dataFromXml).map { Data(name = it) }, SingleChipType.ENTRY_CHIP) } } fun configureView(chipData: List,chipType: SingleChipType,orientation: Int,bgSelector: Int, textSelector: Int, checkedChangedListener: ( Data) -> Unit,) { @@ -107,6 +104,7 @@ class SingleSelectionChipGroup @JvmOverloads constructor(context: Context, attrs return Chip(context, null, R.style.EntryChipStyle).apply { isClickable = true isCheckedIconVisible=false + textAlignment = View.TEXT_ALIGNMENT_CENTER } } @@ -171,6 +169,10 @@ class SingleSelectionChipGroup @JvmOverloads constructor(context: Context, attrs chip.setOnCheckedChangeListener { _, _ -> singleCheckedChangeListener?.invoke(chip.tag as Data) } + chip.setOnCloseIconClickListener { + chipGroup.removeView(chip) + singleCheckedChangeListener?.invoke(chip.tag as Data) + } } private fun generateViewWithId(radioButton: Chip, data: Data) { radioButton.id = View.generateViewId() diff --git a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionItemRadioGroup.kt b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionItemRadioGroup.kt index ea3d53b..dd35823 100644 --- a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionItemRadioGroup.kt +++ b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionItemRadioGroup.kt @@ -17,7 +17,17 @@ import com.ss.smartfilterlib.databinding.RowItemBinding import com.ss.smartfilterlib.utils.BaseLinearLayout /** - * created by Mala Ruparel ON 19/04/24 + * SingleSelectionItemRadioGroup is a custom view that allows single selection from a list of options. + * The options are presented as RadioButtons within a RadioGroup. + * The RadioGroup can be oriented either horizontally or vertically. + * + * @property smartOrientation The orientation of the RadioGroup, can be either horizontal or vertical. + * @property viewTextSelector The ColorStateList for the text color of the RadioButtons. + * @property viewBgSelector The Drawable for the background of the RadioButtons. + * @property dataFromXml The resource ID of the string array containing the list items. + * @property singleCheckedChangeListener The listener for checked change events of the RadioButtons. + * + * @constructor Creates a new SingleSelectionItemRadioGroup with the given context, attrs, and defStyle. */ class SingleSelectionItemRadioGroup(context: Context, attrs: AttributeSet? =null,defStyle: Int = 0) : BaseLinearLayout(context, attrs, defStyle) { @@ -41,24 +51,21 @@ class SingleSelectionItemRadioGroup(context: Context, attrs: AttributeSet? =null } } } - private fun populateDataFromAttributes(){ + private fun populateDataFromAttributes() { if (dataFromXml != 0) { - val mData = resources.getStringArray(dataFromXml); setOrientation() - mData.forEach { - val data = Data(name = it) - addRadioButtonView(data) + resources.getStringArray(dataFromXml).forEach { + addRadioButtonView(Data(name = it)) } } } override fun initializeView() { - containerScrollView = ScrollView(context) - containerHorizontalScrollView = HorizontalScrollView(context) - - val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) - containerScrollView.layoutParams = layoutParams - containerHorizontalScrollView.layoutParams = layoutParams - + containerScrollView = ScrollView(context).apply { + layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) + } + containerHorizontalScrollView = HorizontalScrollView(context).apply { + layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) + } radioGroup = RadioGroup(context) } diff --git a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionListView.kt b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionListView.kt index eb887ad..a757f3d 100644 --- a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionListView.kt +++ b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionListView.kt @@ -13,7 +13,16 @@ import com.ss.smartfilterlib.utils.BaseRecycleView typealias SmartOrientation = com.ss.smartfilterlib.utils.Orientation /** - * created by Mala Ruparel ON 02/05/24 + * SingleSelectionListView is a custom view that allows single selection from a list of options. + * The options are presented in a RecyclerView. + * + * @property smartOrientation The orientation of the RecyclerView, can be either vertical or horizontal. + * @property viewTextSelector The ColorStateList for the text color of the list items. + * @property checkSelector The resource ID for the drawable to use when an item is checked. + * @property dataFromXml The resource ID of the string array containing the list items. + * @property onSingleSelectionClicked The listener for click events of the list items. + * + * @constructor Creates a new SingleSelectionListView with the given context, attrs, and defStyle. */ class SingleSelectionListView @JvmOverloads constructor( context: Context,attrs: AttributeSet? = null,defStyle: Int = 0) : BaseRecycleView(context, attrs, defStyle){ @@ -48,15 +57,12 @@ class SingleSelectionListView @JvmOverloads constructor( context: Context,attrs: } private fun populateDataFromAttributes() { if (dataFromXml != 0) { - val mData = resources.getStringArray(dataFromXml); initializeView() - mData.forEach { - val data = Data(name = it) - mList.add(data) + resources.getStringArray(dataFromXml).forEach { + mList.add(Data(name = it)) } setItems(mList) } - } fun configureView(data: ArrayList, orientation: Int, checkSelector: Int, textSelector: Int, onCheckedChangeListener: ((Data) -> Unit)?){ diff --git a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionMultiLineRadioButton.kt b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionMultiLineRadioButton.kt index 097ffd4..d12f555 100644 --- a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionMultiLineRadioButton.kt +++ b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionMultiLineRadioButton.kt @@ -20,7 +20,21 @@ import com.ss.smartfilterlib.utils.Constant.DEFAULT_SPACING import com.ss.smartfilterlib.utils.Constant.DEFAULT_SPAN_COUNT import com.ss.smartfilterlib.utils.GridSpacingItemDecoration import com.ss.smartfilterlib.utils.SingleChangeDiffUtil - +/** + * SingleSelectionMultiLineRadioButton is a custom view that allows single selection from a list of options. + * The options are presented as RadioButtons within a RecyclerView. + * The RecyclerView is arranged in a grid layout. + * + * @property spanCount The number of columns in the grid layout. + * @property spacing The spacing between items in the grid. + * @property includeEdge Whether to include edge spacing in the grid. + * @property viewTextSelector The ColorStateList for the text color of the RadioButtons. + * @property viewBgSelector The Drawable for the background of the RadioButtons. + * @property dataFromXml The resource ID of the string array containing the list items. + * @property onSingleSelectionClicked The listener for click events of the RadioButtons. + * + * @constructor Creates a new SingleSelectionMultiLineRadioButton with the given context, attrs, and defStyle. + */ class SingleSelectionMultiLineRadioButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) : BaseRecycleView(context, attrs, defStyle){ @@ -32,7 +46,7 @@ class SingleSelectionMultiLineRadioButton @JvmOverloads constructor(context: Con override fun initAttributes(attrs: AttributeSet?) { val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.MultiLineRadioGroup, 0, 0) - with(typedArray) { + with(typedArray) { try { setColumnCount(getInt(R.styleable.MultiLineRadioGroup_rg_ml_spancount,DEFAULT_SPAN_COUNT)) setSpace(getInt(R.styleable.MultiLineRadioGroup_rg_ml_spacing, DEFAULT_SPACING)) @@ -56,12 +70,10 @@ class SingleSelectionMultiLineRadioButton @JvmOverloads constructor(context: Con adapter = MultiLineRadioButtonAdapter(context,onSingleSelectionClicked).also { this.mAdapter = it } } - private fun populateDataFromAttributes(){ + private fun populateDataFromAttributes() { if (dataFromXml != 0) { - val mData = resources.getStringArray(dataFromXml); - mData.forEach { - val data = Data(name = it) - mList.add(data) + resources.getStringArray(dataFromXml).forEach { + mList.add(Data(name = it)) } mAdapter?.setData(mList) } diff --git a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionRadioGroup.kt b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionRadioGroup.kt index a0f528d..046c74c 100644 --- a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionRadioGroup.kt +++ b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/singleselection/SingleSelectionRadioGroup.kt @@ -18,6 +18,20 @@ import com.ss.smartfilterlib.data.Data import com.ss.smartfilterlib.utils.BaseLinearLayout import com.ss.smartfilterlib.utils.Orientation +/** + * SingleSelectionRadioGroup is a custom view that allows single selection from a list of options. + * The options are presented as RadioButtons within a RadioGroup. + * The orientation of the RadioGroup can be set to either vertical or horizontal. + * The view also supports custom attributes for text color, background, and list items. + * + * @property smartOrientation The orientation of the RadioGroup, can be either vertical or horizontal. + * @property viewTextSelector The ColorStateList for the text color of the RadioButtons. + * @property viewBgSelector The Drawable for the background of the RadioButtons. + * @property dataFromXml The resource ID of the string array containing the list items. + * @property singleCheckedChangeListener The listener for checked change events of the RadioButtons. + * + * @constructor Creates a new SingleSelectionRadioGroup with the given context, attrs, and defStyle. + */ /** * created by Mala Ruparel ON 17/04/24 @@ -49,23 +63,20 @@ class SingleSelectionRadioGroup @JvmOverloads constructor(context: Context, attr private fun populateDataFromAttributes() { if (dataFromXml != 0) { - val mData = resources.getStringArray(dataFromXml); setOrientation() - mData.forEach { - val data = Data(name = it) - addRadioButtonView(data) + resources.getStringArray(dataFromXml).forEach { + addRadioButtonView(Data(name = it)) } } } override fun initializeView() { - containerScrollView = ScrollView(context) - containerHorizontalScrollView = HorizontalScrollView(context) - - val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) - containerScrollView.layoutParams = layoutParams - containerHorizontalScrollView.layoutParams = layoutParams + containerScrollView = ScrollView(context).apply { + layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) + } + containerHorizontalScrollView = HorizontalScrollView(context).apply { + layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) + } radioGroup = RadioGroup(context) - } private fun setOrientation() { @@ -122,14 +133,16 @@ class SingleSelectionRadioGroup @JvmOverloads constructor(context: Context, attr applyTextAttributes(this) applyPaddingAttributes(this) applySelector(this) + generateViewWithId(radioButton, data) } - generateViewWithId(radioButton, data) - radioGroup.addView(radioButton) - radioGroup.setOnCheckedChangeListener { _, checkedId -> - val checkedRadioButton: RadioButton = findViewById(checkedId) - val checkedData = checkedRadioButton.tag as Data? - checkedData?.let { singleCheckedChangeListener?.invoke(it) } + radioGroup.apply { + addView(radioButton) + setOnCheckedChangeListener { _, checkedId -> + val checkedRadioButton: RadioButton = findViewById(checkedId) + val checkedData = checkedRadioButton.tag as Data? + checkedData?.let { singleCheckedChangeListener?.invoke(it) } + } } } @@ -141,8 +154,11 @@ class SingleSelectionRadioGroup @JvmOverloads constructor(context: Context, attr private fun generateViewWithId(radioButton: RadioButton, data: Data) { - radioButton.id = View.generateViewId() - radioButton.tag = data + radioButton.apply { + id = View.generateViewId() + tag = data + } + } private fun setDefaultDrawable() : Drawable?{ diff --git a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/utils/Attributes.kt b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/utils/Attributes.kt index 9a6e38d..a3430a1 100644 --- a/ss-smart-filter/src/main/java/com/ss/smartfilterlib/utils/Attributes.kt +++ b/ss-smart-filter/src/main/java/com/ss/smartfilterlib/utils/Attributes.kt @@ -15,7 +15,7 @@ data class TextAttributes( val textEllipsize: Int = 0 ) data class PaddingAttributes( - val paddingStart: Int = 20, + val paddingStart: Int = 30, val paddingTop: Int = 20, val paddingEnd: Int = 20, val paddingBottom: Int = 20 diff --git a/ss-smart-filter/src/main/res/color/chip_text_selector.xml b/ss-smart-filter/src/main/res/color/chip_text_selector.xml index c63b697..dfe1ef2 100644 --- a/ss-smart-filter/src/main/res/color/chip_text_selector.xml +++ b/ss-smart-filter/src/main/res/color/chip_text_selector.xml @@ -1,14 +1,6 @@ - - - - - + + + \ No newline at end of file diff --git a/ss-smart-filter/src/main/res/color/multi_line_stroke.xml b/ss-smart-filter/src/main/res/color/multi_line_stroke.xml deleted file mode 100644 index 4f129d5..0000000 --- a/ss-smart-filter/src/main/res/color/multi_line_stroke.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/ss-smart-filter/src/main/res/color/single_text_color_selector.xml b/ss-smart-filter/src/main/res/color/text_color_selector.xml similarity index 100% rename from ss-smart-filter/src/main/res/color/single_text_color_selector.xml rename to ss-smart-filter/src/main/res/color/text_color_selector.xml diff --git a/ss-smart-filter/src/main/res/drawable/multiline_selector.xml b/ss-smart-filter/src/main/res/drawable/multiline_selector.xml deleted file mode 100644 index 6d4e5d9..0000000 --- a/ss-smart-filter/src/main/res/drawable/multiline_selector.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/ss-smart-filter/src/main/res/drawable/singleline_rb_selector.xml b/ss-smart-filter/src/main/res/drawable/rb_selector.xml similarity index 100% rename from ss-smart-filter/src/main/res/drawable/singleline_rb_selector.xml rename to ss-smart-filter/src/main/res/drawable/rb_selector.xml