<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#ffffffff" android:centerColor="#ffdddddd" android:centerY="0.50" android:endColor="#ffffffff" android:angle="270" /> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#770e75af" android:endColor="#771997e1" android:angle="90" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#ff0e75af" android:endColor="#ff1997e1" android:angle="90" /> </shape> </clip> </item> </layer-list>
<?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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal|center" android:text="Do Work!" /> <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:progressDrawable="@drawable/custom_progress" /> </LinearLayout>
package com.myapp; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; public class MainActivity extends Activity { Button btnDoWork; ProgressBar progressBar; private int progressBarStatus = 0; private Handler progressBarHandler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar = (ProgressBar) findViewById(R.id.progressBar1); progressBar.setVisibility(View.INVISIBLE); // Hide ProgressBar // button1 Do Work btnDoWork = (Button) findViewById(R.id.button1); btnDoWork.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Start Event onClick progressBar.setVisibility(View.VISIBLE); progressBar.setProgress(0); progressBar.setMax(100); progressBar.setProgressDrawable(v.getResources().getDrawable( R.drawable.custom_progress)); progressBarStatus = 0; new Thread(new Runnable() { public void run() { while (progressBarStatus < 100) { // process some tasks progressBarStatus = DoWork(); try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } // Update the progress bar progressBarHandler.post(new Runnable() { public void run() { progressBar.setProgress(progressBarStatus); } }); } } }).start(); } // End Event onClick }); } // DoWork & Set Status Progress Bar public int DoWork() { // Do some work EG: Save , Download , Insert , .. // **** Work // **** Work // **** Work // **** Work progressBarStatus++; // Work process and return status if( progressBarStatus < 100) { return progressBarStatus; } // When Finish try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return 100; } }
<?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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal|center" android:text="Do Work!" /> </LinearLayout>
package com.myapp; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.app.ProgressDialog; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { Button btnDoWork; ProgressDialog progressBar; private int progressBarStatus = 0; private Handler progressBarHandler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // button1 Do Work btnDoWork = (Button) findViewById(R.id.button1); btnDoWork.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Start Event onClick progressBar = new ProgressDialog(v.getContext()); progressBar.setCancelable(true); progressBar.setMessage("Working... ..."); progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressBar.setProgress(0); progressBar.setMax(100); progressBar.setProgressDrawable(v.getResources().getDrawable( R.drawable.custom_progress)); progressBar.show(); progressBarStatus = 0; new Thread(new Runnable() { public void run() { while (progressBarStatus < 100) { // process some tasks progressBarStatus = DoWork(); try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } // Update the progress bar progressBarHandler.post(new Runnable() { public void run() { progressBar.setProgress(progressBarStatus); } }); } } }).start(); } // End Event onClick }); } // DoWork & Set Status Progress Bar public int DoWork() { // Do some work EG: Save , Download , Insert , .. // **** Work // **** Work // **** Work // **** Work progressBarStatus++; // Work process and return status if( progressBarStatus < 100) { return progressBarStatus; } // When Finish try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } progressBar.dismiss(); return 100; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท