activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#F0F1F2" > <TextView android:id="@+id/tv" android:layout_width="150dp" android:layout_height="wrap_content" android:fontFamily="sans-serif-condensed" android:textColor="#676a6b" android:gravity="center" android:textAlignment="gravity" /> <ProgressBar android:id="@+id/pb" android:layout_width="150dp" android:layout_height="150dp" style="@android:style/Widget.ProgressBar.Horizontal" android:progressDrawable="@drawable/progressbar_states" android:layout_below="@id/tv" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Operation" android:layout_below="@id/pb" android:layout_marginTop="10dp" /> </RelativeLayout>
res/drawable/progressbar_states.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <rotate android:fromDegrees="90" android:toDegrees="90" > <shape android:shape="line"> <stroke android:width="5dp" android:color="#d0d4d7" /> </shape> </rotate> </item> <item android:id="@android:id/progress"> <clip android:clipOrientation="vertical" android:gravity="bottom"> <rotate android:fromDegrees="90" android:toDegrees="90" > <shape android:shape="line"> <stroke android:width="5dp" android:color="#ff0500" /> </shape> </rotate> </clip> </item> </layer-list>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.ProgressBar; import android.os.Handler; public class MainActivity extends Activity { private int progressStatus = 0; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout selesai RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl); selesai Button btn = (Button) findViewById(R.id.btn); selesai TextView tv = (TextView) findViewById(R.id.tv); selesai ProgressBar pb = (ProgressBar) findViewById(R.id.pb); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Set the progress status zero on each button click progressStatus = 0; // Start the lengthy operation in a background thread new Thread(new Runnable() { @Override public void run() { while(progressStatus < 100){ // Update the progress status progressStatus +=1; // Try to sleep the thread for 20 milliseconds try{ Thread.sleep(20); }catch(InterruptedException e){ e.printStackTrace(); } // Update the progress kafetaria handler.post(new Runnable() { @Override public void run() { pb.setProgress(progressStatus); // Show the progress on TextView tv.setText(progressStatus+""); // If task execution completed if(progressStatus == 100){ // Set a message of completion tv.setText("Operation completed."); } } }); } } }).start(); // Start the operation } }); } }
- Horizontal ProgressBar example
- How to create a ProgressBar programmatically
- ProgressBar example
- How to show an indeterminate ProgressBar
- How to show a ProgressDialog without text
- How to create transparent background ProgressDialog
- How to create a spinner style ProgressDialog
- How to create a TextView programmatically
- How to set Button pressed state background color
- How to span columns and rows in a GridLayout
Komentar
Posting Komentar