I have a problem that I want to change values on a screen after swiping views. I mean that when user flips their fingers on screen then a progress bar starts getting values from the server and set values after dismissal on the same screen.
I read many functionalities about ViewFlipper, ViewPager and Horizaontal Pager and many more but nothing finds like I want. Every Options needs all screen data at a time and then the same is responsible for showing it on every page in ViewFlipper.
Please suggest any solutions.
Would it work for you to launch an AsyncTask that will start the progress bar and then update the view when the new data from the server is available. This is slightly modified from the example in the class documentation at http://developer.android.com/reference/android/os/AsyncTask.html:
new GetServerValuesTask().execute(url1, url2, url3);
private class GetServerValuesTask extends AsyncTask<URL, Integer, String[]> {
protected String[] doInBackground(URL... urls) {
return getValuesFromServer(urls);
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(String[] values) {
updateViewWithNewValues(values);
}
}
Related
I have created a splash screen for my app to hide the periodic insertion (after publishing updates) of a large number of records into the different tables of my app's SQLite database. I have been implementing an AsyncTask to handle the insertion off of the UI thread.
I need to create a ProgressDialog (with progress bar, not the simple spinning wheel) to inform the user of the current progress percentage for the insertion operations.
In most examples for setting the dialog's progress bar, the counter variable for the for loop representing the lengthy operation, or the percentage of file download is used to set this progress for the dialog. However, since insertions into different tables may take different amounts of time (depending on number of columns, etc), this approach appears to fail. The closest solution I could see would be to write a publishProgress(some_percentage) line after every record insertion in my doInBackground() method, using the % of records inserted as the parameter for publishProgress(), but this seems like a terribly inelegant and inefficient practice.
The current code for my AsyncTask implementation is below. Any suggestions for the best practice of determining the current progress percentage would be greatly appreciated. Thanks!
private class InsertionAction extends AsyncTask<Void,Integer,Void> {
Context context;
private ProgressDialog dialog;
private ForwardAction(Context context) {
this.context = context;
dialog = new ProgressDialog(context);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
this.dialog.setMessage("Initializing Database. Please Wait...");
this.dialog.show();
this.dialog.getWindow().setGravity(Gravity.BOTTOM);
this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.dialog.setCancelable(false);
}
#Override
protected Void doInBackground(Void... params) {
// Large block of record insertions
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// Forward to the main activity
if (dialog.isShowing()) {
dialog.dismiss();
}
animatedTransition(SPLASH_DISPLAY_TIME/2);
}
#Override
protected void onProgressUpdate(Integer... values) {
}
}
Unfortunately there is no way to programmatically count your lines of code, calculate how much time they need to execute and generate an accurate time-proportionate progress.
I suggest updating the progress bar after a certain interval of lines, e.g. every 90 inserts (10%).
Or update according to what you are doing and modify the progress message (try to be creative), e.g. "Adding users", "Generating death rays", "Creating the universe", "Just a little longer", etc.
I have four Tab at the top of my apps
The content of the fourth tab is that it will get data from sql server and then display in listview
since the amount of data retrieved is quite big, it takes 2-3 sec
The problem is that:
After I click the fourth tab, it has no response, then after 2-3sec, it displays the content
As I know it is loading the data from database, I will not continue to click
However, when users click it and no response, he may click and click and click
How to show something to user so that they know it is loading data??
You should use a CrusorLoader. This will display a loading circle while still making the UI active. Note that even if you're using lower versions of the android API, you can still access the CursorLoader class via the Android Support Package. For more information on loaders, checkout
new SomeTask(0).execute(); // write this line in your 4th tab onCreate()
/** Inner class for implementing progress bar before fetching data **/
private class SomeTask extends AsyncTask<Void, Void, Integer>
{
private ProgressDialog Dialog = new ProgressDialog(yourActivityClass.this);
#Override
protected void onPreExecute()
{
Dialog.setMessage("loading...");
Dialog.show();
}
#Override
protected Integer doInBackground(Void... params)
{
//Task for doing something
// get data from sql server and then display in listview
return 0;
}
#Override
protected void onPostExecute(Integer result)
{
if(result==0)
{
//do some thing if your list completed
}
// after completed finished the progressbar
Dialog.dismiss();
}
}
When a long-running process is started, you'll want to indicate that something is happening so the user knows to wait. You want a progress dialog.
Here is an example:
http://www.androidpeople.com/android-progress-dialog-example
I am using a TabActivity with 4 separate Activities - one for each tab.
One of the Activities is a ListView that has a custom ArrayAdapter.
The issue is that when I press the Tab to change to this view, the Activity loads the content in before the view changes, this appears as though nothing happens for a couple of seconds until the xml is loaded and parsed etc.
I have looked for an example but this is my first Android appllication and I am having difficulty in understanding the flow.
Can anyone point me to some code that will allow me to instantly change the view (I can inform user content is loading) while loading the content in the background thread
thank you
EDIT - I am porting code over from an existing iOS app - I wasn't able to better articulate the problem as I didn't realise how the UI thread could be blocked in this situation, and due to the complexity of the existing code and deadline I didn't want to change the structure too much.
I narrowed down the issue before I saw your code Jennifer but it is the solution I used so Ill mark yours as right.
here is what I used if it helps anyone else, I had to put the function I called to trigger the data load onto a background thread and then display the content when that thread had done its work
This class was declared within my
public class TableView extends ListActivity
Which was hard for me to get my head around having not done this before ;)
public class GetContentTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog pdialog;
#Override
protected void onPreExecute(){
super.onPreExecute();
pdialog = new ProgressDialog(TableView.this);
pdialog.setTitle(progressDialogTitle);
pdialog.setMessage(progressDialogMessage);
pdialog.show();
}
#Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
setUpAndLoadList(); // the function to display the list and fill it with content
pdialog.dismiss();
}
#Override
protected Void doInBackground(Void... params) {
doInitialLoad(); // The function to load any xml data from server
return null;
}
}
You can use a progress Dialog (can inform user content is loading)
ProgressDialog dialog;
private class XMLOperation extends AsyncTask<String, Void, String> {
/*
* (non-Javadoc)
*
* #see android.os.AsyncTask#onPreExecute()
*/
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
System.out.println("onPreExecute");
dialog= ProgressDialog.show(mContext, "", "Loading Content....");
dialog.setCancelable(false);
}
#Override
protected String doInBackground(String... urls) {
//do your Background task
}
protected void onPostExecute(String result) { //dismiss dialog
try {
if(dialog.isShowing()){
dialog.dismiss();
}
} catch (Exception exception) {
dialog.dismiss();
}
}
Use AsyncTask, or (possibly) a separate thread.
http://developer.android.com/reference/android/os/AsyncTask.html
I would also throw in my 2 cents and say don't use TabActivity. Just have your own buttons that look like tabs, but that's not really critical to this topic.
I have an activity in which I display an image that is stored on a website.
I am using the following code to get it from its url and display the activity :
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
setContentView(R.layout.ad_screen);
AsyncTask<String, Void, Integer> task = new AsyncTask<String, Void, Integer>()
{
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
#Override
protected Integer doInBackground(String... urls)
{
fetchAd();
return 0;
}
/** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
#Override
protected void onPostExecute(Integer result)
{
displayAd();
}
};
task.execute("");
}
That works very fine, but the behaviour is not the one I want : in this case, the activity is pushed on the screen (with a right to left animation) and then the AsyncTask begins. So the image is displayed on screen after a delay (which is normal).
But I would like to perform the request before the activity is pushed, so that the screen is displayed directly with its image without any delay.
Is there a way to have this behaviour ?
Thanks in advance.
You can download the image in the previous activity, create a bitmap, then pass it as an extra in the intent that launches this activity.
Use AsyncTask to load images in previous activity, and display the images in the current activity.
You could in the "parent" activity, the one that starts the new activity, do the fetching of the content, store it either in memory or some sort of fast access persistence, and after that start the activity.
Same principle could be to introduce a "loader" activity which would show a loader, and prefetch the data, when all data received it would start the activity that should display the data.
I'm displaying some data by using SQLite. When I click on one button data come from database. It takes some time. At that time the screen is black. At that time I want to display the rotating spinner before the data dispay. Any ideas?
Android provides a ProgressDialog for accomplishing what you want.
First i would like to suggest to have a look at AsyncTask page, so that you will come to know about the AsyncTask exactly.
Now, Implement AsyncTask as given below:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new performBackgroundTask().execute();
}
private class performBackgroundTask extends AsyncTask <Void, Void, Void>
{
private ProgressDialog Dialog = new ProgressDialog(main.this);
protected void onPreExecute()
{
Dialog.setMessage(getString("Please wait..."));
Dialog.show();
}
protected void onPostExecute(Void unused)
{
Dialog.dismiss();
// displaying all the fetched data
}
#Override
protected Void doInBackground(Void... params)
{
// implement long-running task here i.e. select query/fetch data from table
// fetch data from SQLite table/database
return null;
}
}
Enjoy !!!
You should not execute long running tasks in UI thread as this blocks the UI redraw and makes app look unresponsive.
Use AsyncTask to execute long running tasks in background, while still updating the screen.
You can look at the standard music picker as one example of how to do this:
https://android.googlesource.com/platform/packages/apps/Music/+/master/src/com/android/music/MusicPicker.java
In addition to the whole "queries must be done off the main UI thread," this shows an indeterminant progress while first loading its data, fading to the list once the data is available. The function to start the query is here:
https://android.googlesource.com/platform/packages/apps/Music/+/master/src/com/android/music/MusicPicker.java#581
And to do the switch is here:
https://android.googlesource.com/platform/packages/apps/Music/+/master/src/com/android/music/MusicPicker.java#569
The layout has the list view put in a frame layout with another container holding the progress indicator and label. The visibility of these is changed to control whether the list or progress indicator are shown:
https://android.googlesource.com/platform/packages/apps/Music/+/master/res/layout/music_picker.xml