0% found this document useful (0 votes)
11 views6 pages

Practical No 13

The document provides code for creating a circular progress bar in an Android application, including XML layout files and Java code for the MainActivity. It also includes a second program that demonstrates a file download simulation with a progress dialog. Both examples showcase the use of progress bars to indicate progress visually in an Android app.
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)
11 views6 pages

Practical No 13

The document provides code for creating a circular progress bar in an Android application, including XML layout files and Java code for the MainActivity. It also includes a second program that demonstrates a file download simulation with a progress dialog. Both examples showcase the use of progress bars to indicate progress visually in an Android app.
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
You are on page 1/ 6

Practical No:- 13

Q1. write a program to display a circular progress bar

Android_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:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">

<RelativeLayout
android:id="@+id/progress_layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="100dp">

<!--progress bar implementation-->


<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/circular_shape"
android:indeterminate="false"
android:progressDrawable="@drawable/circular_progress_bar"
android:textAlignment="center" />

<!--Text implementation in center of the progress bar-->


<TextView
android:id="@+id/progress_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="---"
android:textColor="@color/colorPrimary"
android:textSize="28sp"
android:textStyle="bold" />
</RelativeLayout>

</LinearLayout>
Practical No:- 13

MainActivity.Java
package com.example.a131;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private ProgressBar progressBar;


private TextView progressText;
int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// set the id for the progressbar and progress text


progressBar = findViewById(R.id.progress_bar);
progressText = findViewById(R.id.progress_text);

final Handler handler = new Handler();


handler.postDelayed(new Runnable() {
@Override
public void run() {
// set the limitations for the numeric
// text under the progress bar
if (i <= 100) {
progressText.setText("" + i);
progressBar.setProgress(i);
i++;
handler.postDelayed(this, 200);
} else {
handler.removeCallbacks(this);
}
}
}, 200);
}
}

Circular_progressBar.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate
Practical No:- 13

xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="270"
android:toDegrees="270">

<!--styling the progress bar-->


<shape
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thickness="8dp"
android:useLevel="true">
<gradient
android:angle="0"
android:endColor="@color/colorPrimary"
android:startColor="#000000"
android:type="sweep"
android:useLevel="false" />
</shape>

</rotate>

Circular_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thickness="3dp"
android:useLevel="false">
<solid android:color="@color/colorPrimary" />
</shape>
Practical No:- 13

Q2. Write a Program To show the following output.


MainActivity.Java
<?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">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:text="download file" />

</RelativeLayout>

MainActivity.Java
package com.example.a132;

import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.*;
import android.view.*;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btnStartProgress; ProgressDialog progressBar; private int
progressBarStatus = 0;
private final Handler progressBarHandler = new Handler();
private long fileSize = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick() {
btnStartProgress = findViewById(R.id.button1);
btnStartProgress.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
Practical No:- 13

progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
progressBarStatus = 0;
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
progressBarStatus = doOperation(); try {
Thread.sleep(1000);
} catch (InterruptedException e) { e.printStackTrace();
}
// Updating the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= 100) {
try { Thread.sleep(1000);
} catch (InterruptedException e) { e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
});
}
});
}
public int doOperation() {
//The range of ProgressDialog starts from 0 to 10000
while (fileSize <= 10000) {
fileSize++;
if (fileSize == 1000) {
return 10;
} else if (fileSize == 2000) {
return 20;
} else if (fileSize == 3000) {
return 30;
} else if (fileSize == 4000) {
return 40;
}
/* else {
return 100;
}*/
}
return 100;
Practical No:- 13

}
}
Output:

You might also like