Home | History | Annotate | Download | only in view
      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 
     24 /**
     25  * {@link android.view.View#requestFocus(int, android.graphics.Rect)}
     26  * and
     27  * {@link android.view.View#onFocusChanged(boolean, int, android.graphics.Rect)}
     28  * work together to give a newly focused item a hint about the most interesting
     29  * rectangle of the previously focused view.  The view taking focus can use this
     30  * to set an internal selection more appropriate using this rect.
     31  *
     32  * This Activity excercises that behavior using three adjacent {@link InternalSelectionView}
     33  * that report interesting rects when giving up focus, and use interesting rects
     34  * when taking focus to best select the internal row to show as selected.
     35  *
     36  * Were {@link InternalSelectionView} not to override {@link android.view.View#getFocusedRect}, or
     37  * {@link android.view.View#onFocusChanged(boolean, int, android.graphics.Rect)}, the focus would
     38  * jump to some default internal selection (the top) and not allow for the smooth handoff.
     39  */
     40 public class InternalSelectionFocus extends Activity {
     41 
     42     @Override
     43     protected void onCreate(Bundle savedInstanceState) {
     44         super.onCreate(savedInstanceState);
     45 
     46         final LinearLayout layout = new LinearLayout(this);
     47         layout.setOrientation(LinearLayout.HORIZONTAL);
     48         layout.setLayoutParams(new ViewGroup.LayoutParams(
     49                 ViewGroup.LayoutParams.MATCH_PARENT,
     50                 ViewGroup.LayoutParams.MATCH_PARENT));
     51 
     52         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,
     53                 ViewGroup.LayoutParams.MATCH_PARENT, 1);
     54 
     55         final InternalSelectionView leftColumn = new InternalSelectionView(this, 5, "left column");
     56         leftColumn.setLayoutParams(params);
     57         leftColumn.setPadding(10, 10, 10, 10);
     58         layout.addView(leftColumn);
     59 
     60         final InternalSelectionView middleColumn = new InternalSelectionView(this, 5, "middle column");
     61         middleColumn.setLayoutParams(params);
     62         middleColumn.setPadding(10, 10, 10, 10);
     63         layout.addView(middleColumn);
     64 
     65         final InternalSelectionView rightColumn = new InternalSelectionView(this, 5, "right column");
     66         rightColumn.setLayoutParams(params);
     67         rightColumn.setPadding(10, 10, 10, 10);
     68         layout.addView(rightColumn);
     69 
     70         setContentView(layout);
     71     }
     72 
     73 
     74 }
     75