Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
TextSwitcher / src / com.example.android.textswitcher /

MainActivity.java

       
        1
       
       
        /*
       
       
        2
       
       
        * Copyright 2013 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 com.example.android.textswitcher;
       
       
        18
       
       
       
       
        19
       
       
        import android.app.Activity;
       
       
        20
       
       
        import android.os.Bundle;
       
       
        21
       
       
        import android.view.Gravity;
       
       
        22
       
       
        import android.view.View;
       
       
        23
       
       
        import android.view.animation.Animation;
       
       
        24
       
       
        import android.view.animation.AnimationUtils;
       
       
        25
       
       
        import android.widget.Button;
       
       
        26
       
       
        import android.widget.TextSwitcher;
       
       
        27
       
       
        import android.widget.TextView;
       
       
        28
       
       
        import android.widget.ViewSwitcher.ViewFactory;
       
       
        29
       
       
       
       
        30
       
       
        /**
       
       
        31
       
       
        * This sample shows the use of the {@link android.widget.TextSwitcher} View with animations. A
       
       
        32
       
       
        * {@link android.widget.TextSwitcher} is a special type of {@link android.widget.ViewSwitcher} that animates
       
       
        33
       
       
        * the current text out and new text in when
       
       
        34
       
       
        * {@link android.widget.TextSwitcher#setText(CharSequence)} is called.
       
       
        35
       
       
        */
       
       
        36
       
       
        public class MainActivity extends Activity {
       
       
        37
       
       
        private TextSwitcher mSwitcher;
       
       
        38
       
       
        private int mCounter = 0;
       
       
        39
       
       
       
       
        40
       
       
        @Override
       
       
        41
       
       
        protected void onCreate(Bundle savedInstanceState) {
       
       
        42
       
       
        super.onCreate(savedInstanceState);
       
       
        43
       
       
        setContentView(R.layout.sample_main);
       
       
        44
       
       
       
       
        45
       
       
        // Get the TextSwitcher view from the layout
       
       
        46
       
       
        mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
       
       
        47
       
       
       
       
        49
       
       
        // Set the factory used to create TextViews to switch between.
       
       
        50
       
       
        mSwitcher.setFactory(mFactory);
       
       
        51
       
       
       
       
        52
       
       
        /*
       
       
        53
       
       
        * Set the in and out animations. Using the fade_in/out animations
       
       
        54
       
       
        * provided by the framework.
       
       
        55
       
       
        */
       
       
        56
       
       
        Animation in = AnimationUtils.loadAnimation(this,
       
       
        57
       
       
        android.R.anim.fade_in);
       
       
        58
       
       
        Animation out = AnimationUtils.loadAnimation(this,
       
       
        59
       
       
        android.R.anim.fade_out);
       
       
        60
       
       
        mSwitcher.setInAnimation(in);
       
       
        61
       
       
        mSwitcher.setOutAnimation(out);
       
       
        63
       
       
       
       
        64
       
       
        /*
       
       
        65
       
       
        * Setup the 'next' button. The counter is incremented when clicked and
       
       
        66
       
       
        * the new value is displayed in the TextSwitcher. The change of text is
       
       
        67
       
       
        * automatically animated using the in/out animations set above.
       
       
        68
       
       
        */
       
       
        69
       
       
        Button nextButton = (Button) findViewById(R.id.button);
       
       
        70
       
       
        nextButton.setOnClickListener(new View.OnClickListener() {
       
       
        71
       
       
       
       
        72
       
       
        @Override
       
       
        73
       
       
        public void onClick(View v) {
       
       
        74
       
       
        mCounter++;
       
       
        76
       
       
        mSwitcher.setText(String.valueOf(mCounter));
       
       
        78
       
       
        }
       
       
        79
       
       
        });
       
       
        80
       
       
       
       
        81
       
       
        // Set the initial text without an animation
       
       
        82
       
       
        mSwitcher.setCurrentText(String.valueOf(mCounter));
       
       
        83
       
       
       
       
        84
       
       
        }
       
       
        85
       
       
       
       
        87
       
       
        /**
       
       
        88
       
       
        * The {@link android.widget.ViewSwitcher.ViewFactory} used to create {@link android.widget.TextView}s that the
       
       
        89
       
       
        * {@link android.widget.TextSwitcher} will switch between.
       
       
        90
       
       
        */
       
       
        91
       
       
        private ViewFactory mFactory = new ViewFactory() {
       
       
        92
       
       
       
       
        93
       
       
        @Override
       
       
        94
       
       
        public View makeView() {
       
       
        95
       
       
       
       
        96
       
       
        // Create a new TextView
       
       
        97
       
       
        TextView t = new TextView(MainActivity.this);
       
       
        98
       
       
        t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
       
       
        99
       
       
        t.setTextAppearance(MainActivity.this, android.R.style.TextAppearance_Large);
       
       
        100
       
       
        return t;
       
       
        101
       
       
        }
       
       
        102
       
       
        };
       
       
        104
       
       
        }