Wednesday 17 August 2016

ProgressBar

Progress Bar

Progress bar is a visual indicator which notify the user that a certain task is still being done, and they need to wait. Android has a class named ProgressBar which is instantiated to create progress bar objects.

Types of Progress Bars
There are two types of progress bars that you can use in your programs.
1.      Indeterminate Progress Bar: It is also called infinite progressbar, which progress continuously moving. This is ideally used when the user either doesn’t know or does not want or length of the task is unknown to display the task status.
  1. Determinate Progress Bar: It's called finite progress bar, which show the progress of application. This is ideally used when the user know the length of the task  status.


 












code -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
>
    <
ProgressBar
       
style="?android:attr/progressBarStyleHorizontal"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_marginTop="16dp"
       
android:indeterminate="false"
       
android:max="100"
        
android:minHeight="100dp"
       
android:progress="30"
       
android:progressBackgroundTint="@color/colorPrimaryDark" />
</
LinearLayout>


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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:id="@+id/ll1"

    >



</LinearLayout>

Java
public class MainActivity extends AppCompatActivity {



    LinearLayout layout1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        layout1=(LinearLayout)findViewById(R.id.ll1);



        ProgressBar progressBar=new ProgressBar(this,null,android.R.attr.progressBarStyleLarge);

        progressBar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));



        progressBar.setMax(100);

        progressBar.setMinimumHeight(100);

        progressBar.setProgress(30);

        layout1.addView(progressBar);

    }

}


indeterminate
 It is responsible to enable the indeterminate mode. Here if enabled the progress bar shows an infinite looping animation.
  • The first method is to use progressBar.setIndeterminate(true) in the activity class.
·         The other method is to use android:indeterminate=”true” in the layout xml file. In this mode the actual progress will not be displayedcode -
Must be a boolean value, either "true" or "false".    
 <ProgressBar
       
android:indeterminate="false"
       ...       
/>
Java Code
ProgressBar progressBar1=(ProgressBar)findViewById(R.id.pbar1);

progressBar1.setIndeterminate(false);

indeterminate Drawable
Drawable used for the indeterminate mode.
XML
android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"

indeterminate Only
Restricts to ONLY indeterminate mode (state-keeping progress mode will not work).
Must be a boolean value, either "true" or "false".
android:indeterminateOnly="false"


Max
It is also possible to use the android:max attribute to set the desired maximum value. The default maximum value of the Progress Bar is 100.
Value must be integer.
android:max="100"
Java Code
 setMax() - This  method set maximum value programmatically.
ProgressBar progressBar1=(ProgressBar)findViewById(R.id.pbar1);

progressBar1.setMax(100);

getMax() - This  method get maximum value.
ProgressBar progressBar1=(ProgressBar)findViewById(R.id.pbar1);

int i=progressBar1.getMax();

Max Height
An optional argument to supply a maximum height for this view.
android:maxHeight="24dip"

Max Width
An optional argument to supply a minimum width for this view.
android:minWidth="24dip"
 
Min Height
An optional argument to supply a minimum height for this view.
android:minHeight="24dip"

Min Width
An optional argument to supply a maximum width for this view.
android:maxWidth="24dip"
 

Progress
Use this attribute to set the default progress value which must be between 0 and maximum value.
XML
android:progress="30"

Java
ProgressBar progressBar1=(ProgressBar)findViewById(R.id.pbar1);

progressBar1.setProgress(30);

Secondary Progress
A program can have two progress bars in operation. This attribute defines the secondary progress value which must be between 0 and maximum value.
XML
android:secondaryProgress="50"

Java
ProgressBar progressBar1=(ProgressBar)findViewById(R.id.pbar1);

progressBar1.setSecondaryProgress(50);

 Progress Bar Style


  • Widget.ProgressBar.Small: This style shows a smaller version of the spinning wheel.
  • Widget.ProgressBar.Horizontal: This style is used if you want a horizontal bar.
  • Widget.ProgressBar.Large: This style shows a larger version of the spinning wheel.
  • Widget.ProgressBar.Inverse: Here the spinner has an inverse color scheme compared to the application color scheme.
  • Widget.ProgressBar.Small.Inverse: This style shows a smaller version of the spinning wheel with inverse color schemes.
  • Widget.ProgressBar.Large.Inverse: Use this style if you want a larger version of the spinning wheel with inverse color scheme.
·         <ProgressBar

    android:id="@+id/pbar1"

    style="?android:attr/progressBarStyleHorizontal"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginTop="16dp"

    android:indeterminate="false"

    android:max="100"

    android:minHeight="100dp"

    android:progress="30"

    android:progressBackgroundTint="@color/colorPrimaryDark" />

Increment By


Progress Dialog + Thread

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    >

    <Button

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="116dp"

        android:text="download file" />

</LinearLayout>

Java
public class MainActivity extends AppCompatActivity {



    Button btnStartProgress;

    ProgressDialog progressBar;

    private int progressBarStatus = 0;

    private 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 = (Button) findViewById(R.id.button2);

        btnStartProgress.setOnClickListener(new View.OnClickListener(){



            @Override

            public void onClick(View v) {

                // creating progress bar dialog

                progressBar = new ProgressDialog(v.getContext());

                progressBar.setCancelable(true);

                progressBar.setMessage("File downloading ...");

                progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

                progressBar.setProgress(0);

                progressBar.setMax(100);

                progressBar.show();

                //reset progress bar and filesize status

                progressBarStatus = 0;

                fileSize = 0;



                new Thread(new Runnable() {

                    public void run() {

                        while (progressBarStatus < 100) {

                            // performing operation

                            progressBarStatus = doOperation();

                            try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}

                            // Updating the progress bar

                            progressBarHandler.post(new Runnable() {

                                public void run() {

                                    progressBar.setProgress(progressBarStatus);

                                }

                            });

                        }

                        // performing operation if file is downloaded,

                        if (progressBarStatus >= 100) {

                            // sleeping for 1 second after operation completed

                            try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}

                            // close the progress bar dialog

                            progressBar.dismiss();

                        }

                    }

                }).start();

            }//end of onClick method

        });

    }

    // checking how much file is downloaded and updating the filesize

    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;//you can add more else if

            } else{

                return 100;

            }

        }//end of while

        return 100;

    }//end of doOperation





}


ListView + Async Task
XML
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:paddingLeft="5dp"

    android:paddingRight="5dp" >



    <LinearLayout

        android:id="@+id/progressbar_view"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:gravity="center_horizontal"

        android:orientation="vertical" >



        <LinearLayout

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:gravity="center_horizontal"

            android:orientation="horizontal" >



            <ProgressBar

                style="?android:attr/progressBarStyle"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center_vertical|center_horizontal" />



            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center_vertical|center_horizontal"

                android:text="Loading data..." />

        </LinearLayout>



        <View

            android:layout_width="fill_parent"

            android:layout_height="1dp"

            android:background="#C0C0C0" />

    </LinearLayout>



    <ListView

        android:id="@+id/listView"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_marginTop="1dip"

        android:visibility="gone" />



</RelativeLayout>
JAVA
public class MainActivity extends AppCompatActivity {



    ListView listView;

    LinearLayout layout;

    List<String> stringValues;

    ArrayAdapter<String> adapter;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);

        layout = (LinearLayout) findViewById(R.id.progressbar_view);



        stringValues = new ArrayList<String>();



        adapter = new ArrayAdapter<String>(this,

                android.R.layout.simple_list_item_1, stringValues);



        listView.setAdapter(adapter);

        new Task().execute();

    }



    class Task extends AsyncTask<String, Integer, Boolean> {

        @Override

        protected void onPreExecute() {

            layout.setVisibility(View.VISIBLE);

            listView.setVisibility(View.GONE);

            super.onPreExecute();

        }



        @Override

        protected void onPostExecute(Boolean result) {

            layout.setVisibility(View.GONE);

            listView.setVisibility(View.VISIBLE);

            adapter.notifyDataSetChanged();

            super.onPostExecute(result);

        }



        @Override

        protected Boolean doInBackground(String... params) {

            stringValues.add("String 1");

            stringValues.add("String 2");

            stringValues.add("String 3");

            stringValues.add("String 4");

            stringValues.add("String 5");



            try {

                Thread.sleep(3000);

            } catch (Exception e) {

                e.printStackTrace();

            }

            return null;

        }

    }

}













22

No comments:

Post a Comment