Pages

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

Solution to Adobe AIR installation problem

Many of Windows vista Home users are facing problem in installation of Adobe AIR applications.When you try to install Adobe app, The dialog show up "Problem occured with AIR insatlltion.Please install latest version of Adobe AIR from get.adobe.com/air/

I was bitten by same problem during TweetDeck installtion,finally i found the solution that worked for me!!

Solution:
1.Open AdobeAir.Exe file located at,

2.C:\Program Files\common Files\Adobe AIR\Versions\1.0\Adobe AIR Application Installer.exe

3.When you open .exe file, it asks for .air Installtion file. Browse .air file you want to install.Installtion Completes
succesfully.

If this solution donen't work for you drop comment & if it worked Like It !!