Thursday, December 30, 2010

Show a progress dialog when a thread is running

  We can easily show a progress dialog with Async task. But asyn task has its own drawbacks. There can be no ui's and the task can be executed only once.
   Better way is to use a thread for lengthy processing and show a progess dialog in the ui thread. (I struggled quite a bit. My progress dialog would keep on spinning infinitely :-( )

e.g.

//this is your main activity thread
handler = new FeedReadHandler();
....
.....

pd = ProgessDialog.show(context,"Loading","Loading the file. please wait...");
Thread background = new Thread(new Runnable(){     

                        @Override
                        public void run() {
                            readFeed(nameUrl);
                            Message msg = handler.obtainMessage();
                            msg.arg1=FEED_LOAD_FINISHED;
                            handler.sendMessage(msg);
                           
                        }
                       
                    });
                    background.start();
                    -.....
......

  Now you are starting a progress dialog and then starting the thread called background to run the function readFeed. When the function is completed, the thread sends a message to the handler.

Here is the handler code
 public class FeedReadHandler extends Handler{
       @Override
       public void handleMessage(Message msg){
           Log.d("SHOWRSS","i think i received a message");
           if(msg.arg1==FEED_LOAD_FINISHED){
               pd.dismiss();
               .....

               .....
              
           }
          
       }
   }

So the handler when receives the message from our background thread, on receiving this message it closes the progress- dialog.

Monday, October 18, 2010

Threads in Android

Many of the network activities delay the program and the screen looks unresponsive. And ultimately you will get ANR.
  You can overcome this with a thread


class MyThread extends Thread{

      public MyThread(){
          //initialise your variables
      }
      public void run(){
         //write your expensive code here
     }
};

  In your ui therad you instantiate this thread


    MyThread myt = new MyThread(a,b,c);
    myt.start();

User will not notice the delay as myt is not in ui thread

Tuesday, September 21, 2010

Activity in android

      Activity is important ui unit of android application.

To start an activity from another, you have to use an intent by sending the new activity class as parameter.

e.g.
         Intent i = new Intent(this, NewActivity.class);


Next you call startActivity method

     startActivity(i);

You can pass extra data to the new actiivity by using putExtra method.


   i.putExtra( "Title",mTitle);


In the new activity, you can extract this information using getExtra methods

   Intent i = getIntent();
   String t = i.getStringExtra("Title");

      

Monday, September 20, 2010

List View - How to avoid black selected items

  Twice I faced the same problem. I had used listview. If you drag on the view it would show all the list elements.

Solution:

   In the code, use
listview.setCacheColorHint(Color.TRANSPARENT).

Your problem is solved

Simple. Isn't it?