How can I scale my view to all screen by using Kotlin?


i try setting up my scaling function to scale my view to all screen in Kotlin , i was using a scaling function in java and it was working right , but when i change it to , i start having problem with Float , and calling the set Padding function , and i got a wrong scale factor , how can i resolve it please ?

in java the scale factor is 1.5 but in kotlin 1.44

 fun scaleViewAndChildren(root: View, scale: Float) {

    // Retrieve the view's layout information
    val layoutParams = root.getLayoutParams()

    // Scale the view itself
    if (layoutParams.width != ViewGroup.LayoutParams.MATCH_PARENT && layoutParams.width != ViewGroup.LayoutParams.WRAP_CONTENT) {
        layoutParams.width *= scale
    if (layoutParams.height != ViewGroup.LayoutParams.MATCH_PARENT && layoutParams.height != ViewGroup.LayoutParams.WRAP_CONTENT) {
        layoutParams.height *= scale

    // If this view has margins, scale those too
    if (layoutParams is ViewGroup.MarginLayoutParams) {
        val marginParams = layoutParams as ViewGroup.MarginLayoutParams
        marginParams.leftMargin *= scale
        marginParams.rightMargin *= scale
        marginParams.topMargin *= scale
        marginParams.bottomMargin *= scale

    // Set the layout information back into the view

    // Scale the view's padding
    root.setPadding((root.getPaddingLeft() * scale) ,
            (root.getPaddingTop() * scale) ,
            (root.getPaddingRight() * scale) ,
            (root.getPaddingBottom() * scale) )

    if (root is TextView) {
        val textView = root as TextView

                pixelsToSp(textView.context, textView.textSize * scale))



    if (root is ViewGroup) {
        val groupView = root as ViewGroup
        for (cnt in 0 until groupView.childCount) {
            if ((if (groupView.getChildAt(cnt).tag != null)
                "") as String != "Resize Done")
                scaleViewAndChildren(groupView.getChildAt(cnt), scale)


    root.setTag("Resize Done")


This is really not a Kotlin problem. You’re doing some rather nasty hackery here. One thing to remember though is that Kotlin does not coerce between different numeric types (unlike Java). It seems that you are trying to scale a view(Group) by scaling it’s various dimensions.