Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Remove View Dynamically #47

Open
sagarnayak opened this issue Dec 18, 2020 · 1 comment
Open

Add Remove View Dynamically #47

sagarnayak opened this issue Dec 18, 2020 · 1 comment

Comments

@sagarnayak
Copy link

I want to add views at runtime. then remove all of them and then add another set of views.

Once i do addView() it works. but when i do removeAllView(). after that addView does not work. any one have a suggestion ??

@dattran-pt19
Copy link

I have same problem, I suggest you should remove SegmentedButtonGroup and replace with the other. My code down here (parent view in my case I use a LinearLayout)

` fun setup(
listValue: ArrayList<Pair<Int?, String?>>,
sizeButton: SizeButton = SizeButton.Normal
) {
this.removeAllViews()
val segmentGroup = SegmentedButtonGroup(context).apply {
val layout = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
layoutParams = layout
setBackgroundResource(R.color.searchBackground)
setRipple(false)
radius = 8f.getSizePixel(context)
setBorder(
if (sizeButton == SizeButton.Big) 2f.getSizePixel(context) else 1f.getSizePixel(
context
),
ContextCompat.getColor(context, R.color.searchBackground),
borderDashWidth,
borderDashGap
)
selectionAnimationDuration = 96
isEnabled = disableIndex == null
isDraggable = disableIndex == null
selectedButtonRadius = 8f.getSizePixel(context)
setSelectedBackgroundColor(ContextCompat.getColor(context, R.color.white))

        setOnPositionChangedListener {
            onChangeIndex?.invoke(it)
        }
    }
    this.addView(segmentGroup)
    this.segmentGroup = segmentGroup

    for ((index, item) in listValue.withIndex()) {
        val segmentedButton = SegmentedButton(context).apply {
            val layout = LayoutParams(0, sizeButton.getSize(context), 1f)
            layoutParams = layout
            textTypeface = ResourcesCompat.getFont(context, R.font.roboto_regular)
            drawableTint = ContextCompat.getColor(context, R.color.placeHolder)
            selectedDrawableTint = ContextCompat.getColor(context, R.color.primary)
            drawableWidth = 24f.getSizePixel(context)
            drawableHeight = 24f.getSizePixel(context)
            textColor = ContextCompat.getColor(context, R.color.lightText)
            selectedTextColor = ContextCompat.getColor(context, R.color.primary)
            textSize = context.resources.getDimension(R.dimen.text_title)
            item.first?.let { drawable = ContextCompat.getDrawable(context, it) }
            item.second?.let { text = it }
            disableIndex?.let {
                if (index == it) {
                    textColor = ContextCompat.getColor(context, R.color.disable)
                    drawableTint = ContextCompat.getColor(context, R.color.disable)
                }
            }
        }
        segmentGroup.addView(segmentedButton)
    }
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants