Wednesday 3 September 2014

Progress Bar Tutorial in Android



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.

40 comments :

  1. Found your blog. Its really have good information on java programming. Really liked it. Thank you for all the information.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete


  3. Great 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.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. plz suggest me about seekzed.com

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Thanks 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.

    ReplyDelete
  12. public 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.
    Java Training in Chennai

    ReplyDelete
  13. Very useful blog which helps a lot for students who need to learn new things on Java
    Thank you so much
    Keep sharing on...

    ReplyDelete
  14. I have read your blog its very attractive . I like it your blog. i Can share this in My Friend Circle Keep post | Thanks
    Java Training

    ReplyDelete
  15. Thanks for sharing this post. Very useful and informative.Keep posting.Hope you get more detail on Java Training Course
    Best Java Training in Gurgaon

    ReplyDelete
  16. How you become a Jva Expert. Here are the Solution given by you. Thanks for providing a great content.
    BEST JAVA TRAINING INSTITUTE IN DELHI

    ReplyDelete
  17. 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
    python training in rajajinagar
    Python training in btm
    Python training in usa

    ReplyDelete
  18. 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.
    Devops training in sholinganallur

    ReplyDelete
  19. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.

    Blueprism training in Chennai

    Blueprism training in Bangalore

    ReplyDelete
  20. 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.

    python Training in Bangalore | python Training in Bangalore

    ReplyDelete
  21. 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.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  22. It is very good and very informative. There is a useful information in it.Thanks for posting...
    CEH Training In Hyderabad

    ReplyDelete
  23. 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.
    Java Training in Bangalore

    ReplyDelete
  24. 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.



    mobile 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

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. Excellent Information's are shared...The persistence of This Blogs about JAVA give's a successful Result in feature...keep sharing
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  27. 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!
    Java Training in Chennai

    Java Training in Velachery

    Java Training in Tambaram

    Java Training in Porur

    Java Training in OMR

    Java Training in Annanagar


    ReplyDelete
  28. Why use Drupal Development? Below are some of the benefits of using Drupal as a platform for your CMS. python os path join

    ReplyDelete
  29. 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.
    visit: Big Data Analytics: Challenges and Opportunities


    ReplyDelete