Progress Bar Tutorial in Android Application :
In this tutorial I am using my previous project (you
can see the post here) in which I fetched the data from my blog using JSOUP
library. In this project, I made a progress dialog till the network operation occur.
In progress dialog there is a dialog with the message and a spinner which revolves till the network operation or any background task.
During the data is fetched in the background, progress dialog is used to give the user a better interaction in the android application.
Create new project in Eclipse : File => New => Android Application Project and give
any package name.
Code for activity_main.xml is same as previous tutorial
and is shown below :
<LinearLayout 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:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TITLE :"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="15px"
android:layout_marginTop="10px"
/>
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginLeft="15px"
android:layout_marginTop="10px"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POSTS TITLE :"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="15px"
android:layout_marginTop="10px"
/>
<TextView
android:id="@+id/postsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginLeft="15px"
android:layout_marginTop="10px"
/>
</LinearLayout>
This code is used to show the posts title and blog
title in text views. You can refer my previous tutorial.
Now code for MainActivity.java is shown below :
package com.javalanguageprogramming.jsoupdemo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView titleText, postText;
String title, posts = "";
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize variables
titleText = (TextView)findViewById(R.id.titleText);
postText = (TextView)findViewById(R.id.postsText);
//start progress dialog at the start of network operation
progressBarInitialize();
//run on new thread because we cannot do network operation on main thread
new Thread(new Runnable() {
@Override
public void run() {
try{
//get the Document object from the site. Enter the link of site you want to fetch
Document document = Jsoup.connect("http://javalanguageprogramming.blogspot.in/").get();
//Get the title of blog using title tag
title = document.select("h1.title").text().toString();
//set the title of text view
//Get all the elements with h3 tag and has attribute a[href]
Elements elements = document.select("div.post-outer").select("h3").select("a[href]");
int length = elements.size();
for(int i=0; i<length; i++){
//store each post heading in the string
posts += elements.get(i).text() + "\n\n";
}
//Run this on ui thread because another thread cannot touch the views of main thread
runOnUiThread(new Runnable() {
@Override
public void run() {
//set both the text views
titleText.setText(title);
postText.setText(posts);
//first check if the progress Dialog is already showing than dismiss it
if(progressDialog.isShowing())
progressDialog.dismiss();
}
});
}catch(Exception e){
e.printStackTrace();
//if any exception occurs than progress Dialog should be removed
if(progressDialog.isShowing())
progressDialog.dismiss();
}
}
}).start();
}
private void progressBarInitialize(){
//initialize the progress dialog during the start of network operation
progressDialog = ProgressDialog.show(MainActivity.this, "Loading...", "Fetching the data");
//set the progress dialog to indeterminate state
progressDialog.setIndeterminate(true);
//make the progress dialog not cancellable so that user cannot cancel the network operation
//or you can set it cancellable but for that you need to set the cancel mechanism for the network operation
progressDialog.setCancelable(false);
}
}
In above activity I used show function of
ProgressDialog object and through parameters the message and sub message is
passed to show it on the Activity. I initialize the progress dialog when the
network operation is started and cancel it when network operation finished.
Do not forget to give the uses permission : Internet for the network operation in this application.
Found your blog. Its really have good information on java programming. Really liked it. Thank you for all the information.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete
ReplyDeleteGreat information here. Thank you for sharing. If you are ever in a need of colorado
emergency Services we are Aurora Locksmith Services. You can find us at auroralocksmithco.com.com or call us at: (720) 220-4851.
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteplz suggest me about seekzed.com
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks for posting great information on this blog about Java Programming. I previously followed the site http://java.meritcampus.com/core-java-topics/java-programming-language . But, this blog is also awesome in Java Programming.
ReplyDeletepublic Person(Integer id, String fname, String lname) in this functionaliy i have got an error like right value missing. can you give me a idea for how to rectify this kind of error and how to give it as best and suitable code. keep sharing more. and update me regarding my query.
ReplyDeleteJava Training in Chennai
Aws online training in india
ReplyDeleteSalesforce online training in india
SAS Online Training in india
Salesforce admin online training in india
Linux Online training in India
React.JS online training in india
ReplyDeletePython online Training in India
Oracle DBA online training in India
Java online Training in India
SAP online Training In india
Very useful blog which helps a lot for students who need to learn new things on Java
ReplyDeleteThank you so much
Keep sharing on...
I have read your blog its very attractive . I like it your blog. i Can share this in My Friend Circle Keep post | Thanks
ReplyDeleteJava Training
Thanks for sharing this post. Very useful and informative.Keep posting.Hope you get more detail on Java Training Course
ReplyDeleteBest Java Training in Gurgaon
Java is the most popular programming language.
ReplyDeleteJava training institute in Delhi
How you become a Jva Expert. Here are the Solution given by you. Thanks for providing a great content.
ReplyDeleteBEST JAVA TRAINING INSTITUTE IN DELHI
Excellent blog, I wish to share your post with my folks circle. It’s really helped me a lot, so keep sharing post like this
ReplyDeleteData Science with Python training in chenni
Data Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore
Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me
ReplyDeletepython training in rajajinagar
Python training in btm
Python training in usa
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteDevops training in sholinganallur
Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
ReplyDeleteBlueprism training in Chennai
Blueprism training in Bangalore
Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
ReplyDeletepython Training in Bangalore | python Training in Bangalore
Hello I am so delighted I found your blog, I really found you by mistake, while I was looking on Yahoo for something else, anyways I am here now and would just like to say thanks for a tremendous post. Please do keep up the great work.
ReplyDeleteMicrosoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training
It is very good and very informative. There is a useful information in it.Thanks for posting...
ReplyDeleteCEH Training In Hyderabad
Excellent blog thank u sharing the useful info.....
ReplyDeletedata analytics course
data science course
business analytics course
I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
ReplyDeleteJava Training in Bangalore
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.
ReplyDeletemobile phone repair in Novi
iphone repair in Novi
cell phone repair in Novi
phone repair in Novi
tablet repair in Novi
ipad repair in Novi
mobile phone repair Novi
iphone repair Novi
cell phone repair Novi
phone repair Novi
This comment has been removed by the author.
ReplyDeleteVidmate youtube downloader
ReplyDeleteExcellent Information's are shared...The persistence of This Blogs about JAVA give's a successful Result in feature...keep sharing
ReplyDeleteJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made.thanks lot
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
"such an exceptionally valuable article. Extremely intriguing to peruse this article.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
"
https://globexdocuments.co//
ReplyDeletehttps://globexdocuments.co//
https://globexdocuments.co//
https://globexdocuments.co//
[no anchor text]
https://globexdocuments.co//
https://globexdocuments.co//
https://globexdocuments.co//
I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
ReplyDeleteJava Training in Chennai
Java Training in Velachery
Java Training in Tambaram
Java Training in Porur
Java Training in OMR
Java Training in Annanagar
Why use Drupal Development? Below are some of the benefits of using Drupal as a platform for your CMS. python os path join
ReplyDeleteTired of sharing long, nasty URLs? This app immediately shortens URLsbloom.0 bloom.0 bloom.0 bloom.0 bloom.0 bloom.0 bloom.0 bloom.0 bloom.0 bloom.0
ReplyDeleteVery Nice information
ReplyDeleteMule ESB Training
Best Mulesoft Online Training
Thank you for sharing this insightful content. I always appreciate such high-quality information. The ideas presented here are not only excellent but also quite engaging, making the post a true delight to read. Keep up the fantastic work.
ReplyDeletevisit: Big Data Analytics: Challenges and Opportunities