0% found this document useful (0 votes)
3 views3 pages

7

The document contains XML layout code for an Android application featuring a progress bar and buttons to start, increase, and perform asynchronous tasks. It also includes Java code for the MainActivity class that manages the progress bar's behavior using AsyncTask and a Handler. The application allows users to visually track progress through a horizontal and circular progress bar.

Uploaded by

ayushx312
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

7

The document contains XML layout code for an Android application featuring a progress bar and buttons to start, increase, and perform asynchronous tasks. It also includes Java code for the MainActivity class that manages the progress bar's behavior using AsyncTask and a Handler. The application allows users to visually track progress through a horizontal and circular progress bar.

Uploaded by

ayushx312
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

XML:

<?xml version="1.0" encoding="utf-8"?> <ProgressBar


<ScrollView
xmlns:android="[Link] android:id="@+id/progressHorizontal"
apk/res/android"
android:layout_width="match_parent" style="?android:attr/progressBarStyleHorizon
android:layout_height="match_parent" tal"
android:background="#F5F5F5">
android:layout_width="match_parent"
<LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:max="100"/>
android:orientation="vertical"
android:padding="20dp"> <!-- Buttons -->
<Button
<!-- Title --> android:id="@+id/btnStart"
<TextView
android:layout_width="wrap_content" android:layout_width="match_parent"

android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Progress Bar Demo" android:text="Start Download"
android:textSize="22sp" android:layout_marginTop="12dp"
android:textStyle="bold"
android:textColor="#212121" android:backgroundTint="#4CAF50"
android:layout_gravity="center" android:textColor="#FFFFFF"/>

android:layout_marginBottom="16dp"/> <Button
android:id="@+id/btnIncrease"
<!-- Card look -->
<LinearLayout android:layout_width="match_parent"

android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Increase Progress"
android:layout_height="wrap_content" android:layout_marginTop="8dp"
android:orientation="vertical"
android:background="#FFFFFF" android:backgroundTint="#2196F3"
android:padding="16dp" android:textColor="#FFFFFF"/>
android:elevation="6dp">
<Button
<!-- Circular Progress --> android:id="@+id/btnAsync"
<ProgressBar
android:id="@+id/progressCircle" android:layout_width="match_parent"

android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="AsyncTask
android:layout_height="wrap_content" Download"
android:layout_gravity="center" android:layout_marginTop="8dp"
android:indeterminate="true"
android:visibility="gone" android:backgroundTint="#FF9800"
android:textColor="#FFFFFF"/>
android:layout_marginBottom="12dp"/>
</LinearLayout>
<!-- Horizontal Progress -->
</LinearLayout> </ScrollView>

Java:
package [Link]; @Override
import [Link]; public void run() {
import [Link]; if (progress < 100) {
import [Link]; progress += 10;
import [Link];
import [Link]; [Link](progress);
import [Link]; [Link](this,
import 1000);
[Link]; } else {

public class MainActivity extends [Link]([Link]);


AppCompatActivity { }
}
ProgressBar progressHorizontal, }, 1000);
progressCircle; });
Button btnStart, btnIncrease, btnAsync;
// 4. Modify progress dynamically using
int progress = 0; button
Handler handler = new Handler(); [Link](v -> {
if (progress < 100) {
@Override progress += 5;
protected void onCreate(Bundle
savedInstanceState) { [Link](progress);
[Link](savedInstanceState); }
setContentView([Link].activity_main); });

progressHorizontal = // 5. AsyncTask progress update


findViewById([Link]); [Link](v -> {
progressCircle = new DownloadTask().execute();
findViewById([Link]); });
btnStart = findViewById([Link]); }
btnIncrease =
findViewById([Link]); // 5. AsyncTask class
btnAsync = class DownloadTask extends
findViewById([Link]); AsyncTask<Void, Integer, Void> {

second (Handler) @Override


[Link](v -> { protected void onPreExecute() {
progress = 0; [Link](0);

[Link]([Link]); [Link]([Link]);
}
[Link](new Runnable()
{ @Override
protected Void doInBackground(Void... protected void
voids) { onProgressUpdate(Integer... values) {
for (int i = 0; i <= 100; i += 10) {
publishProgress(i); [Link](values[0]);
try { }
[Link](1000);
} catch (InterruptedException e) { @Override
[Link](); protected void onPostExecute(Void
} unused) {
}
return null; [Link]([Link]);
} }
}
@Override }

You might also like