Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Sunday, July 5, 2009

SimpleAdapter

The corresponding SimpleAdapter maps an element from list of objects to this view.

SimpleAdapter(
Context context,
List<? extends Map<String, ?>> data,
int resource,
String[] from,
int[] to)

SimpleAdapter notes = new SimpleAdapter(
this,
list,
R.layout.main_item_two_line_row,
new String[] { "line1","line2" },
new int[] { R.id.text1, R.id.text2 } );

Now this is not a trivial line of code. What it says is:

  • "list" is a reference to an object implementing the List interface. Each element in this list is an object implementing the Map interface. In our implementation example, the list is ArrayList and the elements in this list are HashMaps.
  • The layout describing one row in the main list is defined in layout/main_item_two_line_row.xml file.
  • In each individual HashMap, there are two key-value pairs. The keys are "line1" and "line2", the corresponding values are arbitrary objects whose toString() method yields the value displayed in the list row. In our case, the values are Strings.
  • Values stored with key "line1" will be displayed in the TextView whose id is "text1". Similarly, "line2" Map key is associated with "text2" TextView.


http://mylifewithandroid.blogspot.com/2008/03/my-first-meeting-with-simpleadapter.html

Thursday, June 18, 2009

Using threads and ProgressDialog

This is a simple tutorial to show how to create a thread to do some work while displaying an indeterminate ProgressDialog. Click here to download the full source.

We'll calculate Pi to 800 digits while displaying the ProgressDialog. For the sake of this example I copied the "Pi" class from this site.

We start with a new Android project, only thing I needed to change was to give that TextView an id in main.xml so that I could update it in the Activity.

Because this Activity is so small I'll show you the whole thing and then discuss it at the end:


package com.helloandroid.android.progressdialogexample;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;

public class ProgressDialogExample extends Activity implements Runnable {

private static String TAG = "ProgressDialogExample";
private String pi_string;
private TextView tv;
private ProgressDialog pd;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

tv = (TextView) this.findViewById(R.id.main);
tv.setText("Press any key to start calculation");
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

pd = ProgressDialog.show(this, "Working..", "Calculating Pi", true, false);

Log.i(TAG, "Thread.currentThread().getName(): " + Thread.currentThread().getName());

// create a new thread using the current class as the runnable object
// new ProgressDialogExample() passed to Thread constructor
Thread thread = new Thread(this);
thread.start();

Log.i(TAG, "thread.getName(): " + thread.getName());

return super.onKeyDown(keyCode, event);
}

public void run() {
Log.i(TAG, "Thread.currentThread().getName(): " + Thread.currentThread().getName());

pi_string = Pi.computePi(800).toString();

Log.i(TAG, "run()");

handler.sendEmptyMessage(0);
}

private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
pd.dismiss();
tv.setText(pi_string);
}
};

}


So we see that this Activity implements Runnable. This will allow us to create a run() function to create a thread.

In the onCreate() function on line 18 we find and initialize our TextView and set the text telling the user to press any key to start the computation.

When the user presses a key it will bring us to the onKeyDown() function on line 27. Here we create the ProgressDialog using the static ProgressDialog.show() function, and as a result the pd variable is initialized. We also create a new Thread using the current class as the Runnable object. When we run thread.start() a new thread will spawn and start executing the run() function.

In the run() function we calculate pi and save it to the String pi_string. Then we send an empty message to our Handler object on line 40.

Why use a Handler?
We must use a Handler object because we cannot update most UI objects while in a separate thread. When we send a message to the Handler it will get saved into a queue and get executed by the UI thread as soon as possible.

When our Handler receives the message we can dismiss our ProgressDialog and update the TextView with the value of pi we calculated. It's that easy!

http://www.helloandroid.com/node/243

Wednesday, May 20, 2009

Activity state changes



State change method handlers available in an Activity


package com.paad.myapplication;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity {

// Called at the start of the full lifetime.
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Initialize activity.
}

// Called after onCreate has finished, use to restore UI state
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
}

// Called before subsequent visible lifetimes
// for an activity process.
@Override
public void onRestart(){
super.onRestart();
// Load changes knowing that the activity has already
// been visible within this process.
}

// Called at the start of the visible lifetime.
@Override
public void onStart(){
super.onStart();
// Apply any required UI change now that the Activity is visible.
}

// Called at the start of the active lifetime.
@Override
public void onResume(){
super.onResume();
// Resume any paused UI updates, threads, or processes required
// by the activity but suspended when it was inactive.
}

// Called to save UI state changes at the
// end of the active lifecycle.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
super.onSaveInstanceState(savedInstanceState);
}

// Called at the end of the active lifetime.
@Override
public void onPause(){
// Suspend UI updates, threads, or CPU intensive processes
// that don’t need to be updated when the Activity isn’t
// the active foreground activity.
super.onPause();
}

// Called at the end of the visible lifetime.
@Override
public void onStop(){
// Suspend remaining UI updates, threads, or processing
// that aren’t required when the Activity isn’t visible.
// Persist all edits or state changes
// as after this call the process is likely to be killed.
super.onStop();
}

// Called at the end of the full lifetime.
@Override
public void onDestroy(){
// Clean up any resources including ending threads,
// closing database connections etc.
super.onDestroy();
}
}

Tuesday, May 19, 2009

Differences between @+id/foo and @android:id/foo syntaxes in main.xml

@+id/foo means you are creating an id named foo in the namespace of your application. You can refer to it using @id/foo.

@android:id/foo means you are referring to an id defined in the android namespace.
This namespace is the namespace of the framework. In this case, you need to use @android:id/list and @android:id/empty because these are the id the framework expects to find (the framework knows only about the ids in the android namespace.)

http://groups.google.com/group/android-developers/browse_thread/thread/dc8023b221351aa7?pli=1

Saturday, May 16, 2009

Import Android project into Eclipse


  1. Create new workspace in Eclipse (optional) anywhere OUTSIDE of the folder that contains project you are importing

  2. Click File -> New -> Android Project

  3. From the pop-up select “Create project from existing source” and navigate to the top folder of Android project you are importing

  4. Click “Finish”

Thursday, May 14, 2009

Android Market vs. iPhone App Store: The First 24 Hours

Google’s Android Market has been officially live for 24 hours. Here are some early observations and comparisons with the iPhone App Store’s first 24 hours.

There are myriad similarities between iPhone and Android users:


* They like to play games, shop, and know what music they are listening to,
* They are curious about the weather, and
* They generally share the same interests as iPhone users

During the first 24 hours of Android Market, 62 apps were available to consumers, all free. This is less than 10% of the number of apps we saw at the launch of Apple’s App Store. Although Apple allowed both free and paid applications to be distributed when the App Store launched, paid downloads for Android will not be available until Q1 2009.

http://www.medialets.com/blog/2008/10/23/android-market-vs-iphone-app-store-the-first-24-hours/