blob: 018ad106d08c3e9d85db028851a004f766d49885 [file] [log] [blame]
Annie Chine918fd22016-03-09 11:07:54 -08001/*
2 * Copyright (C) 2016 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
17package com.android.calculator2;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.HorizontalScrollView;
24
Justin Klaassen6969b022016-08-23 11:13:08 -070025import static android.view.View.MeasureSpec.UNSPECIFIED;
26import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
27import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
28
Annie Chine918fd22016-03-09 11:07:54 -080029public class CalculatorScrollView extends HorizontalScrollView {
30
31 public CalculatorScrollView(Context context) {
32 this(context, null /* attrs */);
33 }
34
35 public CalculatorScrollView(Context context, AttributeSet attrs) {
36 this(context, attrs, 0 /* defStyleAttr */);
37 }
38
39 public CalculatorScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
40 super(context, attrs, defStyleAttr);
41 }
42
Justin Klaassen6969b022016-08-23 11:13:08 -070043 private static int getChildMeasureSpecCompat(int spec, int padding, int childDimension) {
44 if (MeasureSpec.getMode(spec) == UNSPECIFIED
45 && (childDimension == MATCH_PARENT || childDimension == WRAP_CONTENT)) {
46 final int size = Math.max(0, MeasureSpec.getSize(spec) - padding);
47 return MeasureSpec.makeMeasureSpec(size, UNSPECIFIED);
48 }
49 return ViewGroup.getChildMeasureSpec(spec, padding, childDimension);
50 }
51
Annie Chine918fd22016-03-09 11:07:54 -080052 @Override
53 protected void measureChild(View child, int parentWidthMeasureSpec,
54 int parentHeightMeasureSpec) {
55 // Allow child to be as wide as they want.
56 parentWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
Justin Klaassen6969b022016-08-23 11:13:08 -070057 MeasureSpec.getSize(parentWidthMeasureSpec), UNSPECIFIED);
Annie Chine918fd22016-03-09 11:07:54 -080058
59 final ViewGroup.LayoutParams lp = child.getLayoutParams();
Justin Klaassen6969b022016-08-23 11:13:08 -070060 final int childWidthMeasureSpec = getChildMeasureSpecCompat(parentWidthMeasureSpec,
Annie Chine918fd22016-03-09 11:07:54 -080061 0 /* padding */, lp.width);
Justin Klaassen6969b022016-08-23 11:13:08 -070062 final int childHeightMeasureSpec = getChildMeasureSpecCompat(parentHeightMeasureSpec,
Annie Chine918fd22016-03-09 11:07:54 -080063 getPaddingTop() + getPaddingBottom(), lp.height);
64
65 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
66 }
67
68 @Override
69 protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
70 int parentHeightMeasureSpec, int heightUsed) {
71 // Allow child to be as wide as they want.
72 parentWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
Justin Klaassen6969b022016-08-23 11:13:08 -070073 MeasureSpec.getSize(parentWidthMeasureSpec), UNSPECIFIED);
Annie Chine918fd22016-03-09 11:07:54 -080074
75 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
Justin Klaassen6969b022016-08-23 11:13:08 -070076 final int childWidthMeasureSpec = getChildMeasureSpecCompat(parentWidthMeasureSpec,
Annie Chine918fd22016-03-09 11:07:54 -080077 lp.leftMargin + lp.rightMargin, lp.width);
Justin Klaassen6969b022016-08-23 11:13:08 -070078 final int childHeightMeasureSpec = getChildMeasureSpecCompat(parentHeightMeasureSpec,
Annie Chine918fd22016-03-09 11:07:54 -080079 getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin, lp.height);
80
81 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
82 }
83}