title: Adding a view count counter and making app more efficient
body: <p>So I am currently designing an android app that comprises of two activities, MainActivity and viewVideo. I have a button on the Main Activity and when the button in pressed I create an intent to play a random video from a youtube playlist that I have created and once the video finishes playing, the app returns back to the Main Activity.</p>
  
  <p>I want to have a stylish swipeable counter on the Main Activity that says the total views of all the videos in the playlist and how many times the user has viewed a video from the playlist. I want to make it so when the user quits the app the user views are stored somewhere and displayed back on the counter when the app is relaunched. The videos also take a long time to load is this because my code isn't efficient?</p>
  
  <p>Here is my code for the MainActivity</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  Button btnPlay;
  TextView showValue;
  public int counter = 0;
  private static final String TAG = "MainActivity";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      btnPlay = (Button) findViewById(R.id.btnPlay);
      showValue = (TextView) findViewById(R.id.counterValue);
  
      btnPlay.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
              int min = 0;
              int max = 9;
              Random r = new Random();
              int num = r.nextInt(max - min + 1) + min;
  
              counter++;
              showValue.setText(Integer.toString(counter));
  
              Intent intent = new Intent(MainActivity.this, viewVideo.class);
              startActivity(intent);
          }
      });
  }
  }
  </code></pre>
  
  <p>My code for the viewVideo Activity</p>
  
  <pre><code>public class viewVideo extends YouTubeBaseActivity {
  YouTubePlayerView mYouTubePlayerView;
  YouTubePlayer.OnInitializedListener mOnInitializedListener;
  YouTubePlayer.OnFullscreenListener mOnFullscreenListener;
  int min = 0;
  int max = 9;
  Random r = new Random();
  int num = r.nextInt(max - min + 1) + min;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_view_video);
      mYouTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubePlay);
  
      mOnInitializedListener = new YouTubePlayer.OnInitializedListener() {
          @Override
          public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
              youTubePlayer.loadPlaylist("PLOrsSKa3KlU9Mdai4JsiKrfYF0A9DvoLh",num ,0);
              youTubePlayer.setFullscreen(true);
              youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
              youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);
          }
  
          @Override
          public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
              Toast.makeText(viewVideo.this,"Failured to Initialize!", Toast.LENGTH_LONG).show();
          }
      };
      mYouTubePlayerView.initialize(YouTubeConfig.getApiKey(), mOnInitializedListener);
  }
  
  private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
      @Override
      public void onLoading() {
      }
      @Override
      public void onLoaded(String s) {
      }
      @Override
      public void onAdStarted() {
      }
      @Override
      public void onVideoStarted() {
      }
      @Override
      public void onVideoEnded() {
          finish();
          System.exit(0);
      }
      @Override
      public void onError(YouTubePlayer.ErrorReason errorReason) {
      }
  };
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ROlNZ.jpg" rel="nofollow noreferrer">Image of MainActiviy UI (I need the counter on top of the play ad btn)</a></p>