Android-Customize the button with the progress bar

Source: Internet
Author: User

1. Implement a button with a progress bar, display the prompt message when finished, and set the button to not be clicked again

2. Required Images

3. Code

Mainactivity

Package Com.example.buttondemo;import Android.app.activity;import Android.os.bundle;import Android.os.Handler; Import Android.os.message;import android.view.view;import android.widget.button;import Android.widget.ProgressBar; Import Android.widget.textview;public class Mainactivity extends Activity {int i = 0; ProgressBar ProgressBar = null; Button downloadbtn = null;@ overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstances Tate) Setcontentview (r.layout.activity_main);p Rogressbar = (progressBar) Findviewbyid (r.id.progressbar);d ownloadb TN = (Button) Findviewbyid (r.id.downloadbtn);d Ownloadbtn.setonclicklistener (new View.onclicklistener () {@ Overrid epublic void OnClick (View v) {i = 0;handler.sendemptymessage (). what = 1);}} );}  Handler Handler = new Handler () {public void Handlemessage (android.os.Message msg) {switch (msg.what) {case 1:i + = 5;progressbar.setprogress (i); if (i! =) {handler.sendemptymessagedElayed (New Message (). What = 1, a);d Ownloadbtn.settext ( i + "%");} Set the maximum setting prompt and set the button to be non-clickable else if (i = =) {downloadbtn.settext ("Download Done");d ownloadbtn.setclickable (false);} Break;default:break;}};};}

 layout

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "android:padding bottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ".        Mainactivity "> <relativelayout android:layout_width=" fill_parent "android:layout_height=" 50DP " Android:layout_centerhorizontal= "true" android:layout_centervertical= "true" android:gravity= "bottom"        ;            <progressbar android:id= "@+id/progressbar" style= "? Android:attr/progressbarstylehorizontal" Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:background= "@dr Awable/aa_button_gray_normal "Androidoid:max= "android:progress=" 0 "android:progressdrawable=" @drawable/progress_selector "/> <button android:id= "@+id/downloadbtn" android:layout_width= "Fill_parent" Android:la yout_height= "Fill_parent" android:layout_centerhorizontal= "true" android:layout_centervertical= "true "Android:background=" @drawable/btn_selector "android:text=" Download "/> </RelativeLayout>< /relativelayout>

Btn_selector.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_pressed=" false "android:drawable=" @android: Color/transparent "></ item>    <item android:state_pressed= "true" android:drawable= "@drawable/aa_button_gray_pressed" ></ Item></selector>

Progress_selector.xml

<?xml version= "1.0" encoding= "Utf-8"? ><layer-list xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:id=" @android: id/progress ">        <clip            android:cliporientation=" Horizontal "            android:drawable=" @drawable/aa_button_normal "            android:gravity=" left "/>    </item ></layer-list>

Make a little progress every day

Android-Customize the button with the progress bar

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.