How to create Progress Bar in Android studio ?
Output
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:id="@+id/progressBar"
android:layout_marginTop="20dp"
/>
<Button
android:id="@+id/buttonn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_below="@id/progressBar"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
ProgressBar mProgressBar;
Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressBar = findViewById(R.id.progressBar);
mButton = findViewById(R.id.buttonn);
mProgressBar2 = findViewById(R.id.ProgressBar2);
mButton2 = findViewById(R.id.Button2);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Thread thread = new Thread() {
@Override
public void run() {
super.run();
for (int i = 0; i <= 100; ) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mProgressBar.setProgress(i);
i = i + 10;
}
}
};
thread.start();
}
});
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ProgressBar mProgressBar;
Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressBar = findViewById(R.id.progressBar);
mButton = findViewById(R.id.buttonn);
mProgressBar2 = findViewById(R.id.ProgressBar2);
mButton2 = findViewById(R.id.Button2);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Thread thread = new Thread() {
@Override
public void run() {
super.run();
for (int i = 0; i <= 100; ) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mProgressBar.setProgress(i);
i = i + 10;
}
}
};
thread.start();
}
});
}
}
0 Comments