Search This Blog

Showing posts with label Progressbar. Show all posts
Showing posts with label Progressbar. Show all posts

Thursday, March 8, 2012

Run the ProgresBar using the Handler

First of all create tha main.xml file and insert the following code into it.


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                              android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="vertical" >
 <ProgressBar android:id="@+id/progressBar1" style="?       
          android:attr/progressBarStyleHorizontal" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:indeterminate="false" 
          android:max="10" 
          android:padding="4dip" >
 </ProgressBar>
      <Button android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:onClick="startProgress" 
        android:text="Start Progress" > 
</Button> 
</LinearLayout>

Now make an java file and insert the following code into it.

public class ProgressBar extends Activity { 
                             private Handler handler; 
                             private ProgressBar progress;
/** Called when the activity is first created. */ 
                @Override  
                 public void onCreate(Bundle savedInstanceState) { 
                     super.onCreate(savedInstanceState); 
                     setContentView(R.layout.main); 
                     progress = (ProgressBar) findViewById(R.id.progressBar1); 
                     handler = new Handler(); 
             } 

    public void startProgress(View view) { 
                 // Do something long 
                 Runnable runnable = new Runnable() { 
                           @Override public void run() 
                            { 
                                    for (int i = 0; i <= 10; i++) { 
                                             final int value = i; 
                                             try { 
                                                   Thread.sleep(2000); 
                                                   }
                                             catch (InterruptedException e) { 
                                                     e.printStackTrace(); 
                                              }
                  handler.post(new Runnable() { 
                                @Override public void run() { 
                                                 progress.setProgress(value); 
                                     } 
                            }); 
                        }
                   } 
               }; 
                    new Thread(runnable).start(); 
               }
}

Run your application. Once you press your button the ProgressBar will get updated from the background thread.

Wednesday, February 29, 2012

Progressbar using Thread

Here i am going to give the example of the Progress bar using thread.

ProgressBar is a visual indicator of progress in some operation. Displays a bar to the userrepresenting how far the operation has progressed; the application can change the amount of progress (modifying the length of the bar) as it moves forward.

First of all make an .xml file and insert the following into it



<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressbar_default"
/>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressbar_Horizontal"
android:max="100"
/>
</LinearLayout>


make an JAVA file and enter the following code into it.

package com.exercise.AndroidProgressBar;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

public class AndroidProgressBar extends Activity {

         ProgressBar myProgressBar;
         int myProgress = 0;

         /** Called when the activity is first created. */
         @Override
        public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.main);

                        myProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal);

                        new Thread(myThread).start();
         }

         private Runnable myThread = new Runnable(){

         @Override
         public void run() {
                   // TODO Auto-generated method stub
                   while (myProgress<100){
                   try{
                        myHandle.sendMessage(myHandle.obtainMessage());
                        Thread.sleep(1000);
                        }
                   catch(Throwable t){
                     }
            }
 }

          Handler myHandle = new Handler(){

         @Override
          public void handleMessage(Message msg) {
                       // TODO Auto-generated method stub
                       myProgress++;
                       myProgressBar.setProgress(myProgress);
           }

        };
   };
}