Aug 23, 2014

Android example to execute threads with ExecutorService

ExecutorService is an Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks. This example implement 5 threads to update ProgressBars in background thread. The threads executed by a ExecutorService, with thread pool of 2. Such that only 2 thread is running at any time.


package com.example.androidcountdownprogressbar;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends ActionBarActivity {
 
 Button buttonStart;
 ProgressBar progressBar1, progressBar2,
  progressBar3, progressBar4, progressBar5;
 
 CountThread countThread1, countThread2,
  countThread3, countThread4, countThread5;
 
 ExecutorService executorService = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        buttonStart = (Button)findViewById(R.id.start);
        progressBar1 = (ProgressBar)findViewById(R.id.progressbar1);
        progressBar2 = (ProgressBar)findViewById(R.id.progressbar2);
        progressBar3 = (ProgressBar)findViewById(R.id.progressbar3);
        progressBar4 = (ProgressBar)findViewById(R.id.progressbar4);
        progressBar5 = (ProgressBar)findViewById(R.id.progressbar5);

        buttonStart.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    countThread1 = new CountThread(progressBar1);
    countThread2 = new CountThread(progressBar2);
    countThread3 = new CountThread(progressBar3);
    countThread4 = new CountThread(progressBar4);
    countThread5 = new CountThread(progressBar5);
    
    executorService = Executors.newFixedThreadPool(2);
    executorService.execute(countThread1);
    executorService.execute(countThread2);
    executorService.execute(countThread3);
    executorService.execute(countThread4);
    executorService.execute(countThread5);
   }});
        
    }
    
    public class CountThread extends Thread{
     
     ProgressBar progressBar;
     final int MAX_PROGRESS = 10;
     int progress;
     
     CountThread(ProgressBar progressBar){
      this.progressBar = progressBar;
      progress = MAX_PROGRESS;
     }

  @Override
  public void run() {
   
   for(int i=0; i<MAX_PROGRESS; i++){
    progress--;
    MainActivity.this.runOnUiThread(new Runnable(){

     @Override
     public void run() {
      progressBar.setProgress(progress);
     }});
    
    try {
     Thread.sleep(1000);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   
  }
    }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidcountdownprogressbar.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <ProgressBar
        android:id="@+id/progressbar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:progress="0" />
    <ProgressBar
        android:id="@+id/progressbar2"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:progress="0" />
    <ProgressBar
        android:id="@+id/progressbar3"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:progress="0" />
    <ProgressBar
        android:id="@+id/progressbar4"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:progress="0" />
    <ProgressBar
        android:id="@+id/progressbar5"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:progress="0" />
    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start" />

</LinearLayout>

Aug 16, 2014

CountDownTimer and ProgressBar

Example of using CountDownTimer to update ProgressBar.


package com.example.androidcountdownprogressbar;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
 
 Button buttonStart;
 ProgressBar progressBar;
 TextView textCounter;
 
 MyCountDownTimer myCountDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        buttonStart = (Button)findViewById(R.id.start);
        progressBar = (ProgressBar)findViewById(R.id.progressbar);
        textCounter = (TextView)findViewById(R.id.counter);
        
        buttonStart.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    progressBar.setProgress(100);
    myCountDownTimer =  new MyCountDownTimer(10000, 500);
    myCountDownTimer.start();
   }});
        
    }
    
    public class MyCountDownTimer extends CountDownTimer {

  public MyCountDownTimer(long millisInFuture, long countDownInterval) {
   super(millisInFuture, countDownInterval);
  }

  @Override
  public void onTick(long millisUntilFinished) {
   textCounter.setText(String.valueOf(millisUntilFinished));
   int progress = (int) (millisUntilFinished/100);
   progressBar.setProgress(progress);
  }

  @Override
  public void onFinish() {
   textCounter.setText("Finished");
   progressBar.setProgress(0);
  }
     
    }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidcountdownprogressbar.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <ProgressBar
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="100" />
    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start" />
    <TextView
        android:id="@+id/counter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textStyle="bold"
        android:textSize="50sp"
        android:gravity="center" />

</LinearLayout>

Aug 15, 2014

Android L for game development

Check out what's new for games in the upcoming Android L release. Learn about new features in the OpenGL graphics and OpenSL Audio APIs and get a small taste of the new Google Play Game Services released after Google I/O.

Aug 13, 2014

Mobile Developer's Guide To The Galaxy, 14th Edition

Mobile Developer's Guide To The Galaxy

The free, community driven handbook about mobile technologies: Over 20 experts from all over the world are sharing their know-how on topics like mobile app design, platform-specific aspects of app development, cross-platform development, mobile analytics, accessibility, monetization, app stores, LBS, NFC, app testing and a lot more.

The book is popular among developers wanting to deepen their knowledge and decision-makers planning to enter the mobile business alike. The content gets updates and extended continuously, 13 editions have been published since 2009 and 50,000 hardcopies have been distributed.



Aug 9, 2014

Samsung Developers Conference Nov 11-13th

Samsung Developers Conference will be hold on Moscone West, San Francisco, November 11-13th, 2014. Signup to be notified: http://samsungdevcon.com/


Infolinks In Text Ads