title: Android: Custom class for setting the max height of a NestedScrollView does not work (there is no scrollbar)
body: <p>I am creating a custom class to set the max height of a <code>NestedScrollView</code>, based on the answer provided in this StackOverflow question:</p>
  
  <p><a href="https://stackoverflow.com/questions/45358648/how-to-set-the-maximum-height-for-a-nestedscrollview-in-android">How to set the Maximum Height for a NestedScrollView in Android?</a></p>
  
  <p>However, when I include the custom class (<code>MaxHeightNestedScrollView</code>) in my <code>activity_main.xml</code> code layout, there is <strong>no scrollbar appearing</strong> when the TextView inside the <code>MaxHeightNestedScrollView</code> exceeds the defined max height. Below is the code for <code>MaxNestedScrollView</code>:</p>
  
  <pre><code>import android.content.Context;
  import android.util.AttributeSet;
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.core.widget.NestedScrollView;
  
  public class MaxHeightNestedScrollView extends NestedScrollView {
  
      private int maxHeight = -1;
  
      public MaxHeightNestedScrollView(@NonNull Context context) {
          super(context);
      }
  
      public MaxHeightNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
          super(context, attrs);
      }
  
      public MaxHeightNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
          super(context, attrs, defStyleAttr);
      }
  
      public int getMaxHeight() {
          return maxHeight;
      }
  
      public void setMaxHeight(int maxHeight) {
          this.maxHeight = maxHeight;
      }
  
      public void setMaxHeightDensity(int dps){
          this.maxHeight = (int)(dps * getContext().getResources().getDisplayMetrics().density);
      }
  
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          if (maxHeight &gt; 0) {
              heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
          }
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      }
  }
  </code></pre>
  
  <p>Below is the code for the <code>attrs.xml</code> file in the <code>values</code> folder:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;declare-styleable name="MaxHeightNestedScrollView"&gt;
          &lt;attr name="maxHeight" format="dimension" /&gt;
      &lt;/declare-styleable&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>Below is the code for <code>activity_main.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout 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:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
      &lt;ScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#ffffff"
          android:layout_weight="1"
          android:fillViewport="true"&gt;
  
          &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"&gt;
              &lt;TextView
                  android:id="@+id/textView"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
  \nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
  \nLorem ipsum\nLorem ipsum\nLorem ipsum" /&gt;
  
              &lt;com.example.testgradle.MaxHeightNestedScrollView
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  app:maxHeight="130dp"&gt;
                  &lt;TextView
                      android:id="@+id/textView2"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:textSize="20dp"
                      android:textColor="#000000"
                      android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
  \nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
  \nLorem ipsum\nLorem ipsum\nLorem ipsumLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
  \nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
  \nLorem ipsum\nLorem ipsum\nLorem ipsum" /&gt;
              &lt;/com.example.testgradle.MaxHeightNestedScrollView&gt;
  
              &lt;TextView
                  android:id="@+id/textView3"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
  \nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
  \nLorem ipsum\nLorem ipsum\nLorem ipsum" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/ScrollView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>