The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 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 | */ |
| 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 |
| 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 | */ |
| 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 | */ |
| 88 | public void setCurrentText(CharSequence text) { |
| 89 | ((TextView)getCurrentView()).setText(text); |
| 90 | } |
| 91 | } |