Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting height and width of an Anko view to match_parent

I would like to set the height and width of an video view to match_parent. My code looks something like the following. It works without the height and width attributes, but doing it as below give me a val cannot be reassigned error.

class VideoActivityUI : AnkoComponent<VideoActivity> {
    companion object {
        val ID_VIDEO = 11
    }

    override fun createView(ui: AnkoContext<VideoActivity>) = with(ui) {
        videoView{
            id = ID_VIDEO
            height = matchParent
            width = matchParent
        }
    }
}
like image 235
Muz Avatar asked Mar 08 '23 21:03

Muz


1 Answers

You have to use lparams to set layout parameters, like this (you can omit the explicit parameter names if you want to):

videoView {
    id = ID_VIDEO
}.lparams (width = matchParent, height = matchParent)

Alternatively, you can do it like this:

videoView {
    id = ID_VIDEO
}.lparams {
    height = matchParent
    width = matchParent
}

The related wiki section for Anko can be found here.


Note that you have to have a ViewGroup around your VideoView as the root of the Activity's layout for it to have layout parameters available, because it gets different ones depending on whether it's in a FrameLayout, LinearLayout, or RelativeLayout.

For example, with a simple frameLayout, your code would look like this:

override fun createView(ui: AnkoContext<VideoActivity>) = with(ui) {
    frameLayout {
        videoView {
            id = ID_VIDEO
        }.lparams(matchParent, matchParent)
    }
}
like image 67
zsmb13 Avatar answered Mar 24 '23 18:03

zsmb13