Breaking

Friday 21 December 2018

ProgressBar Tutorial With Example In Android Studio | Android Solution | ProgressBar Tutorial


ProgressBar Tutorial With Example In Android Studio

In Android, ProgressBar is utilized to show the status of work being done like investigating status of work or downloading a record and so on. In Android, as a matter of course an advancement bar will be displayed as a turning wheel however If we need it to be displayed as a flat bar then we have to utilize style quality as level. It for the most part utilize the "android.widget.ProgressBar" class.




Important Note: An advancement bar can likewise be made uncertain. In this mode an advancement bar demonstrates a cyclic liveliness without a sign of advancement. This mode is utilized in application when we don't have the foggiest idea about the measure of work to be finished.

To add an advancement bar to a design (xml) document, you can utilize the <ProgressBar> component. As a matter of course, an advancement bar is a turning wheel (a vague marker). To change to an even advancement, apply the advancement bar's level style.

ProgressBar code:


<ProgressBar
android:id="@+id/simpleProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Horizontal ProgressBar code:


<ProgressBar
android:id="@+id/simpleProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

Important Methods Used In ProgressBar:


1. getMax() – restores the most extreme estimation of advancement bar

We can get the most extreme estimation of the advancement bar in java class. This strategy restores a whole number esteem. The following is the code to get the most extreme incentive from a Progress bar.

ProgressBar simpleProgressBar=(ProgressBar) findViewById(R.id.simpleProgressBar); // initiate the progress bar 
int maxValue=simpleProgressBar.getMax(); // get maximum value of the progress bar

2. getProgress() – returns current advancement esteem

We can get the present advancement esteem from an advancement bar in java class. This strategy additionally restores a whole number esteem. The following is the code to get current advancement esteem from a Progress bar.

ProgressBar simpleProgressBar=(ProgressBar)findViewById(R.id.simpleProgressBar); // initiate the progress bar
int progressValue=simpleProgressBar.getProgress(); // get progress value from the progress bar

Attributes of ProgressBar In Android:


Presently how about we examine important attributes that encourages us to design a Progress bar in xml document (format).

1. id: id is a credit used to exceptionally identify a Progress bar.

<ProgressBar
android:id="@+id/simpleProgressBar" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

2. max: max is a credit utilized in android to characterize maximum estimation of the advancement can take. It must be a whole number esteem like 100, 200 and so on.

Underneath we set 100 maximum incentive for an advancement bar.

<ProgressBar
    android:id="@+id/simpleProgressBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:max="100" />

Set Max Value of ProgressBar In Java Class :


ProgressBar simpleProgressBar=(ProgressBar) findViewById(R.id.simpleProgressBar); // initiate the progress bar
simpleProgressBar.setMax(100);


3. advance: advance is a credit utilized in android to characterize the default advance an incentive among 0 and max. It must be a whole number esteem.

Beneath we set the 100 max esteem and afterward set 50 default advance.

<ProgressBar
    android:id="@+id/simpleProgressBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="100"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:progress="50"/

Setting Progress Value of ProgressBar In Java Class :


ProgressBar simpleProgressBar=(ProgressBar)findViewById(R.id.simpleProgressBar); // initiate the progress bar
simpleProgressBar.setMax(100); // 100 maximum value for the progress value
simpleProgressBar.setProgress(50); // 50 default progress value for the progress bar

4. progressDrawable: advance drawable is a credit utilized in Android to set the custom drawable for the advancement mode.

Underneath we set a custom angle drawable for the advancement method of an advancement bar. Before you attempt underneath code make a point to download an advancement symbol from the web and include your drawable organizer.

Stage 1: Add this code in activity_main.xml or main.xml inside format.

<ProgressBar
android:id="@+id/simpleProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="60"
android:layout_marginTop="100dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:progressDrawable="@drawable/custom_progress"/>

Stage 2: Create another drawable asset xml in drawable organizer and name it custom_progress. Here include the beneath code which makes inclination impact in progressbar.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <gradient
                android:endColor="#fff"
                android:startColor="#1f1"
                android:useLevel="true" />
        </shape>
    </item>
</layer-list>

5. background: background credit is utilized to set the background of a Progress bar. We can set a shading or a drawable out of sight of a Progress bar.

Underneath we set the dark shading for the background of a Progress bar.

<ProgressBar
    android:id="@+id/simpleProgressBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="50"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:background="#000"/>

6. indeterminate: indeterminate property is utilized in Android to empower the indeterminate mode. In this mode an advancement bar demonstrates a cyclic movement without a sign of advancement. This mode is utilized in application when we don't have the foggiest idea about the measure of work to be finished. In this mode the genuine working won't be appeared.

In beneath code we set the indeterminate to genuine.

<ProgressBar
    android:id="@+id/simpleProgressBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="50"
    android:background="#000"
    android:padding="20dp" style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:indeterminate="true"/>



Setting indeterminate of ProgressBar In Java class:


ProgressBar simpleProgressBar=(ProgressBar)findViewById(R.id.simpleProgressBar); // initiate the progress bar
simpleProgressBar.setBackgroundColor(Color.BLACK);

7. padding: padding credit is utilized to set the padding from left, right, best or base of ProgressBar.

  • ·         paddingRight: set the padding from the correct side of the Progress bar.
  • ·         paddingLeft: set the padding from the left side of the Progress bar.
  • ·         paddingTop: set the padding from the best side of the Progress bar.
  • ·         paddingBottom: set the padding from the base side of the Progress bar.
  • ·         Padding: set the padding from the all side's of the Progress bar.


Beneath we set the 20dp padding from all the side's of the Progress bar.

<ProgressBar
    android:id="@+id/simpleProgressBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="50"
    android:background="#000"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:padding="20dp"/>



ProgressBar Example In Android Studio:


In the primary example of ProgressBar we displayed a default turning wheel advance bar and a begin catch at whatever point a client tap on the catch the advancement bar is displayed. The following is the last yield, download code and well ordered clarification:



Stage 1: Create a new project and name it ProgressBarExample.

Select File -> New -> New Project then Fill the forms and click "Finish" button.

Step 2: Open res  -> layout -> activity_main.xml (or) main.xml and add following code:

In this step we open an xml file and add the code for displaying a progress bar  and set its visibility to invisible and one start button.

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
 
    <ProgressBar
        android:id="@+id/simpleProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:layout_centerHorizontal="true"/>
 
    <Button
        android:id="@+id/startButton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Start"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:padding="10dp"
        android:background="#0f0"
        android:textColor="#fff"/>
 
</RelativeLayout>

Stage 3: Now Open src - > package - > MainActivity.java

In this progression we open MainActivity where we add the code to initiate the advancement bar and catch and then perform click occasion on catch which display the advancement bar.

package example.gb.progressbarexample;
 
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Button;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // initiate progress bar and start button
        final ProgressBar simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar);
        Button startButton = (Button) findViewById(R.id.startButton);
        // perform click event on button
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // visible the progress bar
                simpleProgressBar.setVisibility(View.VISIBLE);
            }
        });
    }
 
 
}

Output:

Now start the AVD in Emulator and run the App. Tap on the start catch and Progress Bar will be displayed on screen.



Horizontal ProgressBar Example In Android Studio:


In the second example we display a horizontal advancement bar with drawable background and a start catch. Here at whatever point a client taps on catch a thread is utilized to start the advancement. The following is the final output, download code and well ordered explanation:



Stage 1: Create a new project and name it ProgressBarExample.

Select File -> New -> New Project then Fill the forms and click "Finish" button.

Stage 2: Open res - > layout - > activity_main.xml (or) main.xml and add following code:

In this progression we open a xml record and add the code for displaying a horizontal advancement bar by utilizing style property, drawable advancement xml and one start catch.

<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"
    android:background="#000"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
 
    <ProgressBar
        android:id="@+id/simpleProgressBar"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:max="100"
        android:progress="0"
        android:progressDrawable="@drawable/custom_progress" />
 
    <Button
        android:id="@+id/startButton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:background="#0f0"
        android:padding="10dp"
        android:text="Start"
        android:textColor="#fff"
        android:textSize="20sp"
        android:textStyle="bold" />
 
</RelativeLayout>

Stage 3: Create a xml document in drawable - > custom_progress.xml

In this progression we create a custom drawable xml for the advancement bar. In this xml we create a layer list in which we create a thing and then set the gradient hues for our custom advancement bar.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 
    <item>
        <shape>
            <gradient
                android:endColor="#fff"
                android:startColor="#1f1"
                android:useLevel="true" />
 
        </shape>
    </item>
 
 
</layer-list>

Stage 4: Open app - > package - > MainActivity.java

In this progression we open MainActivity and add the code to initiate the advancement bar, catch and then perform click occasion on catch. After that we start the advancing in an advancement bar utilizing a thread.

package example.gb.progressbarexample;
 
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    int progress = 0;
    ProgressBar simpleProgressBar;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // initiate progress bar and start button
        simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar);
        Button startButton = (Button) findViewById(R.id.startButton);
        // perform click event on button
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // call a function
                setProgressValue(progress);
 
            }
        });
    }
 
    private void setProgressValue(final int progress) {
 
        // set the progress
        simpleProgressBar.setProgress(progress);
        // thread is used to change the progress value
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                setProgressValue(progress + 10);
            }
        });
        thread.start();
    }
 
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
 
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
 
        return super.onOptionsItemSelected(item);
    }
}

Output:

Now run the App in AVD, tap on start catch and you will see horizontal progressbar.




No comments:

Post a Comment