ProgressBar Android Element

By Kunal Kapoor
05-05-2018
478

A user interface element that indicates the progress of an operation. Progress bar supports two modes to represent progress: determinate, and indeterminate. 

Determinate progress bar can have finite values. eg they are used to display progress of file download from server.

Indeterminate progress bar have no fixed progress value, they show an endless progressing indication. eg Often used to display progress when page or an activity is loading.

Setting up Progress bar is easy. The following code example shows how to read a data from a seeker and display it on a progress bar

Project Overview:

.


AndroidManifest.xml:

xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ourcoaching.simpleprogressbar">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
intent-filter
>
activity>
application>

manifest>


activity_main.xml :

xml version="1.0" encoding="utf-8"?>
<android.widget.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"
tools:context="com.ourcoaching.simpleprogressbar.MainActivity"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="30dp">

<SeekBar
android:id="@+id/seekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
LinearLayout
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="30dp">

<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:textAlignment="center"
android:text="measurement"/>
LinearLayout>

android.widget.LinearLayout>


MainActivity.java :

package com.ourcoaching.simpleprogressbar;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private ProgressBar progressBar;
private SeekBar seekBar;
private TextView textView;

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

progressBar=(ProgressBar)findViewById(R.id.progressBar);
seekBar=(SeekBar)findViewById(R.id.seekBar);
textView=(TextView)findViewById(R.id.textview);
}


@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onStart() {
super.onStart();

progressBar.setMax(30);
seekBar.setMax(30);
// seekBar.setMin(1);
}

@Override
protected void onResume() {
super.onResume();

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
textView.setText(i+"");
progressBar.setProgress(i);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});
}
}


UI layout view:



Download the Project code from GITHUB




Related Tutorials