android10

  • Increase font size
  • Default font size
  • Decrease font size
Welcome, Guest
Please Login or Register.    Lost Password?

TUTORIAL: ProgressBar running in AsyncTask
(1 viewing) (1) Guest
Go to bottomPage: 1
TOPIC: TUTORIAL: ProgressBar running in AsyncTask
#908
Fernando Cejas
Platinum Boarder
Posts: 782
graph
User Offline Click here to see the profile of this user
TUTORIAL: ProgressBar running in AsyncTask 1 Year, 5 Months ago Karma: 18
It's a exercise of a Horizontal ProgressBar which running in a background AsyncTask.



Modify main.xml to have a button to start the progress, and the ProgressBar.
Code:

<?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"
 />
<Button
 android:id="@+id/startprogress"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Start"
 />
<ProgressBar
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 style="?android:attr/progressBarStyleHorizontal"
 android:id="@+id/progressbar_Horizontal"
 android:max="100"
 />
</LinearLayout>



Modify the java code.
Code:

package com.exercise.AndroidAsyncTaskProgressBar;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class AndroidAsyncTaskProgressBar extends Activity {
 
 ProgressBar progressBar;
 Button buttonStartProgress;
 
 public class BackgroundAsyncTask extends
    AsyncTask<Void, Integer, Void> {
  
  int myProgress;

  @Override
  protected void onPostExecute(Void result) {
   // TODO Auto-generated method stub
   Toast.makeText(AndroidAsyncTaskProgressBar.this,
         "onPostExecute", Toast.LENGTH_LONG).show();
   buttonStartProgress.setClickable(true);
  }

  @Override
  protected void onPreExecute() {
   // TODO Auto-generated method stub
   Toast.makeText(AndroidAsyncTaskProgressBar.this,
         "onPreExecute", Toast.LENGTH_LONG).show();
   myProgress = 0;
  }

  @Override
  protected Void doInBackground(Void... params) {
   // TODO Auto-generated method stub
   while(myProgress<100){
    myProgress++;
    publishProgress(myProgress);
       SystemClock.sleep(100);
   }
   return null;
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
   // TODO Auto-generated method stub
   progressBar.setProgress(values[0]);
  }

 }
 
 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
  
     buttonStartProgress = (Button)findViewById(R.id.startprogress);
     progressBar = (ProgressBar)findViewById(R.id.progressbar_Horizontal);
     progressBar.setProgress(0);
  
     buttonStartProgress.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    new BackgroundAsyncTask().execute();
    buttonStartProgress.setClickable(false);
   }});
 }
}

 
The administrator has disabled public write access.
Go to topPage: 1
get the latest posts directly to your desktop

ANDROID10 --- TIP!!!

android10 tipYou can edit your position easily: just click on your picture when you're logged in, then on the top you will find a menu, go there, edit your profile, go to your position tab and drag the icon in the map. That's all. Just easy!!!
contact android10!!!