Kotlin / anko

Pleasant Android application development
Apache License 2.0
15.88k stars 1.29k forks source link

Proposal: easier view visibility #391

Open neworld opened 7 years ago

neworld commented 7 years ago

Current Android way to change the visibility of views is not very clean. What if we could have these:

button.gone()
title.visibleIf(item.title != null, otherwise = View::gone)

Interface could be:

fun View.gone()
fun View.visible()
fun View.invisible()

fun View.visibleIf(state: Boolean, otherwise: View.() -> Unit)

If these little helpers could be a part of Anko, I would like to implement this.

yanex commented 7 years ago

👍

By the way, what about makeGone(), makeVisible(), makeInvisible()?

neworld commented 7 years ago

Naming depends. I personally and my colleagues prefer shorter. It is pretty intuitive, heavily used, especially my beloved visibleIf(...). However, the interface has to be consistent, so I don't see any problem to use longer names if they are following some consistency or conventions.

Pfuster12 commented 6 years ago

Has this been implemented? I am personally using similar helpers and they're just what Anko commons should have. My extension functions, which are as concise as image_view.show():

// shows this view
fun View.show(): View {
    this.visibility = View.VISIBLE
    return this
}
// hides the view
fun View.hide(): View {
    this.visibility = View.INVISIBLE
    return this
}
// makes the view gone
fun View.gone(): View {
    this.visibility = View.GONE
    return this
}