https://kotlinlang.org logo
#splitties
Title
# splitties
i

ispbox

08/08/2019, 9:35 AM
I'm migrating from anko, and can't get the same effect for padding and margins: Here is an example:
return scrollView {
verticalLayout {
}.lparams {
margin = dip(buttonMargin) // Margin for all sides, except bottom (it doesn't work in ScrollView).
}
bottomPadding = dip(2 * buttonMargin) // Special padding for bottom of ScrollView.
}
I tried something like:
private val scrollView = view(::ScrollView) {
add(content, lParams {
margin = dip(buttonMargin) // Margin for all sides, except bottom (it doesn't work in ScrollView).
width = matchParent
height = matchParent
bottomPadding = dip(buttonMargin) // Special padding for bottom of ScrollView.
})
}
override val root = verticalLayout {
background = resources.getDrawable(android.R.color.white, null)
add(scrollView, lParams {
margin = dip(buttonMargin) // Margin for all sides, except bottom (it doesn't work in ScrollView).
width = matchParent
height = matchParent
bottomPadding = dip(buttonMargin) // Special padding for bottom of ScrollView.
})
}
But no margins/padding is applied at all. What's wrong?
l

louiscad

08/08/2019, 10:12 AM
Why don't you use triple quotes for the snippets? Single quoting every line makes it harder for both writing and reading (with the added bounding boxes).
@ispbox Anyway, the snippets are far from being equivalent as you have a
LinearLayout
inside a
ScrollView
in the Anko snippet, but the other way around for the Splitties snippet. For the Splitties version, you should use the
wrapInScrollView()
extension function instead of
view(::ScrollView)
, it's simpler and harder to misuse. After that, make sure you're setting the margin on the right layout params, and the padding on the right view (beware of receiver clash if any, the closest one will be picked).
i

ispbox

08/08/2019, 10:51 AM
@louiscad, thanks for quick reply.
Copy code
private val scrollView = view(::ScrollView) {
        background = resources.getDrawable(android.R.color.holo_red_dark, null)

        add(content, lParams {
            margin = dip(10)
            width = matchParent
            height = matchParent
        })
    }

    override val root = verticalLayout {
        background = resources.getDrawable(android.R.color.white, null)

        add(scrollView, lParams {
            width = matchParent
            height = matchParent
            bottomPadding = dip(10)
        })
    }
Here is how it works as desired - there is bottom padding for ScrollView, and margins for content from top, left and right. Actually I didn't find a way how to replace this code with
wrapInScrollView()
correctly.
And about equivalents: I want to thank you for Splitties, as it provides much better user experience in a case when you don't want to use activities/fragments at all. I'm going to use Conductor for my views, and Anko looks ugly in such scenario. Your approach with
Ui
interface is much better 🙂
l

louiscad

08/08/2019, 12:36 PM
Great you found a working way. If you want to use
wrapInScrollView
without having to bring your own version to support non default layout params, you could probably ditch usage of margin and use padding only for the content of the ScrollView. I'm interested to know how it works for you with Conductor, please let me know then! And thanks for the recognition, I appreciate it.
i

ispbox

08/08/2019, 4:29 PM
could probably ditch usage of margin and use padding only for the content of the ScrollView
Unfortunately that's not the case. It is Android bug with ScrollView - it cuts its content from bottom. And in case of padding I get scrollbar in not appropriate place 🙂 I'll keep you informed about my experience with Conductor + Splitties DSL.
I've found a better solution from one of the Splitties samples:
Copy code
private val scrollView = view(::ScrollView) {
        add(content, lParams {
            width = matchParent
            height = matchParent
            horizontalMargin = dip(buttonMargin) // Margin for left and right.
            verticalPadding = dip(buttonMargin)  // Padding for top and bottom. Works good with clipToPadding = false
            clipToPadding = false
        })
    }
👍 1
9 Views