How To Implement Progressbar While Loading Data?
I want to implement progressBar like on the picture below. Right now, I have AsyncTask whic is sperated from fragments classes, and I use that AsyncTask to load data, and right now
Solution 1:
Dude,
If you really just want a progress bar like the picture you posted, you can simply set the progress bar indeterminate property to true :)
You can eighter do on code or directly on the xml.
In code:
yourProgressBar.setIndeterminate(true);
In your XML, just set the attribute indeterminate to true.
Here's is a simple layout as an example:
<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/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="true" />
</RelativeLayout>
Solution 2:
If you just want to show progress, take a look at this code. This might give you an idea:
publicclassMainActivityextendsActivityimplementsOnClickListener {
myTask mytask = newmyTask();
Button button1;
ProgressBar progressbar;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
progressbar = (ProgressBar) findViewById(R.id.progressBar);
button1.setOnClickListener(this);
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
@OverridepublicvoidonClick(View view) {
mytask.execute();
}
classmyTaskextendsAsyncTask<String, String, String> {
@OverrideprotectedvoidonPreExecute() {
progressbar.setProgress(0);
progressbar.setMax(100);
int progressbarstatus = 0;
};
@OverrideprotectedStringdoInBackground(String... params) {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
progressbar.incrementProgressBy(10);
}
return"completed";
}
@OverrideprotectedvoidonPostExecute(String result) {
// TODO Auto-generated method stubsuper.onPostExecute(result);
}
@OverrideprotectedvoidonProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
}
}
But if you want to show like a dialog, take a look at this code:
publicclassYoutubeVideoMainextendsActivity {
ListView videolist;
ArrayList<String> videoArrayList = newArrayList<String>();
ArrayAdapter<String> videoadapter;
Context context;
String feedURL = "https://gdata.youtube.com/feeds/api/users/twistedequations/uploads?v=2&alt=jsonc&start-index=1&max-results=5";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.youtubelist);
videolist = (ListView) findViewById(R.id.videolist);
videoadapter = newArrayAdapter<String>(this, R.layout.video_list_item,
videoArrayList);
videolist.setAdapter(videoadapter);
VideoListTask loadertask = newVideoListTask();
loadertask.execute();
}
privateclassVideoListTaskextendsAsyncTask<Void, String, Void> {
ProgressDialog dialogue;
@OverrideprotectedvoidonPostExecute(Void result) {
super.onPostExecute(result);
dialogue.dismiss();
videoadapter.notifyDataSetChanged();
}
@OverrideprotectedvoidonPreExecute() {
dialogue = newProgressDialog(context);
dialogue.setTitle("Loading items..");
dialogue.show();
super.onPreExecute();
}
@OverrideprotectedVoiddoInBackground(Void... params) {
HttpClient client = newDefaultHttpClient();
HttpGet getRequest = newHttpGet(feedURL);
try {
HttpResponse response = client.execute(getRequest);
StatusLine statusline = response.getStatusLine();
int statuscode = statusline.getStatusCode();
if (statuscode != 200) {
returnnull;
}
InputStream jsonStream = response.getEntity().getContent();
BufferedReader reader = newBufferedReader(
newInputStreamReader(jsonStream));
StringBuilder builder = newStringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String jsonData = builder.toString();
JSONObject json = newJSONObject(jsonData);
JSONObject data = json.getJSONObject("data");
JSONArray items = data.getJSONArray("items");
for (int i = 0; i < items.length(); i++) {
JSONObject video = items.getJSONObject(i);
videoArrayList.add(video.getString("title"));
}
Log.i("YouJsonData:", jsonData);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
returnnull;
}
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
}
Maybe this can help you.
Solution 3:
With the same asynctask using publishProgress()
Solution 4:
classYourCLassNameextendsAsyncTask<String, Integer, Void> {
privateProgressDialog pd = newProgressDialog(Files.this);
@OverrideprotectedvoidonPreExecute() {
// TODO Auto-generated method stubsuper.onPreExecute();
pd.setTitle("your message");
pd.show();
}
@OverrideprotectedVoiddoInBackground(String... params) {
// YOUR PIECE OF CODEreturnnull;
}
@OverrideprotectedvoidonPostExecute(Void result) {
// TODO Auto-generated method stubsuper.onPostExecute(result);
pd.setMessage("Task Completed .. whatever");
@OverrideprotectedvoidonProgressUpdate(Integer... values) {
// TODO Auto-generated method stubsuper.onProgressUpdate(values);
}
}
Post a Comment for "How To Implement Progressbar While Loading Data?"