Mobile Application Development Practical 13

Question 1
Write a program to display following output.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Download File" />

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginBottom="30dp"
            android:layout_marginLeft="20dp"
            android:textSize="16dp"
            android:text="" />
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical13;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView tv;
    Button b;
    ProgressBar pb;
    Handler h=new Handler();
    int status=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=findViewById(R.id.textView);
        b=findViewById(R.id.button);
        pb=findViewById(R.id.progressBar);

        class ProcessThread extends Thread
        {
            @Override
            public void run() {
            while(status<100)
            {
                try {
                    sleep(200);
                    status++;
                }
                catch (Exception e){
                    e.printStackTrace();
                }
                h.post(new Runnable() {
                    @Override
                    public void run() {
                        pb.setProgress(status);
                    }
                });
            }
            h.post(new Runnable() {
                @Override
                public void run() {
                    tv.setText("Download complete");
                }
            });
            }
        }
        View.OnClickListener cl=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                b.setVisibility(View.INVISIBLE);
                pb.setVisibility(View.VISIBLE);
                ProcessThread pth=new ProcessThread();
                pth.start();
                tv.setText("File Downloading");
            }
        };
        b.setOnClickListener(cl);
    }
}
Output
Screenshot_20230313_193908
Screenshot_20230313_194019
Screenshot_20230313_194040

Leave a Comment

Your email address will not be published. Required fields are marked *