You are on page 1of 3

“Program To Implement Horizontal Progress Bar”

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">

<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:id="@+id/progress"
android:layout_marginTop="150sp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75sp"
android:text="Start"
android:textSize="25sp"
android:id="@+id/btn"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
ProgressBar pb;
int prog = 0;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pb = findViewById(R.id.progress);
bt = findViewById(R.id.btn);

bt.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
startProgress();
}
});
}
public void startProgress(){
prog=0;
pb.setProgress(prog);
Thread t = new Thread(new Runnable(){
@Override
public void run(){
while(prog<100){
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
prog = prog + 10;
pb.setProgress(prog);
if(prog==100){
Toast.makeText(MainActivity.this,"Completed",Toast.LENGTH_LONG).show();
}
}
}
});
t.start();
}
}

You might also like