title: How to make width of a view dependent on other view in a recyclerview?
body: <p>I want to measure the width of a cardview and match to the cardview that is on its outside, to get a clear understanding I will attach an image along with the question:</p>
  
  <p><a href="https://i.stack.imgur.com/idDVU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/idDVU.png" alt="enter image description here"></a></p>
  
  <p>Here, the inner cardview should be same width of the outer cardview in every case where inner cardview is the reply chat bubble and outer is the chat message bubble.</p>
  
  <p>I have tried extending the cardview:</p>
  
  <pre><code>public class ExtendCardView extends CardView {
  
  Integer newWidth;
  
  public ExtendCardView(Context context) {
      super(context);
  }
  
  public ExtendCardView(Context context, AttributeSet attrs) {
      super(context, attrs);
  }
  
  
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      newWidth= MeasureSpec.getSize(widthMeasureSpec);
        super.onMeasure(newWidth, heightMeasureSpec);
  
  }
  
  public Integer getNewWidth() {
      return newWidth;
  }
  
  public void setNewWidth(Integer newWidth) {
      this.newWidth = newWidth;
  }}
  </code></pre>
  
  <p>and on the onBindView of adapter I have added a view tree observer to change the width:</p>
  
  <pre><code>((CardView) holder).inner_card_view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
          @SuppressLint("NewApi")
          @SuppressWarnings("deprecation")
          @Override
          public void onGlobalLayout() {
              //((CardView) holder).inner_card_view.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY);
  
              ((CardView) holder).inner_card_view.setNewWidth(((CardView) holder).outer_card_view.getNewWidth());
              //now we can retrieve the width and height
              //...
              //do whatever you want with them
              //...
              //this is an important step not to keep receiving callbacks:
              //we should remove this listener
              //I use the function to remove it based on the api level!
  
              if(android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.JELLY_BEAN)
                  ((CardView) holder).inner_card_view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
              else
                  ((CardView) holder).inner_card_view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
          }
      });
  </code></pre>
  
  <p>What is the perfect way to achieve something like this, here is my layout file for the cardview:</p>
  
  <pre><code>      &lt;com.example.ui.view.ExtendCardView
      android:id="@+id/outer_card"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentEnd="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:layout_marginEnd="8dp"
      android:layout_marginLeft="50dp"
      android:layout_marginRight="8dp"
      android:layout_marginStart="50dp"
      app:cardCornerRadius="8dp"&gt;
  
  
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"&gt;
  
          &lt;RelativeLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="2dp"&gt;
  
              &lt;TextView
                  android:id="@+id/tv_name"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentTop="true"
                  android:textAppearance="@style/TextAppearance.AppCompat.Small"
                  android:textColor="@color/black" /&gt;
  
              &lt;com.example.ui.view.ExtendCardView
                  android:id="@+id/inner_card"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/tv_name"
                  android:layout_margin="1dp"
                  android:minWidth="80dp"
                  app:cardBackgroundColor="@color/red"
                  app:cardCornerRadius="8dp"
                  app:cardElevation="0dp"&gt;
  
                  &lt;LinearLayout
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:orientation="horizontal"&gt;
  
                      &lt;RelativeLayout
                          android:id="@+id/rel_layout"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_margin="5dp"
                          android:layout_marginEnd="15dp"
                          android:layout_marginRight="15dp"&gt;
  
                          &lt;TextView
                              android:id="@+id/tv_user_name"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_alignParentLeft="true"
                              android:layout_alignParentStart="true"
                              android:textStyle="bold" /&gt;
  
                          &lt;LinearLayout
                              android:id="@+id/lin_layout"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_below="@id/tv_user_name"
                              android:layout_marginBottom="8dp"
                              android:orientation="horizontal"&gt;
  
                              &lt;ImageView
                                  android:id="@+id/iv_type"
                                  android:layout_width="18dp"
                                  android:layout_height="18dp"
                                  android:layout_marginEnd="4dp"
                                  android:layout_marginRight="4dp"
                                  android:tint="@color/dark_grey"
                                  android:visibility="gone" /&gt;
  
                              &lt;TextView
                                  android:id="@+id/tv_message"
                                  android:layout_width="wrap_content"
                                  android:layout_height="wrap_content"
                                  android:autoLink="web"
                                  android:maxLines="3" /&gt;
                          &lt;/LinearLayout&gt;
  
  
                      &lt;/RelativeLayout&gt;
  
                      &lt;ImageView
                          android:id="@+id/iv_media"
                          android:layout_width="40dp"
                          android:layout_height="40dp"
                          android:layout_gravity="center_vertical"
                          android:layout_marginEnd="8dp"
                          android:layout_marginLeft="15dp"
                          android:layout_marginRight="8dp"
                          android:layout_marginStart="15dp"
                          android:visibility="gone" /&gt;
                  &lt;/LinearLayout&gt;
  
              &lt;/com.nodd.nodd.ui.view.ExtendCardView&gt;
  
              &lt;TextView
                  android:id="@+id/tv_reply_message"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true"
                  android:layout_below="@+id/inner_card"
                  android:layout_marginBottom="5dp"
                  android:layout_marginEnd="4dp"
                  android:layout_marginLeft="4dp"
                  android:layout_marginRight="4dp"
                  android:layout_marginStart="4dp"
                  android:autoLink="web"
                  android:linksClickable="true"
                  android:textColor="@color/white"
                  android:textColorLink="@color/linkedin_blue" /&gt;
  
              &lt;ImageView
                  android:id="@+id/tv_reply_image"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true"
                  android:layout_below="@+id/inner_card"
                  android:visibility="gone"
                  android:layout_marginBottom="5dp"
                  android:layout_marginEnd="4dp"
                  android:layout_marginLeft="4dp"
                  android:layout_marginRight="4dp"
                  android:layout_marginStart="4dp"
                  android:layout_marginTop="5dp" /&gt;
          &lt;/RelativeLayout&gt;
  
      &lt;/RelativeLayout&gt;
  &lt;/com.example.ui.view.ExtendCardView&gt;
  </code></pre>