ProgressBarActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_progress_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.bhaumik.programmingstudy.ProgressBarActivity">
<ProgressBar
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Progress"
android:textAllCaps="false"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:textSize="16dp"
android:id="@+id/btn_progress"/>
</LinearLayout>
ProgressBarActivity.java
package com.bhaumik.programmingstudy;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressBarActivity extends AppCompatActivity {
ProgressBar progressBar;
Thread thread;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_bar);
progressBar = (ProgressBar) findViewById(R.id.progress);
btn = (Button) findViewById(R.id.btn_progress);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
thread = new Thread(){
@Override
public void run() {
super.run();
for (int i=0;i<=100;)
{
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.setProgress(i);
i=i+10;
}
}
};
thread.start();
}
});
}
}
0 comments:
Post a Comment