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
17package android.widget;
18
19
20import android.content.Context;
21import android.util.AttributeSet;
22import android.view.View;
23import android.view.ViewGroup;
24import android.view.accessibility.AccessibilityEvent;
25import 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 */
35public class TextSwitcher extends ViewSwitcher {
36    /**
37     * Creates a new empty TextSwitcher.
38     *
39     * @param context the application's environment
40     */
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     */
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
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     */
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     */
90    public void setCurrentText(CharSequence text) {
91        ((TextView)getCurrentView()).setText(text);
92    }
93
94    @Override
95    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
96        super.onInitializeAccessibilityEvent(event);
97        event.setClassName(TextSwitcher.class.getName());
98    }
99
100    @Override
101    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
102        super.onInitializeAccessibilityNodeInfo(info);
103        info.setClassName(TextSwitcher.class.getName());
104    }
105}
106