1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.dialer.widget; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.support.annotation.Nullable; 22 import android.util.AttributeSet; 23 import android.widget.ScrollView; 24 25 /** {@link ScrollView} with a max height attribute. */ 26 public class MaxHeightScrollView extends ScrollView { 27 28 private final int maxHeight; 29 MaxHeightScrollView(Context context, @Nullable AttributeSet attrs)30 public MaxHeightScrollView(Context context, @Nullable AttributeSet attrs) { 31 this(context, attrs, 0); 32 } 33 MaxHeightScrollView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)34 public MaxHeightScrollView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 35 this(context, attrs, defStyleAttr, 0); 36 } 37 MaxHeightScrollView( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)38 public MaxHeightScrollView( 39 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 40 super(context, attrs, defStyleAttr, defStyleRes); 41 if (isInEditMode()) { 42 maxHeight = -1; 43 return; 44 } 45 46 TypedArray array = 47 context.getTheme().obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView, 0, 0); 48 try { 49 maxHeight = array.getDimensionPixelSize(R.styleable.MaxHeightScrollView_maxHeight, -1); 50 } finally { 51 array.recycle(); 52 } 53 } 54 55 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)56 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 57 if (maxHeight < 0) { 58 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 59 } else { 60 super.onMeasure( 61 widthMeasureSpec, MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST)); 62 } 63 } 64 } 65