Search code examples
androidkotlinsyntaxequivalent

Kotlin ternary operator


How can I convert the following code from Java to Kotlin?

Boolean mBoolean = false
view.setVisibility(mBoolean ? View.VISIBLE : View.GONE);

Solution

  • val mBoolean = false
    view.visibility = if(mBoolean) View.VISIBLE else View.GONE
    

    However I doubt it makes any sense to make mBoolean immutable here, so instead of val mBoolean I'd go with var mBoolean.