Scrolling TextView by Ticker



Robinhood team had written a library to drawing sparkline in Android applicatio

Example

This example demostrate about how to integrate Android Scrolling TextView by Ticker.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = "info.devexchanges.scrollingtextview.MainActivity">
   <com.robinhood.ticker.TickerView
      android:id = "@+id/ticker_usd"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_centerInParent = "true"
      android:layout_marginTop = "@dimen/activity_vertical_margin"
      android:background = "#243D99"
      android:padding = "@dimen/activity_horizontal_margin"
      app:ticker_textColor = "@android:color/white"
      app:ticker_textSize = "20sp" />
   <com.robinhood.ticker.TickerView
      android:id = "@+id/ticker_number"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_above = "@+id/ticker_usd"
      android:layout_centerInParent = "true"
      android:background = "@color/colorPrimary"
      android:padding = "@dimen/activity_horizontal_margin"
      app:ticker_textColor = "@android:color/white"
      app:ticker_textSize = "20sp" />
   <com.robinhood.ticker.TickerView
      android:id = "@+id/ticker_text"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_below = "@id/ticker_usd"
      android:layout_centerInParent = "true"
      android:layout_marginTop = "@dimen/activity_vertical_margin"
      android:background = "#0099ff"
      android:padding = "@dimen/activity_horizontal_margin"
      app:ticker_textColor = "@android:color/white"
      app:ticker_textSize = "20sp" />
</RelativeLayout>

Step 3 − Add the following code to src/MainActivity.java

package myapplication.example.com.myapplication;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.robinhood.ticker.TickerUtils;
import com.robinhood.ticker.TickerView;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
   private TickerView tickerView, tickerUSD, tickerText;
   private char[] alphabetlist;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      tickerView = (TickerView) findViewById(R.id.ticker_number);
      tickerUSD = (TickerView) findViewById(R.id.ticker_usd);
      tickerText = (TickerView) findViewById(R.id.ticker_text);
      
      alphabetlist = new char[53];
      alphabetlist[0] = TickerUtils.EMPTY_CHAR;
      for (int i = 0; i < 2; i++) {
         for (int j = 0; j < 26; j++) {
            // Add all lowercase characters first, then add the uppercase characters.
            alphabetlist[1 + i * 26 + j] = (char) ((i == 0) ? j + 97 : j + 65);
         } 
      } 
      tickerView.setCharacterList(TickerUtils.getDefaultNumberList());
      tickerUSD.setCharacterList(TickerUtils.getDefaultListForUSCurrency());
      tickerText.setCharacterList(alphabetlist);
      
      tickerView.setText("2000");
      tickerUSD.setText("$5,200");
      setRandomText();
      setRandomCurrency();
   } 
   public void setRandomText() {
      final Handler handler = new Handler();
      handler.postDelayed(new Runnable() {
         public void run() {
            runOnUiThread(new Runnable() {
               @Override
               public void run() {
                  Random r = new Random();
                  int low = 1000;
                  int high = 10000;
                  final int result = r.nextInt(high - low) + low;
                  tickerView.setText("" + result);
                  handler.postDelayed(this, 1500);
               } 
            });
         }
      }, 2000);
   } 
   
   public void setRandomCurrency() {
      final Handler handler = new Handler();
      handler.postDelayed(new Runnable() {
         public void run() {
            runOnUiThread(new Runnable() {
               @Override
               public void run() {
                  Random r = new Random();
                  int low = 1000;
                  int high = 10000;
                  final int result = r.nextInt(high - low) + low;
                  NumberFormat formatter = new DecimalFormat("#,###,###");
                  String usdString = formatter.format(Integer.parseInt(
                     String.valueOf(result)));
                     
                  tickerUSD.setText("$" + usdString);
                  tickerText.setText(generateChars(r, alphabetlist, 6));
                  handler.postDelayed(this, 1500);
               } 
            });
         }
      }, 2000);
   }
   
   private String generateChars(Random random, char[] list, int numDigits) {
      final char[] result = new char[numDigits];
      for (int i = 0; i < numDigits; i++) {
         result[i] = list[random.nextInt(list.length)];
      } 
      return new String(result);
   }
}

Step 4 − Add the following code to build.gradle

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:24.2.1'
   compile 'com.robinhood.ticker:ticker:1.0.0'
}

Step 5 − No need to changemanifest.xml

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Eclipse Run Icon
Advertisements