Learn Python


Click to Learn Python3

Tuesday, March 29, 2011

ListActivity with checkbox using AsyncTask with sleep time example in android

We are going to see about using the ListActivity with checkbox example and we are going to see, how to using the AsyncTask with a good example, here we will be using sleep call, it is optional. The view will be like this



First we can see about using sleep option in android, in our code we used this


Java

for (String item : items) {
publishProgress(item);
SystemClock.sleep(200);
}


Here SystemClock.sleep(200); is used to sleep for a while, for this ListActivity with checkBox or AsyncTask this is not compulsory, this is used in this example for making to know about this sleep option.

The ListActivity with check box can be done by using this xml code in the layout


XML

<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>


like that we need to insert the code in the java file as


Java

setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_checked, new ArrayList()));


and for check operation we need to override the function as


Java

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView check = (CheckedTextView)v;
check.setChecked(!check.isChecked());
}


That will do for showing a List view using the ListActivity, for using the AsyncTask, we need to call the AsyncTask class like


Java

new AddStringTask().execute();


On this call the AsyncTask class will be called


Java

class AddStringTask extends AsyncTask {
@Override
protected Void doInBackground(Void... unused) {
for (String item : items) {
publishProgress(item);
SystemClock.sleep(200);
}

return (null);
}

@Override
protected void onProgressUpdate(String... item) {
((ArrayAdapter) getListAdapter()).add(item[0]);
}

@Override
protected void onPostExecute(Void unused) {
setSelection(3);
Toast.makeText(AsyncTaskDemo.this, "Done!", Toast.LENGTH_SHORT).show();
}
}


The full AsyncTaskDemo.java file will be like this


Java

package com.async.demo;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;

/**
*
* @author http://www.android-codes-examples.blogspot.com/
*
*/

public class AsyncTaskDemo extends ListActivity {
private static String[] items = { "Joseph", "George", "Mary", "Antony", "Albert",
"Michel", "John", "Abraham", "Mark", "Savior", "Kristopher",
"Thomas", "Williams", "Assisi", "Sebastian", "Aloysius", "Alex", "Daniel",
"Anto", "Alexandar", "Brito", "Robert", "Jose",
"Paul", "Peter" };

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

setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_checked, new ArrayList()));

new AddStringTask().execute();
}

class AddStringTask extends AsyncTask {
@Override
protected Void doInBackground(Void... unused) {
for (String item : items) {
publishProgress(item);
SystemClock.sleep(200);
}

return (null);
}

@Override
protected void onProgressUpdate(String... item) {
((ArrayAdapter) getListAdapter()).add(item[0]);
}

@Override
protected void onPostExecute(Void unused) {
setSelection(3);
Toast.makeText(AsyncTaskDemo.this, "Done!", Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView check = (CheckedTextView)v;
check.setChecked(!check.isChecked());
}


}


You can download the full source code


Download source


Have a good day.

Example code for OnKeyDown listener in Android

In this example Toast is shown for the key presses.



The approach is simple, just we need to put this code, thats all


Java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
switch(keyCode)
{
case KeyEvent.KEYCODE_CAMERA:
Toast.makeText(KeyActions.this, "Pressed Camera Button", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_1:
Toast.makeText(KeyActions.this, "Pressed 1", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_HOME:
Toast.makeText(KeyActions.this, "Pressed Home Button", Toast.LENGTH_SHORT).show();
return true;

case KeyEvent.KEYCODE_BACK:
Toast.makeText(KeyActions.this, "Pressed Back Button", Toast.LENGTH_SHORT).show();
Intent result = new Intent("Complete");
setResult(Activity.RESULT_OK, result);
finish();
return true;
}

return false;
}


For every key there is the code, after entering the KeyEvent. the eclipse will show the list of codes available.

If you have doubts means

The full source code

Download source


Have a good day.