blob: bcf5650a16c4aaa0dffa7a294c5c2ea6349d53c3 [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
25public class CalculatorScrollView extends HorizontalScrollView {
26
27 public CalculatorScrollView(Context context) {
28 this(context, null /* attrs */);
29 }
30
31 public CalculatorScrollView(Context context, AttributeSet attrs) {
32 this(context, attrs, 0 /* defStyleAttr */);
33 }
34
35 public CalculatorScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
36 super(context, attrs, defStyleAttr);
37 }
38
39 @Override
40 protected void measureChild(View child, int parentWidthMeasureSpec,
41 int parentHeightMeasureSpec) {
42 // Allow child to be as wide as they want.
43 parentWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
44 MeasureSpec.getSize(parentWidthMeasureSpec), MeasureSpec.UNSPECIFIED);
45
46 final ViewGroup.LayoutParams lp = child.getLayoutParams();
47 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
48 0 /* padding */, lp.width);
49 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
50 getPaddingTop() + getPaddingBottom(), lp.height);
51
52 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
53 }
54
55 @Override
56 protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
57 int parentHeightMeasureSpec, int heightUsed) {
58 // Allow child to be as wide as they want.
59 parentWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
60 MeasureSpec.getSize(parentWidthMeasureSpec), MeasureSpec.UNSPECIFIED);
61
62 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
63 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
64 lp.leftMargin + lp.rightMargin, lp.width);
65 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
66 getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin, lp.height);
67
68 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
69 }
70}