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.focus; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.widget.LinearLayout; 22 import android.widget.Button; 23 import android.widget.TextView; 24 import android.view.Gravity; 25 import android.view.ViewGroup; 26 import android.content.Context; 27 28 /** 29 * Holds a few buttons of various sizes and horizontal placements in a 30 * vertical layout to excercise some core focus searching. 31 */ 32 public class VerticalFocusSearch extends Activity { 33 34 private LinearLayout mLayout; 35 36 private Button mTopWide; 37 private Button mMidSkinny1Left; 38 private Button mBottomWide; 39 40 private Button mMidSkinny2Right; 41 42 43 public LinearLayout getLayout() { 44 return mLayout; 45 } 46 47 public Button getTopWide() { 48 return mTopWide; 49 } 50 51 public Button getMidSkinny1Left() { 52 return mMidSkinny1Left; 53 } 54 55 public Button getMidSkinny2Right() { 56 return mMidSkinny2Right; 57 } 58 59 public Button getBottomWide() { 60 return mBottomWide; 61 } 62 63 @Override 64 protected void onCreate(Bundle icicle) { 65 super.onCreate(icicle); 66 67 mLayout = new LinearLayout(this); 68 mLayout.setOrientation(LinearLayout.VERTICAL); 69 mLayout.setHorizontalGravity(Gravity.START); 70 mLayout.setLayoutParams(new ViewGroup.LayoutParams( 71 ViewGroup.LayoutParams.MATCH_PARENT, 72 ViewGroup.LayoutParams.MATCH_PARENT)); 73 74 mTopWide = makeWide("top wide"); 75 mLayout.addView(mTopWide); 76 77 mMidSkinny1Left = addSkinny(mLayout, "mid skinny 1(L)", false); 78 79 mMidSkinny2Right = addSkinny(mLayout, "mid skinny 2(R)", true); 80 81 mBottomWide = makeWide("bottom wide"); 82 mLayout.addView(mBottomWide); 83 84 setContentView(mLayout); 85 } 86 87 // just to get toString non-sucky 88 private static class MyButton extends Button { 89 90 public MyButton(Context context) { 91 super(context); 92 } 93 94 95 @Override 96 public String toString() { 97 return getText().toString(); 98 } 99 } 100 101 private Button makeWide(String label) { 102 Button button = new MyButton(this); 103 button.setText(label); 104 button.setLayoutParams(new LinearLayout.LayoutParams( 105 ViewGroup.LayoutParams.MATCH_PARENT, 106 ViewGroup.LayoutParams.WRAP_CONTENT)); 107 return button; 108 } 109 110 /** 111 * Add a skinny button that takes up just less than half of the screen 112 * horizontally. 113 * @param root The layout to add the button to. 114 * @param label The label of the button. 115 * @param atRight Which side to put the button on. 116 * @return The newly created button. 117 */ 118 private Button addSkinny(LinearLayout root, String label, boolean atRight) { 119 Button button = new MyButton(this); 120 button.setText(label); 121 button.setLayoutParams(new LinearLayout.LayoutParams( 122 0, // width 123 ViewGroup.LayoutParams.WRAP_CONTENT, 124 480)); 125 126 TextView filler = new TextView(this); 127 filler.setText("filler"); 128 filler.setLayoutParams(new LinearLayout.LayoutParams( 129 0, // width 130 ViewGroup.LayoutParams.WRAP_CONTENT, 131 520)); 132 133 LinearLayout ll = new LinearLayout(this); 134 ll.setOrientation(LinearLayout.HORIZONTAL); 135 ll.setLayoutParams(new LinearLayout.LayoutParams( 136 ViewGroup.LayoutParams.MATCH_PARENT, 137 ViewGroup.LayoutParams.WRAP_CONTENT)); 138 139 if (atRight) { 140 ll.addView(filler); 141 ll.addView(button); 142 root.addView(ll); 143 } else { 144 ll.addView(button); 145 ll.addView(filler); 146 root.addView(ll); 147 } 148 return button; 149 } 150 151 } 152