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 com.example.android.apis.view;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.view.ViewGroup;
22 import android.widget.LinearLayout;
23 import android.widget.ScrollView;
24 
25 /**
26  * Demonstrates how a well behaved view with internal selection
27  * ({@link InternalSelectionView}) can cause its parent {@link android.widget.ScrollView}
28  * to scroll to keep the internally interesting rectangle on the screen.
29  *
30  * {@link InternalSelectionView} achieves this by calling {@link android.view.View#requestRectangleOnScreen}
31  * each time its internal selection changes.
32  *
33  * {@link android.widget.ScrollView}, in turn, implements {@link android.view.View#requestRectangleOnScreen}
34  * thereby acheiving the result.  Note that {@link android.widget.ListView} also implements the
35  * method, so views that call {@link android.view.View#requestRectangleOnScreen} that are embedded
36  * within either {@link android.widget.ScrollView}s or {@link android.widget.ListView}s can
37  * expect to keep their internal interesting rectangle visible.
38  */
39 public class InternalSelectionScroll extends Activity {
40 
41     @Override
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44 
45         ScrollView sv = new ScrollView(this);
46         ViewGroup.LayoutParams svLp = new ScrollView.LayoutParams(
47                 ViewGroup.LayoutParams.MATCH_PARENT,
48                 ViewGroup.LayoutParams.WRAP_CONTENT);
49 
50         LinearLayout ll = new LinearLayout(this);
51         ll.setLayoutParams(svLp);
52         sv.addView(ll);
53 
54         InternalSelectionView isv = new InternalSelectionView(this, 10);
55         int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
56         LinearLayout.LayoutParams llLp = new LinearLayout.LayoutParams(
57                 ViewGroup.LayoutParams.MATCH_PARENT,
58                 2 * screenHeight);  // 2x screen height to ensure scrolling
59         isv.setLayoutParams(llLp);
60         ll.addView(isv);
61 
62         setContentView(sv);
63     }
64 }
65