Jay Garzon
Jay Garzon

Reputation: 41

Is there a way to put a margin between views programmatically in Android Kotlin?

Good day. I'm new to android development and I am having a hard time setting margin for my view inside my kotlin file.

I tried all the guides I've seen but nothing seems to work.

Would someone take the time to check my code?

What I am trying to accomplish is inside a "for loop", I want to create a cardview with a textview inside and add it to the linear layout while adding some margin for each cardview.

This is my code:

package com.fangs.number_sorter_app

import android.app.ActionBar
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView

class NumberSorterActivity : AppCompatActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_number_sorter)




    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        val size = NumberHolder.getSize()
        val listOfNumbersLayout = findViewById<LinearLayout>(R.id.ll_list_of_numbers)



        for (item in 1..size!!) {

            val newCardView = CardView(this)
            newCardView.foregroundGravity = Gravity.CENTER_HORIZONTAL
            newCardView.setCardBackgroundColor(Color.parseColor("#FF6200EE"))

            val text = TextView(this)
            text.gravity = Gravity.CENTER
            text.text = "test"
            text.setTextColor(Color.WHITE)
            newCardView.addView(text)
            listOfNumbersLayout.addView(newCardView,100,100)

        }

    }

}

Thank you and hope ya'll are doing great.

Edited: I would just like to add this code change that I made:


import android.app.ActionBar
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView

class NumberSorterActivity : AppCompatActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_number_sorter)

    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        val size = NumberHolder.getSize()
        val listOfNumbersLayout = findViewById<LinearLayout>(R.id.ll_list_of_numbers)



        for (item in 1..size!!) {

            val newCardView = CardView(this)
            newCardView.foregroundGravity = Gravity.CENTER_HORIZONTAL
            newCardView.setCardBackgroundColor(Color.parseColor("#FF6200EE"))

            val text = TextView(this)
            text.gravity = Gravity.CENTER
            text.text = "test"
            text.setTextColor(Color.WHITE)
            newCardView.addView(text)

            //get layout of newCardView
            val params = newCardView.layoutParams as ViewGroup.MarginLayoutParams
            params.setMargins(10,10,10,10)
            //apply params
            newCardView.layoutParams = params

            listOfNumbersLayout.addView(newCardView,100,100)

        }

    }

}

I keep on having this error:

        java.lang.NullPointerException: null cannot be cast to non-null type android.view.ViewGroup.MarginLayoutParams
                at com.fangs.number_sorter_app.NumberSorterActivity.onWindowFocusChanged(NumberSorterActivity.kt:42)
               at androidx.appcompat.view.WindowCallbackWrapper.onWindowFocusChanged(WindowCallbackWrapper.java:124)

Upvotes: 0

Views: 1426

Answers (1)

Ryan
Ryan

Reputation: 1050

This line is what is failing:

val params = newCardView.layoutParams as ViewGroup.MarginLayoutParams

This is because newCardView.layoutParams is returning null. The attempt to downcast null to ViewGroup.MarginLayoutParams is what's causing your error.

You need to first create the layout params, and then set them:

val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONENT)
layoutParams.setMargins(0, 0, 0, 0)
newCardView.layoutParams = layoutParams

Upvotes: 1

Related Questions