Practical No 13
Practical No 13
Practical No.13
ANS— To update the percentage of progress displayed in Android, you can use
the following attributes in the XML layout file:
android:max: Sets the maximum value of the progress bar. The default value is
100.
android:indeterminate: A boolean value that determines whether the progress
bar shows the actual progress or a cyclic animation. Set to false to show the
actual progress, or true to display a cyclic animation.
android:progress: Sets the number by which the progress bar value will be
incremented.
style: Sets the display of the progress bar. The default is a spinning wheel, but
you can set it to a horizontal bar by using the
attribute style=“? android:attr/progressBarStyleHorizontal”.
You can also create a progress bar in an Android app by adding
a <ProgressBar> element to the XML layout file.
ANS— <ProgressBar
android:id="@+id/idPBLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/idBtnDisplayProgress"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:visibility="gone" />
ANS— Here are some different progress bar styles available in Android:
Mobile Application Development (22617) Ms. Batule P.S
Indeterminate
Used when the progress information is unclear, the loading progress is
unknown, or the waiting time is incalculable
Circular
An animated indicator moves along an invisible circular track in a clockwise
direction. This type of progress bar can be applied to a surface, such as a button
or card.
Horizontal
A horizontal line that grows from left to right as the progress increases
Linear
An indicator animates along a fixed, visible track to display progress. This type of
progress bar supports both determinate and indeterminate operations
ANS— MainActivity.java:
package com.blogspot.codingatharva.manualprograms;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
ProgressBar pb;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = findViewById(R.id.btn);
Mobile Application Development (22617) Ms. Batule P.S
pb = findViewById(R.id.progressBar);
b.setOnClickListener(new View.OnClickListener() {
@Override
startProgress(v);
}
});
}
public void startProgress(View view) {
pb.setProgress(0);
}
class Task implements Runnable {
@Override
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
pb.setProgress(value);
} }}
}
activity_main.xml:
<div style="white-space: normal; height: auto; visibility: visible; font-
size: 14px;">
<RelativeLayout xmlns:android =
"http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
Mobile Application Development (22617) Ms. Batule P.S
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:layout_margin= "16dp"
<ProgressBar
android:id= "@+id/progressBar"
style= "?android:attr/progressBarStyleHorizontal"
android:layout_width= "200dp"
android:layout_height= "200dp"
android:layout_centerInParent= "true"
android:background= "@drawable/circular_shape"
android:indeterminate= "false"
android:max= "100"
android:progress= "0"
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_below="@+id/progressBar"
android:text="Start"/>
</RelativeLayout></div>
android:fromDegrees= "270"
<shape
android:innerRadiusRatio= "2.5"
android:shape= "ring"
android:thickness= "1dp"
android:useLevel= "true" > <!-- this line fixes the issue for
lollipop api 21 -->
<gradient
android:angle= "0"
android:endColor= "#007DD6"
android:startColor= "#007DD6"
android:type= "sweep"
</shape>
</rotate>
android:innerRadiusRatio= "2.5"
android:shape= "ring"
android:thickness= "1dp"
</shape>
ANS-- MainActivity.java:
package com.example.mad;
import android.os.Bundle;
import android.os.Handler;
Mobile Application Development (22617) Ms. Batule P.S
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadProgressBar = findViewById(R.id.downloadProgressBar);
downloadButton = findViewById(R.id.downloadButton);
downloadProgressBar.setVisibility(ProgressBar.VISIBLE);
downloadButton.setEnabled(false);
handler.postDelayed(new Runnable() {
int progress = 0;
@Override
progress += 5;
downloadProgressBar.setProgress(progress);
Mobile Application Development (22617) Ms. Batule P.S
handler.postDelayed(this, 500);
else {
downloadProgressBar.setVisibility(ProgressBar.GONE);
downloadButton.setEnabled(true);
}}
},500);
}}
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools=http://schemas.android.com/tools
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button android:id="@+id/downloadButton"
android:layout_width="wrap_content" a
ndroid:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Download File"
android:onClick="startDownload"/>
<ProgressBar android:id="@+id/downloadProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/downloadButton"
Mobile Application Development (22617) Ms. Batule P.S
android:layout_marginTop="20dp"
android:indeterminate="false"
android:visibility="gone"/>
</RelativeLayout>