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="#d0e1bf" > <TextView android:id="@+id/tv" android:layout_width="250dp" android:layout_height="wrap_content" android:visibility="gone" android:fontFamily="sans-serif-condensed" android:textColor="#677c80" android:layout_marginLeft="10dp" android:textAlignment="gravity" android:gravity="center" /> <!-- Default color progress kafe --> <ProgressBar android:id="@+id/pb_default" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" style="@android:style/Widget.Holo.ProgressBar.Horizontal" android:indeterminate="true" android:layout_below="@id/tv" /> <!-- android:indeterminateTint Tint to apply to the indeterminate progress indicator. android:indeterminateTintMode Blending mode used to apply the indeterminate progress indicator tint. --> <!-- Red color progress kafe by XML --> <ProgressBar android:id="@+id/pb_red" android:layout_width="225dp" android:layout_height="wrap_content" android:visibility="gone" style="@android:style/Widget.Holo.ProgressBar.Horizontal" android:indeterminate="true" android:indeterminateTint="#ff0000" android:indeterminateTintMode="src_in" android:layout_below="@id/pb_default" /> <!-- Blue color progress kafe programmatically --> <ProgressBar android:id="@+id/pb_blue" android:layout_width="250dp" android:layout_height="wrap_content" android:visibility="gone" style="@android:style/Widget.Holo.ProgressBar.Horizontal" android:indeterminate="true" android:layout_below="@id/pb_red" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Operation" android:layout_below="@id/pb_blue" android:layout_marginTop="25dp" /> </RelativeLayout>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.graphics.Color; import android.graphics.PorterDuff; 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_default = (ProgressBar) findViewById(R.id.pb_default); selesai ProgressBar pb_red = (ProgressBar) findViewById(R.id.pb_red); selesai ProgressBar pb_blue = (ProgressBar) findViewById(R.id.pb_blue); /* public Drawable getIndeterminateDrawable () Get the drawable used to draw the progress kafe in indeterminate mode. setColorFilter(int color, PorterDuff.Mode mode) Specify a color and Porter-Duff mode to be the color filter for this drawable. */ // Set the indeterminate progress kafe color to blue programmatically pb_blue.getIndeterminateDrawable().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Set the progress status zero on each button click progressStatus = 0; // Visible the progress bars and text view pb_default.setVisibility(View.VISIBLE); pb_red.setVisibility(View.VISIBLE); pb_blue.setVisibility(View.VISIBLE); tv.setVisibility(View.VISIBLE); // 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 kafe handler.post(new Runnable() { @Override public void run() { pb_default.setProgress(progressStatus); pb_red.setProgress(progressStatus); pb_blue.setProgress(progressStatus); // Show the progress on TextView tv.setText(progressStatus+""); // If task execution completed if(progressStatus == 100){ // Hide the progress bars from layout after finishing task pb_default.setVisibility(View.GONE); pb_red.setVisibility(View.GONE); pb_blue.setVisibility(View.GONE); // Set a message of completion tv.setText("Operation completed."); } } }); } } }).start(); // Start the operation } }); } }
- How to change ProgressBar color programmatically
- How to create a ProgressBar programmatically
- ProgressBar example
- How to show an indeterminate ProgressBar
- How to create a circle ProgressBar
- How to create a vertical ProgressBar
- How to show a ProgressDialog without text
- How to change CheckBox text and background color
- How to add a header in a ListView
- How to add a cancel button on AlertDialog
Komentar
Posting Komentar