[googleapps domain="docs" dir="present/embed" query="id=ddt3n4v_18679vs4thh&autoStart=true&loop=true&size=m" width="555" height="451" /]
Calendar Cubes
You has two cubes. Every day you have to arranges thes cubes so that the front faces show the current day of the month.
How would you name the numbers on these cubes?
Note the nos are denoted as 01 for 1 and so on
Apples and Oranges
There are 3 boxes. One is labeled "APPLES" another is labeled "ORANGES". The last one is labeled "APPLES AND ORANGES". You know that each is labeled incorrectly. You may ask me to pick 1 fruit from 1 box which you choose.
Based on that, how can you label the boxes correctly?
Why one has to use a Handler in Android?
Following is inferred from the documentation about a Handler
The handler is used to schedule a runnable on the message queue of the thread that created the handler. it is mostly used to schedule UI updates on the UI thread
The one think I could not understand why I can't use any thread to update UI.
I did some experiment for the same.
Here I tried to update a TextView after every 1 second for 50 seconds using a normal thread and it failed. I got an exception
package org.punegtug.sample;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class HandlerTest extends Activity {
private TextView textView = null;
private int index = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
textView.setText("index="+index);
}
/*
* (non-Javadoc)
*
* @see android.app.Activity#onPause()
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
/*
* (non-Javadoc)
*
* @see android.app.Activity#onResume()
*/
@Override
protected void onResume() {
Runnable runnable = new Runnable() {
@Override
public void run() {
index = 0;
while (index < 50) {
//Tried to access text from UI component textView
String text= textView.getText().toString();
//Directly tried to update UI in this thread
textView.setText("index="+index++);
try {
// do what you want to do before sleeping
Thread.currentThread().sleep(1000);// sleep for 1000 ms
// do what you want to do after sleeptig
} catch (InterruptedException ie) {
// If this thread was intrrupted by nother thread
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
But the following code with Handler worked
t android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class HandlerTest extends Activity {
private TextView textView = null;
private int index = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
textView.setText("index="+index);
}
/*
* (non-Javadoc)
*
* @see android.app.Activity#onPause()
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
/*
* (non-Javadoc)
*
* @see android.app.Activity#onResume()
*/
@Override
protected void onResume() {
final Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
textView.setText("index="+index++);
return false;
}
});
Runnable runnable = new Runnable() {
@Override
public void run() {
index = 0;
while (index < 50) {
//Tried to access text from UI component textView
String text= textView.getText().toString();
//Indirectly tried to update UI in this thread
handler.sendEmptyMessage(0);
try {
// do what you want to do before sleeping
Thread.currentThread().sleep(1000);// sleep for 1000 ms
// do what you want to do after sleeptig
} catch (InterruptedException ie) {
// If this thread was intrrupted by nother thread
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
So I could, one can not update a UI component in any other thread than UI thread. However, getting values from existing UI components in any thread (e.g textView.getText()) works fine.
Hence we need to use Handler.
Soon, I will send a post about Looper.
Google Visualization API and Code Playground Discussion
Let's talk on Google Visualization API this saturday (20th of June 2009). I will also introduce you to Google Code Playground, an excellent tool to try the Google's javascript based APIs
With Google Visualization APIs you can do the following thing
1. Pull data from datasources like Google Spreadsheet or your own PHP or Java Servlet DataSource
2. Pull Data in SQL fashion with Select Queries and Where clauses
3. Render the data in
a. Data Table
b. Various Charts and Graphs
c. Any third party visualization libraries
With Google Code Playground any one can learn in hours Google's javascript based APIs like
1. Google Maps
2. Google Search
3. Google Translation
etc
Come and join us this saturday at
Venue
Dnyanvatsal Commercial Complex, Floor 5
Survey No. 23, Plot No. 189,
Karve Nagar,
Pune, India 411052
Contact no. - 9923085006
See on Google Map
http://www.synerzip.com
Between
4:00 p.m to 6:00 p.m, Saturday, 20th June 2009