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