Android ProgressBar Progress bar use detailed _android

Source: Internet
Author: User
Tags stringbuffer

ProgressBar Progress bar, divided into rotating progress bar and horizontal progress bar, the style of the progress bar to customize according to need, have not understood how the progress bar in the actual project use, the online demo progress bar cases are mostly through button click to increase, reduce the progress of the value, Using method Incrementprogressby (int), the simplest approach is to place the ProgressBar space in an XML layout file and then execute the Incrementprogressby (int) after triggering the event in mainactivity. The code is as follows:

<linearlayout 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 "
tools:context= "cn.teachcourse.www.MainActivity"
android:orientation= "vertical"
android:gravity= " Center "
android:id=" @+id/onclick_ll >
<!--click LinearLayout Control to change ProgressBar progress-->
< ProgressBar
android:id= "@+id/progressbar_horizontal"
style= "? android:attr/progressbarstylehorizontal "
android:layout_width=" match_parent "
android:layout_height=" 15DP "
android:layout_margintop=" 28DP "/>

</LinearLayout>

It's very simple in Java code, as shown in the following illustration:

public class Mainactivity extends actionbaractivity implements view.onclicklistener{private ProgressBar pb_large; priva
Te ProgressBar pb_normal;
Private ProgressBar Pb_small;
Private ProgressBar pb_horizontal;
private int readed;

private int progressvalue;

Private LinearLayout Onclick_ll; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);

Initview ();

drawable drawable = Getresources (). getdrawable (R.drawable.progress_bar_states); Pb_horizontal.setprogressdrawable (drawable),//Set the horizontal scroll bar style pb_horizontal.setmax (100);/set the maximum of the progress bar Onclick_ Ll.setonclicklistener (this);//linearlayout add monitor} private void Initview () {pb_horizontal = (ProgressBar) Findviewbyid (
R.id.progressbar_horizontal);
Onclick_ll= (LinearLayout) Findviewbyid (R.ID.ONCLICK_LL); @Override public void OnClick (View v) {Pb_horizontal.incrementprogressby (5); if (pb_horizontal.getprogress () ==pb_ Horizontal.getmax ()) {Toast.maketext (This, "progressThe bar has reached its maximum value, the progress bar disappears, Toast.length_long. Show (); Pb_horizontal.setprogress (0);
 When the maximum is reached, the progress value is 0}}}

We are here to show the ProgressBar read the file byte stream, the display of Read progress bar, the above simple demo is not said.

Effect diagram: ProgressBar read file byte throttle demo

This shows the process of reading the word stream, the ProgressBar progress, when the reading is complete, the progress bar is full, this is often used in our actual project development, how to become the reason why I write this article.
Layout file Progress_horizontal_read_data.xml

<linearlayout 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 "
tools:context= "cn.teachcourse.www.MainActivity"
android:orientation= "vertical"
android:gravity= " Center ">
<textview
android:layout_width=" match_parent "
android:layout_height=" Wrap_content "
android:layout_weight= "1"
android:id= "@+id/read_data_tv"
android:text= "@string/read_info"
android:scrollbars= "vertical"
android:layout_marginleft= "10DP"
android:layout_marginright= " 10DP "
android:linespacingextra=" 10DP "/>

<progressbar
android:id=" @+id/progressbar_ Horizontal_read_data "
style="? android:attr/progressbarstylehorizontal "
android:layout_width=" Match_ Parent "
android:layout_height=" 15DP "
android:layout_margintop=" 28DP "/>

</LinearLayout>

Java Code Progressbaractivity.java

public class Progressbaractivity extends Actionbaractivity {private ProgressBar pb_horizontal; private int readed; Priva
te int progressvalue;
Private TextView Mreadprogress_tv;
Private String Mdata; Private StringBuffer sb;//Each read byte data store int length;//tag file size private thread thread;//read file thread @Override protected void Oncrea Te (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.progress_horizontal_
Read_data);

Initview ();
drawable drawable = Getresources (). getdrawable (R.drawable.progress_bar_states); String path = environment.getexternalstoragedirectory () + "/progressmonitor.txt";/

ProgressMonitor.txt is a file I save to SDcard, can customize file name Size position final file = new file (path); if (file.exists ()) {length= (int) file.length (); Pb_horizontal.setmax (length);//Set the maximum progress bar pb_ 
Horizontal.setprogressdrawable (drawable);//Custom progress bar Style}//start a thread to read the contents of the file Thread=new thread () {@Override public void run () {
ReadFromFile (File.getabsolutepath ());
}

};

Thread.Start (); } Handler Handler= new Handler () {@Override public void Handlemessage (msg) {switch (msg.what) {case 110:progressvalue + = Msg.a
RG1;
Pb_horizontal.setprogress (Progressvalue);
Mreadprogress_tv.settext (Sb.tostring ());
if (progressvalue==length) {toast.maketext (progressbaractivity.this, file read complete, toast.length_long). Show ();
LOG.D ("Progressvalue-------------->", Progressvalue + "");

Break

}
}

};
public void ReadFromFile (String path) {FileInputStream fis; try {fis = new FileInputStream (path);
DataInputStream dis = new DataInputStream (FIS);
Sb=new StringBuffer ();
byte b[] = new byte[10];//reads 1 bytes while (readed = Dis.read (b))!=-1) {msg = new message (); msg.arg1 = readed;
Msg.what = 110;
Mdata=new String (b,0,readed);
Sb.append (Mdata);
Handler.sendmessage (msg);

try {thread.sleep (1000);} catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();}
} dis.close ();
Fis.close ();

catch (Exception e) {e.printstacktrace ();} private void Initview () {Pb_hoRizontal = (ProgressBar) Findviewbyid (r.id.progressbar_horizontal_read_data);
mreadprogress_tv= (TextView) Findviewbyid (R.ID.READ_DATA_TV);
 }

}

This uses handler message processing, which updates the progress bar after each read to the data, places the read byte data in the ARG1, sets the progress value in the handler Handlemessage method, and refreshes the contents of the TextView display. It's hard to find here. How to get the number of bytes read each time, and then refresh the progress bar, in the learning of the byte stream, we can customize the size of the bytes per read, the file class can get the total size of the files, and finally we get the effect of the above picture. In this example, we can read the phone card files, instead of downloading files, transfer files also adapt.

This is the full content of this article, I hope you can help with the Android software programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.