1 /*
2  * Copyright (C) 2007 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 android.widget.scroll;
18 
19 import com.android.frameworks.coretests.R;
20 
21 import android.app.Activity;
22 import android.os.Bundle;
23 import android.widget.LinearLayout;
24 import android.widget.ScrollView;
25 import android.widget.TextView;
26 import android.widget.Button;
27 
28 
29 /**
30  * Basic scroll view example
31  */
32 public class ScrollViewButtonsAndLabels extends Activity {
33 
34     private ScrollView mScrollView;
35     private LinearLayout mLinearLayout;
36 
37     private int mNumGroups = 10;
38 
39 
getScrollView()40     public ScrollView getScrollView() {
41         return mScrollView;
42     }
43 
getLinearLayout()44     public LinearLayout getLinearLayout() {
45         return mLinearLayout;
46     }
47 
getNumButtons()48     public int getNumButtons() {
49         return mNumGroups;
50     }
51 
getButton(int groupNum)52     public Button getButton(int groupNum) {
53         if (groupNum > mNumGroups) {
54             throw new IllegalArgumentException("groupNum > " + mNumGroups);
55         }
56         return (Button) mLinearLayout.getChildAt(2*groupNum);
57     }
58 
59     @Override
onCreate(Bundle icicle)60     protected void onCreate(Bundle icicle) {
61         super.onCreate(icicle);
62         setContentView(R.layout.scrollview_linear_layout);
63 
64 
65         // estimated ratio to get enough buttons so a couple are off screen
66         int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
67         mNumGroups = screenHeight / 30;
68 
69         mScrollView = findViewById(R.id.scrollView);
70         mLinearLayout = findViewById(R.id.layout);
71 
72         LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
73                 LinearLayout.LayoutParams.MATCH_PARENT,
74                 LinearLayout.LayoutParams.WRAP_CONTENT
75         );
76 
77         for (int i = 0; i < mNumGroups; i++) {
78             // want button to be first and last
79             if (i > 0) {
80                 TextView textView = new TextView(this);
81                 textView.setText("Text View " + i);
82                 mLinearLayout.addView(textView, p);
83             }
84 
85             Button button = new Button(this);
86             button.setText("Button " + (i + 1));
87             mLinearLayout.addView(button, p);
88         }
89     }
90 
91 }
92