Pages

Showing posts with label Android Tutorial. Show all posts
Showing posts with label Android Tutorial. Show all posts

April 16, 2011

Android Custom Listener Example

Custom listener implemnatation in android with Full Sourcecode.
WorkHelper : Processing class  

package com.customlistener;

import android.location.Location;
import android.util.Log;

/**
 * @author piyush
 * Does data processing.
 */
public class WorkHelper {

 private HelperResult helperResult;
 private String tag="WorkHelper",resultString;
 private int randomNumber=-1,limit=1000;
 
 public WorkHelper(HelperResult helperResult) {
  this.helperResult=helperResult;
 }
 
 public void doProcessing()
 {
  //generate number between 0 to 1000
  randomNumber=(int)(Math.random()*limit);  
  Log.d(tag,"doProcessing:"+randomNumber);
  
  resultString="New RandomNumber is : "+randomNumber;
  //post data back to MainActivity
  helperResult.getResult(resultString);
 }
 
 public static abstract class HelperResult{
        public abstract void getResult(String resultString);
    }
}


MainActivity : UI Class



package com.customlistener;

import com.customlistener.WorkHelper.HelperResult;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * Main Activity 
 * @author piyush
 */
public class MainActivity extends Activity {
 
 private String tag="HelperResult",resultStr="";
 private Button startButton;
 private WorkHelper workHelper;
 private Context context;
 private TextView textViewData;
 
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        context=this;
        workHelper=new WorkHelper(helperResult);
        
        startButton=(Button)findViewById(R.id.button1);
        textViewData=(TextView)findViewById(R.id.textDetail);
        
        startButton.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) 
   {
    //generate random number
    workHelper.doProcessing();
   }
  });
    }
    
    /**
     * Create Instance of HelperResult Class
     * and Implement getResult().
     */
    HelperResult helperResult=new HelperResult() 
    {
     @Override
  public void getResult(String resultString) {
   Log.d(tag,"in MainActivity");
   Log.d(tag,"Result Received :"+resultString);
   resultStr+=resultString+"\n";
   textViewData.setText(resultStr);   
  }
 };
    
}
Download Source Code

August 29, 2010

Android Tips : onClickListener:onClick()


In this post , we will learn "Easy/Effective way to write code in onClickListener's onClick() to know which Button is clicked"

Inside onClick(View view) : write a switch-case as shown in below code.

Android UI Thread & Massive Work Thread

Many times Android Application becomes in-responsive due to heavy load on UI thread & user gets "FORCE CLOSE" message.

Avoid performing long-running operations (such as network I/O) directly in the UI thread — the main thread of an application where the UI is run — or your application may be blocked and become unresponsive. Here is a brief summary of the recommended approach for handling expensive operations:

There is simple solution to make application that does Massive work & need to frequently update UI.
Solution is to create a mechanism using  android.os.handler ,
  1. Create a Handler object in your UI thread
  2. Spawn off worker threads to perform any required expensive operations
  3. Post results from a worker thread back to the UI thread via Message Object.
  4. Update the views on the UI thread as needed
Below is simple solution:

public class MyActivity extends Activity {

    [ . . . ]
    // Need handler for callbacks to the UI thread
    final Handler mHandler = new Handler();

    // Create runnable for posting
    final Runnable mUpdateResults = new Runnable() {
        public void run() {
            updateResultsInUi();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        [ . . . ]
    }

    protected void startLongRunningOperation() {

        // Fire off a thread to do some work that we shouldn't do 

        // directly in the UI thread 

        Thread t = new Thread() {
            public void run() {
                mResults = doSomethingExpensive();
                mHandler.post(mUpdateResults);
            }
        };
        t.start();
    }

    private void updateResultsInUi() {

        // Back in the UI thread -- update our UI elements based on 

        //the data in mResults
        [ . . . ]
    }
}
Article on Android.com | Common Task