1 /* 2 * Copyright (C) 2006 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; 18 19 20 import android.content.Context; 21 import android.util.AttributeSet; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.view.accessibility.AccessibilityEvent; 25 import android.view.accessibility.AccessibilityNodeInfo; 26 27 /** 28 * Specialized {@link android.widget.ViewSwitcher} that contains 29 * only children of type {@link android.widget.TextView}. 30 * 31 * A TextSwitcher is useful to animate a label on screen. Whenever 32 * {@link #setText(CharSequence)} is called, TextSwitcher animates the current text 33 * out and animates the new text in. 34 */ 35 public class TextSwitcher extends ViewSwitcher { 36 /** 37 * Creates a new empty TextSwitcher. 38 * 39 * @param context the application's environment 40 */ TextSwitcher(Context context)41 public TextSwitcher(Context context) { 42 super(context); 43 } 44 45 /** 46 * Creates a new empty TextSwitcher for the given context and with the 47 * specified set attributes. 48 * 49 * @param context the application environment 50 * @param attrs a collection of attributes 51 */ TextSwitcher(Context context, AttributeSet attrs)52 public TextSwitcher(Context context, AttributeSet attrs) { 53 super(context, attrs); 54 } 55 56 /** 57 * {@inheritDoc} 58 * 59 * @throws IllegalArgumentException if child is not an instance of 60 * {@link android.widget.TextView} 61 */ 62 @Override addView(View child, int index, ViewGroup.LayoutParams params)63 public void addView(View child, int index, ViewGroup.LayoutParams params) { 64 if (!(child instanceof TextView)) { 65 throw new IllegalArgumentException( 66 "TextSwitcher children must be instances of TextView"); 67 } 68 69 super.addView(child, index, params); 70 } 71 72 /** 73 * Sets the text of the next view and switches to the next view. This can 74 * be used to animate the old text out and animate the next text in. 75 * 76 * @param text the new text to display 77 */ setText(CharSequence text)78 public void setText(CharSequence text) { 79 final TextView t = (TextView) getNextView(); 80 t.setText(text); 81 showNext(); 82 } 83 84 /** 85 * Sets the text of the text view that is currently showing. This does 86 * not perform the animations. 87 * 88 * @param text the new text to display 89 */ setCurrentText(CharSequence text)90 public void setCurrentText(CharSequence text) { 91 ((TextView)getCurrentView()).setText(text); 92 } 93 94 @Override onInitializeAccessibilityEvent(AccessibilityEvent event)95 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 96 super.onInitializeAccessibilityEvent(event); 97 event.setClassName(TextSwitcher.class.getName()); 98 } 99 100 @Override onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)101 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 102 super.onInitializeAccessibilityNodeInfo(info); 103 info.setClassName(TextSwitcher.class.getName()); 104 } 105 } 106