Using ConstraintSet with ViewBinding

798 views Asked by At

I can't a programmed button to appear with ConstraintSet when used in conjunction with ViewBinding. It doesn't crash, the button just doesn't appear. I can't work out why. The print statements shows btn is at 0,0 and zero height and width. Before ViewBinding I was able to create and position buttons etc with ConstraintSet so any help I can get to solve this would be great.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/topView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_200"
    tools:context=".MainActivity"/>

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import com.randrdevelop.viewbinding.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        displayCustomBtn1()
//        displayCustomBtn2()
    }

    private fun displayCustomBtn1() {
        val btn = Button(this)
        val set = ConstraintSet()
        val layout: ConstraintLayout = binding.topView
        btn.text = "Programmed Button 1"
        binding.topView.addView(btn)
        btn.id = View.generateViewId()
        set.clone(layout)
        set.centerHorizontally(btn.id, layout.id)
        set.centerVertically(btn.id, layout.id)
        set.applyTo(layout)
        var array = intArrayOf(-1, -1)
        btn.getLocationOnScreen(array)
        println("Note height ${btn.height}")
        println("Note width ${btn.width}")
        println("Note Location ${array[0]} ${array[1]}")
    }

    private fun displayCustomBtn2() {
        // no constraint set. This works.
        val btn = Button(this)
        btn.text = "Programmed Button 2"
        binding.topView.addView(btn)
    }
}

Screenshot

1

There are 1 answers

6
Nisanth Reddy On

UPDATE

Your code is working perfectly. What issue are you still facing ? Can you add screenshots.

This is the code I am running

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        displayCustomBtn()
    }

    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private fun displayCustomBtn() {
        val btn = Button(this)
        val set = ConstraintSet()
        val layout: ConstraintLayout = binding.topView
        btn.text = "Programmed Button 1"
        binding.topView.addView(btn)
        btn.id = View.generateViewId()
        set.clone(layout)
        set.centerHorizontally(btn.id, layout.id)
        set.centerVertically(btn.id, layout.id)
        set.applyTo(layout)
    }
}

Output

enter image description here

While enabling viewBinding,

Have you changed your setContentView(R.layout.activity_main) to setContentView(binding.root).

This should fix your issue.