title: How do I 'findViewById' in fragment with RecyclerView?
body: <p>I have a ConstraintLayout inside RecyclerView which lives in a fragment. I want to change the dimensions of the ConstraintLayout (based on screen resolution) but can't seem to find the right way of getting the ConstraintLayout in code.</p>
  
  <p>I've tried:</p>
  
  <p>view!!.findViewById(R.id.postConstraint)</p>
  
  <p>activity!!.findViewById(R.id.postConstraint)</p>
  
  <p>Both of which return null.</p>
  
  <p>My Fragment</p>
  
  <pre><code>package no.alacho.pgr202_positivity
  
  import android.graphics.Color
  import android.graphics.Point
  import android.os.Bundle
  import android.util.Log
  import android.view.Display
  import android.view.LayoutInflater
  import android.view.View
  import android.view.ViewGroup
  import android.widget.Button
  import androidx.constraintlayout.widget.ConstraintLayout
  import androidx.fragment.app.Fragment
  import androidx.recyclerview.widget.GridLayoutManager
  import kotlinx.android.synthetic.main.fragment_main.*
  import kotlinx.android.synthetic.main.single_post_item.*
  
  class Posts : Fragment() {
  
    private lateinit var layoutManager: GridLayoutManager
    private lateinit var adapter: PostAdapter
    private lateinit var display: Display
    private lateinit var size: Point
  
    override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      display = activity!!.windowManager.defaultDisplay
      size = Point()
      display.getSize(size)
    }
  
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
      return inflater.inflate(R.layout.fragment_main, container, false)
    }
  
    override fun onActivityCreated(savedInstanceState: Bundle?) {
      super.onActivityCreated(savedInstanceState)
  
      layoutManager = GridLayoutManager(activity, 2)
      postRecycler.layoutManager = GridLayoutManager(activity, 2)
      adapter = PostAdapter()
      postRecycler.adapter = adapter
  
      //view!!.findViewById&lt;Button&gt;(R.id.hurraButton).text =  "Something"
  
      view!!.findViewById&lt;ConstraintLayout&gt;(R.id.postConstraint).minWidth = 100
      activity!!.findViewById&lt;ConstraintLayout&gt;(R.id.postConstraint).minWidth = 100
    }
  }
  </code></pre>
  
  <p>And my XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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:layout_height="wrap_content"
          android:minHeight="150dp"
          android:minWidth="150dp"
          android:layout_width="wrap_content"
          android:backgroundTint="#FF0000"
          android:background="#FF0000"
          android:id="@+id/postConstraint"&gt;
  
      &lt;Button android:layout_width="wrap_content"
              android:id="@+id/hurraButton"
              android:layout_height="wrap_content"
              android:text="Hallo"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              android:layout_marginEnd="8dp"
              android:layout_marginBottom="8dp"
              app:layout_constraintBottom_toBottomOf="parent"/&gt;
  
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>