12

What is a good way to do a horizontalLayout in anko / kotlin ? verticalLayout works fine - could set orientation on it but it feels wrong. Not sure what I am missing there.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
ligi
  • 39,001
  • 44
  • 144
  • 244

2 Answers2

18

Just use a linearLayout() function instead.

linearLayout {
    button("Some button")
    button("Another button")
}
yanex
  • 1,101
  • 8
  • 10
0

Yeah, LinearLayout is by default horizontal, but I tend to be extra specific and rather use a separate horizontalLayout function for that.

You can simply add the horizontalLayout function to your project:

  val HORIZONTAL_LAYOUT_FACTORY = { ctx: Context ->
    val view = _LinearLayout(ctx)
    view.orientation = LinearLayout.HORIZONTAL
    view
  }

  inline fun ViewManager.horizontalLayout(@StyleRes theme: Int = 0, init: _LinearLayout.() -> Unit): _LinearLayout {
      return ankoView(HORIZONTAL_LAYOUT_FACTORY, theme, init)
  }

I have opened a feature request at Anko: https://github.com/Kotlin/anko/issues/413

Martin Vysny
  • 3,088
  • 28
  • 39