Can I offer two options to a paremeter value?

Viewed 56

I would like to do something like to control the "invisble state" of a View.

So I have a function that receive the View and there's a optional parameter invisibleType that can be View.INVISIBLE or View.GONE.

I would like to know how can I limit the options of this parameter for these two. Like fun makeInvisible(view: View, invisibleType: View.INVISIBLE | View.GONE)

Can it be done with Kotlin? Or my best option is create a custom enum or something like that to map the options to View.INVISBLE and View.GONE?

1 Answers

You can use enums or sealed classes to do it but you can use kotlin extension functions for more readability and understanding.

Make 3 extension function on View Object.

fun View.visible(): View {
    this.visibility = View.VISIBLE
    if (this is Group) {
        this.requestLayout()
    }
    return this
}

fun View.inVisible(): View {
    this.visibility = View.INVISIBLE
    if (this is Group) {
        this.requestLayout()
    }
    return this
}

fun View.gone(): View {
    this.visibility = View.GONE
    if (this is Group) {
        this.requestLayout()
    }
    return this
}

then you can use like this

 mView.layout_photoid_success.gone()
 mView.layoutPhotoReview.visible()
Related