Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

ProgressBar - Android Widgets Example

ProgressBar - Android Form Widgets เป็น Widget ที่ไว้สำหรับการสร้าง ProgressBar ซึ่งในการเขียน App หลาย ๆ ตัว ProgressBar ถือได้ว่าเป็นฟีเจอร์หลักที่แทบทุก App จำเป็นจะต้องใช้ เพราะในระหว่างการโหลดข้อมูล หรือการโหลดของ Application นั้น การนำ ProgressBar มาใช้งานก็เป็นการแจ้งให้ผู้ใช้หรือ User ทราบว่าในขณะนี้โปรแกรมกำลังทำงานอยู่ และให้ทำการรอจนกว่าจะทำงานเสร็จ และหลังจากเสร็จแล้วก็จะแสดงผลข้อมูลหรือรายละเอียดอื่น ๆ อีกครั้ง

ProgressBar - Android Widgets

XML Syntax
    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ProgressBar" />



รูปแบบ ProgressBar ใน Widgets ของ Android มีอยู่ 2 ประเภทด้วยกัน แบบแรกคือแบบปกติ ที่เป็นวงกลมหมุ่น ๆ และแบบที่ 2 แบบ ProgressBar Horizontal (แนวนอน แสดงสถานะตัวเลขที่กำลัง Progress ทำงาน)

ProgressBar (Horizontal) - Android Widgets Example


Example 1 การใช้ ProgressBar แบบง่าย ๆ ด้วยการแสดง Loading ประมาณ 5 วินาที จากนั้นจะแสดงข้อความหลังจากที่ ProgressBar ทำงานเสร็จสิ้น

ProgressBar - Android Widgets

และตัวอย่างนี้จะเป็นตัวอย่างการใช้งานแบบ ProgressBar ปกติ (วงกลมโหลด)

ออกแบบหน้าจอ GraphicalLayout ด้วย Widget ตามรูป

activity_main.xml (XML Layout)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="163dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressBar1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp"
        android:text="TextView" />

</RelativeLayout>


จาก XML Layout ประกอบด้วย ProgressBar และ TextView โดยในขั้นตอนนี้จะเป็นการแสดง ProgressBar ประมาณ 5 วินาที และจากนั้นจะแสดงข้อความใน TextView

MainActivity.java (Java Code)
package com.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static final int REFRESH_SCREEN = 1;
	ProgressBar Progress;
	TextView txtView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // progressBar1
        Progress = (ProgressBar) findViewById(R.id.progressBar1);
        
        // textView1
        txtView = (TextView) findViewById(R.id.textView1);
        txtView.setVisibility(View.INVISIBLE); // Default Hide
        
        startScan(); // Sleep
    }
    
	public void startScan() {
		new Thread() {
			public void run() {					
				try{			
					Thread.sleep(5000);
					hRefresh.sendEmptyMessage(REFRESH_SCREEN);
				}catch(Exception e){
				}
			}
		}.start();
	}

	Handler hRefresh = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case REFRESH_SCREEN:
				Progress.setVisibility(View.INVISIBLE); // Hide ProgressBar
				ShowText();
				break;
			default:
				break;
			}
		}
	};
	
	public void ShowText() {
		txtView.setText("Welcome to My App");
		txtView.setVisibility(View.VISIBLE); // Show Text
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}


คำอธิบาย
จาก Code ที่เป็น Java มีการเรียกใช้ startScan(); ซึ่งเป็น Method ที่เรียกใช้ Thread และใน Thread จะมีการ Sleep(5000) หรือหยุดการทำงาน 5 วินาที (1000 = 1 นาที) หลังจากนั้นใน Handler ของ Thread จะมีการเรียกใช้ hRefresh ซึ่งจะ ShowText(); ซึ่งจะแสดงข้อความ "Welcome to My App" พร้อม ๆ กับการ Hide ตัว ProgressBar

เพิ่มเติม
ในการใช้งานจริงแล้ว ProgressBar จะใช้แสดงผลในช่วงที่ระหว่างกำลังโหลดข้อมูล หรือทำงานส่วนอื่น ๆ อยู่ และไม่จำเป็นจะต้องใช้ Thread Sleep() นานเกินไป เพราะในช่วงที่ระหว่าง ProgressBar แสดงผลอยู่นั้น เราอาจจะเขียน Handler ของ Thread Sleep() ทำงานในสิ่งที่ต้องการ (สามารถอ่านได้จากบทความ ProgressBar Horizontal)

Screenshot

ProgressBar - Android Widgets

เมื่อโหลดข้อมูลมาหน้าแรก ProgressBar จะทำงานประมาณ 5 วินาที หลังจากนั้นก็จะหายไป

แสดงปุ่ม ProgressBar

ProgressBar - Android Widgets

แสดงข้อความ "Welcome to My App" หลังจากโหลดข้อมูลเรียบร้อย





Example 2 การใช้ ProgressBar กับ ViewSwitcher เพื่อแสดงสถานะ ProgressBar ในช่วงระหว่างที่รอการเปลี่ยน Layout

เกี่ยวกับ ViewSwitcher อ่านได้ที่นี่


ProgressBar - Android Widgets

ออกแบบหน้าจอ GraphicalLayout ด้วย Widget ตามรูป

activity_main.xml (XML Layout)
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewSwitcher1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    	
	<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:id="@+id/tableLayout1"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:layout_marginTop="50dp" >

		<ProgressBar
		    android:id="@+id/progressBar1"
		    style="?android:attr/progressBarStyleLarge"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content" />

     </TableLayout>
     
	
	<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:id="@+id/tableLayout2"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:layout_marginTop="50dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Welcome to My App"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pic_a"
            android:layout_marginTop="10dp"  />
        
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="CopyRight 2012 www.ThaiCreate.Com"
            android:textAppearance="?android:attr/textAppearanceSmall" />   
            
        <Button
		    android:id="@+id/button1"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="Reload again" />
           
    </TableLayout>

</ViewSwitcher>


จาก XML Layout ที่อยู่ภายใต้ ViewSwitcher และจะทำการเปลี่ยนแปลง Layout ระหว่าง tableLayout1 และ tableLayout2 และจะสังเหตุกว่าใน tableLayout1 มี ProgressBar อยู่ ซึ่งเราจะใช้ ProgressBar แสดงผลและสถานะก่อนทำการเปลี่ยนแปลงระหว่าง Layout

MainActivity.java (Java Code)
package com.myapp;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity {

    ViewSwitcher Vs;
    private static final int REFRESH_SCREEN = 1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // viewSwitcher1
        Vs = (ViewSwitcher) findViewById(R.id.viewSwitcher1);
        startScan();
        
        // Reload again
        Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Vs.showPrevious();
            	startScan();
            }
        });
        	
    }
    
	public void startScan() {
		new Thread() {
			public void run() {					
				try{			
					Thread.sleep(5000);
					hRefresh.sendEmptyMessage(REFRESH_SCREEN);
				}catch(Exception e){
				}
			}
		}.start();
	}
	
	@SuppressLint("HandlerLeak")
	Handler hRefresh = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case REFRESH_SCREEN:
				Vs.showNext();
				break;
			default:
				break;
			}
		}
	};

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	
 
}


คำอธิบาย
เมือ่โปรแกรมทำงานจะแสดง Layout แรกที่เป็น tableLayout1 และมีการใช้ Thread Sleep(5000) ซึ่งจะแสดง ProgressBar ประมาณ 5 วินาที และหลังจากนั้น ViewSwitcher จะใช้การ showNext() เพื่อแสดง tableLayout2

Screenshot

ProgressBar - Android Widgets

แสดง tableLayout1 ประมาณ 5 วินาที

ProgressBar - Android Widgets

จากนั้นจะแสดง tableLayout2 ซึ่งจะได้ผลดังรูป





Example 3 การใช้ ViewAnimator กับ ProgressBar สำหรับแสดงผล Progress ก่อนการเปลี่ยนแปลงข้อมูลในแต่ล่ะ Layout เช่นเดียวกับ ViewSwitcher

เกี่ยวกับ ViewAnimator อ่านได้ที่นี่


สำหรับ ViewAnimator แตกต่างกับ ViewSwitcher ตรงที่ จะสามารถมีองค์ประกอบได้หลาย Layout และสามารถกำหนดเงื่อนไขอื่น ๆ หรือ Effect ได้ดีกว่า ViewSwitcher

ProgressBar - Android Widgets

ออกแบบหน้าจอ GraphicalLayout ด้วย Widget ตามรูป

activity_main.xml (XML Layout)
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  	android:id="@+id/viewAnimator1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center_horizontal" >
   
	<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:id="@+id/tableLayout1"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:layout_marginTop="50dp" >

        <Button
	    android:id="@+id/button1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="LoadData" />
            	
     </TableLayout>
     
	

	<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:id="@+id/tableLayout2"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent">

		<ProgressBar
		    android:id="@+id/progressBar1"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content" />

     </TableLayout>
     
	
	<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:id="@+id/tableLayout3"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:layout_marginTop="50dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Welcome to My App"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pic_a"
            android:layout_marginTop="10dp"  />
        
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceSmall" />    
            	     
    </TableLayout>

</ViewAnimator>


จาก XML Layout จะมีการใช้ ViewAnimator ซึ่งจะควบคุมการแสดงผลของ Layout ทั้ง 3 ตัวคือ tableLayout1 , tableLayout1 และ tableLayout3

tableLayout1 = แสดงปุ่ม Button ว่า LoadData
tableLayout2 = แสดง ProgressBar (ใช้ Thread Sleep 5 วินาทีใน Code Java)
tableLayout3 = แสดงข้อความ textView และ imageView

MainActivity.java (Java Code)
package com.myapp;

import java.util.Date;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewAnimator;

public class MainActivity extends Activity {

	ViewAnimator va;
    private static final int REFRESH_SCREEN = 1;
    String currTime;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // viewAnimator1
        va = (ViewAnimator) findViewById(R.id.viewAnimator1);
        
        ShowStep1(); // Default Slow Step1
             	
    }
    
    public void ShowStep1() {
        // Reload Data
        Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	va.showNext();   // Show Load Progress Step2
            	ShowStep2(); 
            }
        });
    }
    
    public void ShowStep2() {
    	// Progress Bar and Check Loading Data
    	CheckLoadingData();
    } 
    
    // Check Loading Data
	public void CheckLoadingData() {
		new Thread() {
			public void run() {					
				try{			
					// Do something
					// Eg : Load Data From Server
					Date d = new Date();
					currTime  = DateFormat.format("EEEE, MMMM d, yyyy ", d.getTime()).toString();
					Thread.sleep(5000);
					
					hRefresh.sendEmptyMessage(REFRESH_SCREEN);
				}catch(Exception e){
				}
			}
		}.start();
	}
	
	@SuppressLint("HandlerLeak")
	Handler hRefresh = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case REFRESH_SCREEN:
		    	va.showNext();   // Show Home Step 3
		    	ShowStep3();  
				break;
			default:
				break;
			}
		}
	};
	
    public void ShowStep3() {
        // textView2
    	TextView txtView2 = (TextView) findViewById(R.id.textView2);
    	txtView2.setText("Current Time : " + currTime);
    } 

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	
 
}


คำอธิบาย
แสดงขั้นตอนการทำงานเหมือนกับตัวอย่างก่อน ๆ หน้านี้ คือ

tableLayout1 -> tableLayout2 [Progress Bar (Sleep 5 Minutes)] -> tableLayout3


Screenshot

ProgressBar - Android Widgets

คลิกที่ LoadData

ProgressBar - Android Widgets

แสดง ProgressBar ประมาณ 5 วินาที

ProgressBar - Android Widgets

แสดง tableLayout3 ซึ่งประกอบด้วย textView และ ImageView






   
Share

Property & Method (Others Related)

Android Form Widgets
TextView - Android Widgets Example
Large Text (TextView) - Android Widgets Example
Medium Text (TextView) - Android Widgets Example
Small Text (TextView) - Android Widgets Example
Button - Android Widgets Example
Small Button (Button) - Android Widgets Example
Toggle Button - Android Widgets Example
Checkbox - Android Widgets Example
RadioButton - Android Widgets Example
Spinner - Android Widgets Example
ProgressBar (Horizontal) - Android Widgets Example
ProgressBar (Large) - Android Widgets Example
ProgressBar (Small) - Android Widgets Example
SeekBar - Android Widgets Example
RadioGroup - Android Widgets Example
RatingBar - Android Widgets Example
Switch - Android Widgets Example

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-07-01 15:56:47 / 2012-07-14 11:33:22
  Download : No files
 Sponsored Links / Related

 
Android Custom Adapter
Rating :

 
Android People Contact List, Name, Phone No, Photo Picture, Email and Address
Rating :

 
Android Rating (Vote) and ListView Part 1
Rating :

 
Android Rating (Vote) and ListView Part 2 (Member Login and Average Rating)
Rating :

 
Android PhoneGap (jQuery Mobile) Create Convert App from Website(URL)
Rating :

 
Android Capture Image and Camera Capture Screenshot (android.view.SurfaceView)
Rating :

 
Android Pull Down to Refresh And Release to Refresh or Update (Part 1)
Rating :

 
Android Pull Down to Refresh And Release to Update (Part 2 , PHP & MySQL)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่