Android: How to solve slow response after clicking TabWidget - android

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

Related

how to show progress dialog on switching tabs in Tab layout

I have read many solutions here to show a progress dialog on switching tabs as some of the tabs fetching data from server takes time in between that period i need to show progress dialog, do suggest where to put the code to accomplish my task
Initiate an Async which does the fetching data in the background and manages the progress dialog (or even progress layout which might be better) in the onprogressupdate method. If you subclass this it will be fairly easy to implement. Here's a link.
Add this
private class myAsyncTaskClass extends AsyncTask{
#Override
protected void onProgressUpdate(Object... values) {
// TODO Add updates to your progress dialog here.
super.onProgressUpdate(values);
}
#Override
protected Object doInBackground(Object... params) {
// TODO Add your fetching data here
//Use publish progress to call the onProgress update passing whatever you want.
publishProgress(values);
return null;
}}

Delay between One Activity to Another Activity

I develop one application in Android. Which contains so many data (String) and images. string data are come from database and images comes from /res folder.
In My Application First Activity shows categories of books. Then i select any one of them then jump to the next activity which display all books images and brief description of selected category these all data are coming from database with Query operation and fill custom list view with ArrayAdapter. These is working and display all the things which i want.
but the problem is that when i click on category from one activity it takes more than 1 minute time to display second activity (Detail Information of Selected Category). So, Here user is stuck. It is bad for my application.
So is there any way to solve these or any Idea to display Activity Loading Process between One Activity to Second Activity ?
Thanks in Advance.
Please Help Me to solve these.
use AsyncTask as
public class My_Game_Task extends AsyncTask<String, Void, Void> {
#Override
protected void onPreExecute() {
//put a preloder
super.onPreExecute();
}
#Override
protected Void doInBackground(String... arg0) {
// TODO Auto-generated method stub
find data from database
return null;
}
#Override
protected void onPostExecute(Void result) {
dismiss preloader
set adapter here
super.onPostExecute(result);
}
}
call on
oncreate as new My_Game_Task().execute();
this will immediately show the next activity and will show a preloader
First of all where are you testing the app.. if its on emulator then check it on actual device you would get a idea how much delay takes place actually.!
I had similar problem it takes minutes on emulator but runs amazingly on actual device.It takes just 1 second to display..
Try this one it will make a delay of one second before going to execute any other operations,
try {
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}

Setting AsyncTask progress for SQLite - cannot get progress percentage

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.

Android Loading Screen for AsyncHttpRequest

I have a list in my app that is populated via a http request to our database API. Then it parses the JSONArray that is returned, and sets the list appropriately. By the way I am quite new to Java coding and the eclipse environment.
I have achieved the http request with the custom library developed by loopj (located here)
However, when I go to the list in my app, it freezes for a second or two (while it collects the data) and then populates this list and everything works fine. Is it possible to implement a loader that will display until the list has completed loading with the current AsyncHttpClient I am using? Or do I need to change to a different one. I can't provide any code due to contractual agreements.
Thanks for any suggestions!
I do something like this in my Async class
public class Async extends AsyncTask {
private ProgressDialog dialog;
public Context applicationContext;
#Override
protected void onPreExecute() {
//this should appear like a loading bar
this.dialog = ProgressDialog.show(applicationContext, "Calling",
"Update List ...", true);
}
#Override
protected Object doInBackground(Object... params) {
//call your method and threat response
return SyncActivity.getUpdatedList();
}
#Override
protected void onPostExecute(Object result) {
this.dialog.cancel();
}
}

Display the rotating progress before 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

Categories

Resources