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