Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

You can choose to create a view inside the layout or create the view programmatically and add it to the layout later.

View in Layout File

This code goes inside the root of your layout file.

...

Code Block
# MainActivity.kt

val wrapper = findViewById(R.id.wrapper)

View Programmatically

Code Block
languagekotlin
# MainActivity.kt
    // Get the root layout of the activity
    val parent = findViewById<ViewGroup>(R.id.main_layout) as RelativeLayout

    // Create your custom layout
    val wrapper = LinearLayout(this)
    wrapper.orientation = LinearLayout.HORIZONTAL

    // parameters for the wrapper inside a relative layout
    val params = RelativeLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT
    )
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
    params.addRule(RelativeLayout.CENTER_HORIZONTAL)
    
    wrapper.layoutParams = params
    
    // add the wrapper to the main layout
    parent.addView(wrapper)