Aug 27, 2014

Android example using ScheduledExecutorService

java.util.concurrent.ScheduledExecutorService is an ExecutorService that can schedule commands to run after a given delay, or to execute periodically. This example show how to create a one-shot action using ScheduledExecutorService.



package com.example.androidscheduledexecutorservice;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

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.Toast;

public class MainActivity extends ActionBarActivity {
 
 Button buttonStart;
 
 ScheduledExecutorService scheduledExecutorService;

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

   @Override
   public void onClick(View v) {
    
    Toast.makeText(MainActivity.this, 
      "Start", 
      Toast.LENGTH_LONG).show();
    
    scheduledExecutorService = Executors.newScheduledThreadPool(1);
    
    scheduledExecutorService.schedule(new Runnable(){

     @Override
     public void run() {
      MainActivity.this.runOnUiThread(new Runnable(){

       @Override
       public void run() {
        Toast.makeText(MainActivity.this, 
          "Times-up", 
          Toast.LENGTH_LONG).show();
       }});
      
     }}, 
     5, 
     TimeUnit.SECONDS);
    
   }
   
        });
    }

}


<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" />

    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start" />

</LinearLayout>

Aug 25, 2014

android.widget.TextClock example

android.widget.TextClock, Added in API level 17, display the current date and/or time as a formatted string.

<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" />
 
    <TextClock
        android:id="@+id/clock"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textStyle="bold"
        android:textSize="50sp" />
    
</LinearLayout>

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/


Aug 7, 2014

Port iOS or Android apps to Windows and Windows Phone

Are you interested in porting your iOS or Android apps to Windows and Windows Phone? Moving to a new platform needn't be as difficult as you think, as this video will demonstrate. Learn about your different options, and the exciting tools available to help you. See you in the Windows app Store!


Check http://channel9.msdn.com/Blogs/One-Dev-Minute/Porting-your-app if you can't view the video here.

Aug 5, 2014

Display formatted date/time using String.format()

Examples of using String.format() to display formatted calendar:


package com.example.androidtime;

import java.util.Calendar;

import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView cal = (TextView)findViewById(R.id.cal);
        
        Calendar calendar = Calendar.getInstance(); 

        cal.setText(
         String.format("%td-%tm-%tY\n", calendar, calendar, calendar) +
         String.format("%ty/%tm/%td\n", calendar, calendar, calendar) +
         String.format("%tI:%tM:%tS %tp\n", calendar, calendar, calendar, calendar) +
         "\n" +
         String.format("%tD\n", calendar) +
         String.format("%tc\n", calendar) +
         String.format("%tF\n", calendar) +
         String.format("%tr\n", calendar)
        );

    }

}

The layout file is same as last post.

Aug 4, 2014

Display date/time in various format with SimpleDateFormat

Examples of using java.text.SimpleDateFormat:


package com.example.androidtime;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView cal = (TextView)findViewById(R.id.cal);
        
        Calendar calendar = Calendar.getInstance(); 
        SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat format2 = new SimpleDateFormat("yy/MM/dd");
        SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat format4 = new SimpleDateFormat("yyyy MMM dd (EEE)");
        SimpleDateFormat format5 = new SimpleDateFormat("hh:mm a");
        SimpleDateFormat format6 = new SimpleDateFormat("HH:mm");

        cal.setText(
         format1.format(calendar.getTime()) + "\n" +
         format2.format(calendar.getTime()) + "\n" +
         format3.format(calendar.getTime()) + "\n" +
         format4.format(calendar.getTime()) + "\n" +
         format5.format(calendar.getTime()) + "\n" +
         format6.format(calendar.getTime())
         );

    }

}

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidtime.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />
    <TextView
        android:id="@+id/cal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="20sp" />

</LinearLayout>

Related: Display formatted date/time using String.format().

Get time of system

Example to get time (in millisecond and nanosecond) from System, and get instance of Calendar to get current time.


package com.example.androidtime;

import java.util.Calendar;

import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        TextView tim = (TextView)findViewById(R.id.tim);
        TextView cal = (TextView)findViewById(R.id.cal);
        
        tim.setText(
         "currentTimeMillis: " + System.currentTimeMillis() + "\n" +
         "nanoTime" + System.nanoTime() + "\n"
         );
        
        Calendar calendar = Calendar.getInstance(); 
        cal.setText(
         "YEAR: " + calendar.get(Calendar.YEAR) + "\n" +
         "MONTH: " + calendar.get(Calendar.MONTH) + "\n" +
         "DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH) + "\n" +
         "HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY) + "\n" +
         "MINUTE: " + calendar.get(Calendar.MINUTE) + "\n" +
         "SECOND: " + calendar.get(Calendar.SECOND) + "\n"
         );

    }

}

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidtime.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />
    <TextView
        android:id="@+id/tim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/cal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Infolinks In Text Ads