1 package com.android.tv.settings.widget; 2 /* 3 * Copyright (C) 2025 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 import android.content.Context; 19 import android.content.res.TypedArray; 20 import android.util.AttributeSet; 21 import android.widget.ScrollView; 22 23 import com.android.tv.settings.R; 24 25 /** 26 * A ScrollView that limits the maximum height that it can take. Can be used with wrap_content 27 */ 28 public final class MaxHeightScrollView extends ScrollView { 29 private static final int NO_MAX_HEIGHT = -1; 30 31 private final int maxHeight; 32 MaxHeightScrollView(final Context context, final AttributeSet attrs)33 public MaxHeightScrollView(final Context context, final AttributeSet attrs) { 34 super(context, attrs); 35 final TypedArray attr = 36 context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView, 0, 0); 37 maxHeight = 38 attr.getDimensionPixelSize( 39 R.styleable.MaxHeightScrollView_android_maxHeight, NO_MAX_HEIGHT); 40 attr.recycle(); 41 } 42 43 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)44 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 45 if (maxHeight != NO_MAX_HEIGHT) { 46 heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST); 47 } 48 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 49 } 50 }