Kotlin doOnLayout not setting visibility.GONE

Viewed 576

I have a custom ConstraintLayout Myword. when I set the visibility of myword1 to GONE inside kotlin fun doOnLayout, it becomes invisible but myword2 doesn't move down.

But if I set its visibility to GONE in XML or outside doOnLayout, it works as intended.

<learnprogramming.academy.relaf.Myword
    android:id="@+id/myword1"
    android:layout_width="0dp"

    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<learnprogramming.academy.relaf.Myword
    android:id="@+id/myword2"
    android:layout_width="0dp"

    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@id/myword1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

Source code:

lateinit var bannerword : MutableList<Myword>
lateinit var swippon : ConstraintLayout


fun updatewords{
        swippon.doOnLayout {
            if (swippon.height>1000)    bannerword[0].visibility = View.GONE}

class PhrasesFragment: Fragment() {



        companion object {
            fun newInstance(): PhrasesFragment {
                return PhrasesFragment()
            }
        }

        override fun onCreateView(inflater: LayoutInflater,
                                  container: ViewGroup?,
                                  savedInstanceState: Bundle?): View? {
            return inflater.inflate(R.layout.inglesa_screen, container, false) }

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)

             bannerword=mutableListOf(myword1,myword2)
             swippon=parent
             updatewords()
        }
    }

doOnLayout function when layout is laid out calculates parent Layout height and eventually removes myword1. I actually solved changing doOnLayout with Handler(Looper.getMainLooper()).post{} but is wrong with doOnLayout? How should i have used it in the correct way?

0 Answers
Related