Progress Bar ICS - android

I have an activity with a listview populated from the web.
While data is loading I would like to display a progress dialog instead of the listview.
My layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="#+android:id/progress_large"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ExpandableListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
I do this so that the progress bar shows itself (and this works):
setProgressBarVisibility(true);
However, once the listview is populated (with an AsyncTask), I'd like the progress bar to disappear which is not the case, it stays above the listview.
Here is what I did in the AsyncTask:
#Override
protected void onPostExecute(Void result) {
setProgressBarVisibility(false);
//... and some code to update UI
}

Do this in your code,
#Override
protected void onPostExecute(Void result) {
//progressBar.dismiss(); //this dismisses the progressbar
progressBar.setVisibility(ProgressBar.GONE)
//... and some code to update UI
}

Instead of having a ProgressBar in the layout, you can create a ProgressDialog in the preExecute method of the AsyncTask and dismiss the ProgressDialog in the postExecute method.

Try this:
Declare dialog: ProgressDialog dialog ;
In onPostExecute: dialog.dismiss();
hope it helps..

Related

Android ProgressBar styled like progress view in SwipeRefreshLayout

I use android.support.v4.widget.SwipeRefreshLayout in my Android app. It wraps a ListView. The content of the list view is downloaded from a server.
A progress view is shown when user swipes down in order to reload data from server. The progress view looks like a piece of circle that grows and shrinks during the animation. It seems that the style of this progress view cannot be customized much. However, I am fine with its built-in style.
I also show the same progress animation during initial data loading. This can be achieved by calling mySwipeRefreshLayout.setRefreshing(true). That's perfectly OK too.
However, I would like to show the same progress indication through the whole app. Consider e.g. another activity that looks like a form with submit button. There is neither a ListView nor a SwipeRefreshLayout in this form activity. Some progress indication should be displayed while the submitted data are transferred to the server. I want to show a progress bar with the same animation like in SwipeRefreshLayout.
Is there a simple and clean way to have the same progress indicator for both a SwipeRefreshLayout and a form activity that does not contain any list view and refresh layout and does not support any swipe gesture?
Thanks.
However, I would like to show the same progress indication through the
whole app. Consider e.g. another activity that looks like a form with
submit button. There is neither a ListView nor a SwipeRefreshLayout in
this form activity. Some progress indication should be displayed while
the submitted data are transferred to the server. I want to show a
progress bar with the same animation like in SwipeRefreshLayout.
yes you can use SwipeRefreshLayout to show like ProgressDialog when button click. check below.
i have added SwipeRefreshLayout inside my layout file still there is not ListView or ScrollView. just like below
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="niu.com.djandroid.jdroid.androidstack.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/activity_main_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="54dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
now in my activity i used AsyncTask to show SwipeRefreshLayout. also use mSwipeRefreshLayout.setOnRefreshListener(null) to stop swipe gesture.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
mSwipeRefreshLayout.setOnRefreshListener(null);
((Button) findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// mSwipeRefreshLayout.setRefreshing(true);
// call AsyncTask
new LongOperation().execute("");
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
// stop mSwipeRefreshLayout
mSwipeRefreshLayout.setRefreshing(false);
}
#Override
protected void onPreExecute() {
// start mSwipeRefreshLayout
mSwipeRefreshLayout.setRefreshing(true);
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
EDITED:15/02/20
After 4 year, Here is great explanation on android developer site
mSwipeRefreshLayout.setOnRefreshListener(null)
didn't work for me, so I made this method to enable and disable refreshing.
private void setToRefresh(boolean refresh) {
if (refresh) {
mSwipeRefreshLayout.setEnabled(true);
mSwipeRefreshLayout.setRefreshing(true);
} else {
mSwipeRefreshLayout.setRefreshing(false);
mSwipeRefreshLayout.setEnabled(false);
}
}

Create splash screen in the same Activity

I need to show splash layout file until operation is completed and than change to main layout.
Here is an example .
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
//Here app is copying files or something other it doesn't mater.
RelativeLayout panel = new RelativeLayout(this);
//Here is creating of main layout
setContentView(panel);
I need to show splash screen while operations are performing (between two methods setContentView) And than I need to set main layout,
I have tried to do in this way but it shows only black screen and only than shows main layout (panel).
Where is the problem ?
Thx for help in advance.
You can do something like this, if you're not doing heavy stuff precisely on setContentView(R.layout.main)
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
handler = new Handler();
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
//Do some heavy stuff
return null;
}
#Override
public void onPostExecute(Void result){
handler.post(new Runnable(){
#Override
public void run(){
setContentView(R.layout.main);
}
});
}
}.execute();
}
EDIT You can check this True SplashScreen
Here is a sample taken from my project:
Layout page, you can customize the progress bar as you want:
`
<ProgressBar
android:id="#+id/inquiry_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:visibility="gone"
android:indeterminateDrawable="#drawable/loading_progress"></ProgressBar>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal|center_vertical"
android:id="#+id/panel_inquiry"
>
.....
</FrameLayout>
`
Java source code:
private ProgressBar progressBar;
private FrameLayout inquiryFrame;
inquiryFrame=(FrameLayout)findViewById(R.id.panel_inquiry);
progressBar=(ProgressBar)findViewById(R.id.inquiry_progress);
In order to show the splash screen/do work in background
private void callService() {
progressBar.setVisibility(View.VISIBLE);
inquiryFrame.setVisibility(View.GONE);
new ServerReqAsync().execute(this
, ServiceConstants.getRequestList());
}
When background job completed, just view the activity default layout
progressBar.setVisibility(View.GONE);
inquiryFrame.setVisibility(View.VISIBLE);
Thanks

ProgressBar just wont show up and show any kind of progress

I have been trying for a while now, Have looked at a few SO posts, and got to this stage. I am sure I am very close to getting it working, but am missing something very basic.
Code Snippets of the activity below
public class SearchResultsMap extends MapActivity {
....
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.searchresultsmap);
...
busy = (LinearLayout) findViewById(R.id.busy);
busy.bringToFront();
progressBar = (ProgressBar) findViewById(R.id.progressBar) ;
progressBar.bringToFront() ;
searchResponse = (HashMap<Integer, CustomJourneyUserInformation>) this.getIntent()
.getSerializableExtra("searchResponse");
new GetProviderAndUserDetails(this).execute(null);
>>edit1 progressBar.setMax(searchResponse.size());
>>edit1 progressBar.setProgress(0) ;
...
}
//Inner Class, type AsyncTask
public class GetProviderAndUserDetails extends AsyncTask<String, Integer, Object> {
public GetProviderAndUserDetails(Activity mapActivity) {
this.mapActivity = mapActivity;
}
protected void onPreExecute() {
busy.setVisibility(View.VISIBLE);
setProgressBarVisibility(true) ;
setProgressBarIndeterminate(true) ;
setProgressBarIndeterminateVisibility(true) ;
}
#Override
protected void onProgressUpdate(Integer... progress) {
if (searchActivity != null) {
searchActivity.setProgress(progress[0]);
>>edit1 progressBar.setProgress(progress[0]) ;
}
}
#Override
protected Object doInBackground(String... args) {
...
//for loop, i
publishProgress(i) ;
//end for loop
...
}
protected void onPostExecute(Object result) {
super.onPostExecute(result);
busy.setVisibility(View.GONE);
setProgressBarVisibility(false) ;
setProgressBarIndeterminate(false) ;
setProgressBarIndeterminateVisibility(false) ;
...
}
}
Layout XML File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/busy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="visible" >
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:indeterminateOnly="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp" />
</LinearLayout>
.....
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/searchMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/tip"
android:layout_alignParentTop="true"
android:apiKey="mykey"
android:clickable="true" />
....
</RelativeLayout>
Device Results : Unable to upload to stackoverflow. Some SO issue.
searchresults image
You're calling setProgressBarIndeterminate() and related functions, which act on your activity's title, not on the progress bar in your layout.
Use progressBar.setIndeterminate() and friends instead.
EDIT: And if you want a progress bar instead of the indeterminate progress indicator, use setIndeterminate(false).
Setting the style to progressBarStyleHorizontal in the xml worked finally. Seems like progressBarStyleLarge cant show progress.
Also the indeterminate thing helped.

Determinate ProgressDialog in Android

I want to have a ProgressDialog with determinate progress bar instead of default spinner. It seems to be easy: ProgressDialog has method setIndeterminate, and method show accepts a boolean to indicate indeterminateness. But these way don't work for me! The dialog is still indeterminate with a spinner. How to change the behaviour?
You need call:
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Check out the official dev guide Showing a progress bar with the example source code attached to that section.
Oh Sorry.. Try this
1) Create a layout file with below code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/RelativeLayout_DownloadProgress" android:paddingLeft="20dp" android:paddingBottom="20dp" android:paddingRight="10dp" android:paddingTop="20dp">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/downloading_video_tv" android:text="#string/downloading_video" android:textStyle="bold" android:textColor="#color/COLOR_WHITE"></TextView>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/download_progress_tv" android:text="#string/zero_percent" android:layout_below="#+id/downloading_video_tv" android:textColor="#color/COLOR_WHITE"></TextView>
<Button
android:id="#+id/download_cancel_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/downloading_video_tv"
android:text="#string/Cancel"
android:textColor="#color/COLOR_CANCEL_BTN" android:layout_marginLeft="5dp">
</Button>
<ProgressBar
android:id="#+id/download_progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="13dp"
android:layout_below="#id/download_progress_tv"
android:layout_toLeftOf="#id/download_cancel_btn"
android:indeterminateOnly="false"
android:max="100"
android:progressDrawable="#drawable/dwnld_progress" >
</ProgressBar>
</RelativeLayout>
2) In your code do the following
i) in oncreate() method add the following lines..
//Download progress layout inflate
RelativeLayout relativelayout_DownloadProgress = (RelativeLayout)_inflater.inflate(R.layout.downloadprogress, null);
TextView downloadProgress_PercentageTV = (TextView)relativelayout_DownloadProgress.findViewById(R.id.download_progress_tv);
Button downloadProgress_CancelBtn = (Button)relativelayout_DownloadProgress.findViewById(R.id.download_cancel_btn);
ProgressBar download_progressBar = (ProgressBar)relativelayout_DownloadProgress.findViewById(R.id.download_progressbar);
downloadProgress_CancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dismissDialog(0);
}
});
ii) //Creating Dialogs i.e., below onCreate() method
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(_activity)
.setView(relativelayout_DownloadProgress)
.create();
}//switch end
return null;
}//onCreateDialog()
iii) on any click event add the following lines.
showDialog(0);download_progressBar.setProgress(0);
Sorry i know its too long code but its working code in application...Try it might need modifications like variables must be declared in class scope.
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Registering. Please wait...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
Try this code should work fine...

handle emptylist and asynctask

I use useful guide written by Cyril Mottier there empty state of ListView inflate in ViewStub with id #android:id/empty. In this method all good, but if i have ListActivity without attached Adapter via setListAdapter or attached empty Adapter then i see empty view. It's all clear but if i want realize logic like this:
Open ListActivity
Attach adapter
Execute AcyncTask and get data for Adapter, show progress dialog
Call notifyDataSetChanged
dismiss dialog
Then from 1 to 3 steps i look empty view with error (e.g. "not found"). But I want show error only after execute AsyncTask. While during executing AsyncTask i want show only process dialog. I try play around use visibility option of #android:id/empty, try use include in xml. But can't realize this logic. Any suggestion would appreciate.
UPD: More information
I have activity.xml:
<FrameLayout
android:id="#id/gd_action_bar_content_view"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ListView
android:id="#android:id/list"
style="#style/listViewStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="false" />
<ViewStub
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
layout="#layout/error" />
</FrameLayout>
error.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="#drawable/ic_cheese" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textStyle="bold"
android:textSize="16sp"
android:text="#string/no_cheese" />
</LinearLayout>
ListActivity Class:
public class MyListActivity extends ListActivity implements OnScrollListener, OnItemClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity); //inflate activity with ViewStub
mAdapter = new MyAdapter(this, Collections.EMPTY_LIST);
setListAdapter(mAdapter);
new SearchAndFill().execute("");
}
}
And Async class:
private class SearchAndFill extends AsyncTask<String, Void, List<Object>>{
LoadingDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
if (dialog==null){
dialog = new LoadingDialog(MyListActivity.this);
dialog.setCancelable(false);
}
dialog.show();
}
#Override
protected List<Object> doInBackground(String... str) {
return search(str[0]); //this is HTTP GET request
}
#Override
protected void onPostExecute(List<Object> result) {
super.onPostExecute(result);
dialog.dismiss();
showResult(result); //this is fill adapter data and call notifyDataSetChanged
}
}
When activity create and inflate activity.xml it's show empty with and then execute AsyncTask, while AsyncTask executing i see empty view. When call onPostExecute method of AsyncTask i see result of HTTP GET request. When activity creating and inflated activity.xml it's show empty with and then execute AsyncTask, while AsyncTask executing i see empty view. When call onPostExecute method of AsyncTask i see result of HTTP GET request. I think it's wrong. I want show blank ListView before AsyncTask executed, and after if result equals Collections.EMPTY_LIST i want show text with image (e.g nothing found)
As much as I understood this is what you want to achieve...
public class MyListActivity extends ListActivity implements OnScrollListener, OnItemClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity); //inflate activity with ViewStub
((ViewStub) findViewById(R.id.YOUR_VIEW_STUB_ID)).setVisibility(View.GONE);
mAdapter = new MyAdapter(this, Collections.EMPTY_LIST);
setListAdapter(mAdapter);
new SearchAndFill().execute("");
}
}
... ... ....
private class SearchAndFill extends AsyncTask<String, Void, List<Object>>{
LoadingDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
if (dialog==null){
dialog = new LoadingDialog(MyListActivity.this);
dialog.setCancelable(false);
}
dialog.show();
}
#Override
protected List<Object> doInBackground(String... str) {
return search(str[0]); //this is HTTP GET request
}
#Override
protected void onPostExecute(List<Object> result) {
super.onPostExecute(result);
if (result == null || result.size() == 0){
mAdapter.notifyDataSetInvalidated();
((ViewStub) findViewById(R.id.YOUR_VIEW_STUB_ID)).setVisibility(View.VISIBLE);
} else {
// add stuff to mAdapter here...
mAdapter.notifyDataSetChanged();
}
dialog.dismiss();
}
}
I suggest you to try setting text value of your empty view to empty string ("") then change the text value to error message when AsyncTask is complete.
Hope it solved your problem.

Categories

Resources