Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends FragmentActivity {
- private TextSwitcher mSwitcher, mSwitcher1;
- private int mCounter = 0;
- String textToShow[] = {
- "Main HeadLine", "Your Message", "New In Technology", "New Articles", "Business News", "What IS New"
- };
- String textToShow1[] = {
- "Main HeadLine", "Your Message", "New In Technology", "New Articles", "Business News", "What IS New"
- };
- int messageCount = textToShow.length;
- // to keep current Index of text
- int currentIndex = -1;
- Timer timer = new Timer();
- public class MyTimerTask extends TimerTask {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- updateTextView();
- }
- });
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.example_layout);
- mSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
- mSwitcher1 = (TextSwitcher) findViewById(R.id.textSwitcher01);
- // BEGIN_INCLUDE(setup)
- // Set the factory used to create TextViews to switch between.
- mSwitcher.setFactory(mFactory);
- mSwitcher1.setFactory(mFactory);
- Animation in = AnimationUtils.loadAnimation(this,
- android.R.anim.fade_in);
- Animation out = AnimationUtils.loadAnimation(this,
- android.R.anim.fade_out);
- mSwitcher.setInAnimation(in);
- mSwitcher1.setInAnimation(in);
- mSwitcher.setOutAnimation(out);
- mSwitcher1.setOutAnimation(out);
- }
- @Override
- protected void onStart() {
- super.onStart();
- timer.schedule(new MyTimerTask(), 0, 1000);
- }
- private void updateTextView() {
- int maxIndex = textToShow.length;
- Random random = new Random();
- int generatedIndex = random.nextInt(maxIndex);
- mSwitcher.setText(textToShow[generatedIndex]);
- mSwitcher1.setText(textToShow1[generatedIndex]);
- }
- private ViewFactory mFactory = new ViewFactory() {
- @Override
- public View makeView() {
- // Create a new TextView
- TextView t = new TextView(MainActivity.this);
- t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
- t.setTextAppearance(MainActivity.this, android.R.style.TextAppearance_Large);
- return t;
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment