I'm new in the forum and new in android development.
I searched all over internet and around stackoverflow but I can't get an practical answer to my issues. I want to know your opinions about the best approach (handlers, async tasks, etc....) to my application. I have:
**1 - A button that when is clicked must show a list (the data is obtained parsin an html response)
2 - While the list isn't loaded the application must show a "Loading" dialog.**
Now I can load de list when I click on the button, but sometimes (depending on the internet connecxtion) it can take to long and I want to show the Loading progress dialog.
I tried to implement AsyncTasks but win the method onPostExecute the dialog was closed but the list wasn't shown on the UI despite it was with correct data.
Can anyone help me with the best approach?
Thank you very much.
You have two obvious solutions here you can choose from:
1) Put a ProgressBar within your Layout XML and have it set to View.GONE until the button is pressed and then change to View.VISIBLE, changing to View.GONE again on finish.
2) Have a separate Layout XML with a more details loading view and use setContentView to switch between the both during requests.
I often go with the View.GONE ProgressBar approach
Related
This question already has an answer here:
How can I display a Progress at start up application in android
(1 answer)
Closed 5 years ago.
I have a heavy user interface that can delay the application load. I want to show an preloader before the UI of activity loaded. note that my ui is in xml file
EDIT:
If you want to load 10 tabs in a view pager, use a FragmentStatePagerAdapter which only loads neighboring tabs(default behavior).
If the heavy UI you specify is only the UI elements, then the app must freeze while loading it. So you'd better show a non-cancelable dialog(without animation) with loading message and after a few moment (like 200ms) load up your UI and explicitly dismiss the dialog.
But if the heavy stuffs is not just UI, maybe some calculations or image processing, just do it in a background thread while showing a dialog with progress and cancel the dialog when the task is done.
#Hassan according to me if on clicking the launcher icon if it takes sometime for your application to render the first screen(perhaps giving a black screen in between). This needs to be corrected in your application.
On the contrary if your applications main screen requires population/retrieval of certain resources for effectively engaging the users, You can possibly do something like a splash screen(outdated) where you do all "population/retrieval" and then simply pass data to your heavy UI.
Now regarding the progress bar if this fetching of data is small, you can give an indeterminate "custom"(some moving animation that would suite your app) progress bar,else if its something like a download you can easily track its progress and show in a horizontal progressbar
You sure can! You are describing a preloader. Here is a nice example of one https://github.com/rtheunissen/md-preloader
You'll have to add more info to your question to get a specific answer, but there are a few basic principles.
You make your life a lot easier if you use a preloader which doesn't show progress of the load, it just goes round and round, because the speed of some load processes can't be measured.
If its a data-load which is taking the time (such as a call to an API), you might want to set a variable for "loading" to true at the top of your script, then when the data has resolved, set it to "false". In your view, have a state or a conditional element which hides / unhides the preloader.
If lots of images are slowing down the page, you might want to look into "lazy-loading" or using "infinite scroll" to only show content when the UI needs to display it on screen.
Thats all the info I can give without more information on the code you have so far. Hope that helps!
I have an app where an action by the user sometimes causes the app to get data from the server before it can be displayed, like getting detail info for an item (the info is too large to preload it for all items) or refreshing all data.
While this is in process i don't want the user to do anything else. What is the guideline to display a "please wait" message? Ideally it should be possible for the user to cancel the request if he wants to.
I can do it with an alert dialog, but the operation usually takes just half a second to a second, and imo it looks really strange for an alert dialog to pop up just for a moment, maybe not even long enough to be able to read the message.
Another option i see is the snack bar, but it doesn't prevent the user from doing anything else or navigate away.
Is there a guideline or best practice what to do in this scenario?
I'm using Xamarin.Android, but i don't think that matters.
You can show a loading circle in place where the content being loaded will be displayed like this. When it finishes, replace the loading circle with the content.
Actually "i don't want the user to do anything else" is not acceptable in most Android design patterns. Android app should allow the user to take control of the app.
About canceling the request, you can add a cancel button near the loading circle, or add a cross inside the circle which will stop the process when being pressed. The latter is preferred.
For more patterns see this.
About progress dialog
As far as I know, progress dialog which block the user interaction is discouraged in the new Android. But I am sorry that I cannot find the reference yet, maybe somewhere in Material Design guidelines document. However, because you are the developer, it's all up to you :D.
Check out the documentation on AsyncTask.
I would do the long running task in an AsyncTask, show a ProgressDialog in it's onPreExecute method and hide it after it finised, in onPostExecute.
To prevent the user from closing the dialog you can use the Dialog.setCancelable() method.
You can define a custom layout for your Dialog and set it via the setContentView() method.
here i want to develop an app that can run a simple quiz for psychology character test, but here i need some advice on how to show the question on the activity. Because i want to showing each question without moving to other activity.
This is my temporary layout (it's still ugly XD), but i want the question can appear on the inside of that red box. And when user press Next button, the question inside that box can changed but before it change the app has been stored the answer of previous question.
I really don't know how to use fragment or something like that.
NB. This is my mainActivity look :
For your app I would recommend not to use fragments at all, it will make your app much slower than it can be.
What I would do in your case, is only load the new question in the textbox on pressing next, and call an asynchronous function (asynctask) to perform the storage of the answer. This way the user sees no delays whatshowever.
If getting the next question required a server connection, use a progress dialog in between to show the user something happens.
Agree with Bas van Stein, using different fragments will slow down the app. What you could do is separate frequently changing part of your screen (e.g. question with answer options) into a fragment and reload it's data on Next/Answer button click. This will separate the code and make it easier to maintain and change it later.
On the other hand you could do it in the activity itself if you don't want to use fragment.
How can we change the button text and gif loading image on button click till we didn't get the server response in android.
please see the image:
Thanks in advance...
Here's an implementation of what your asking, it's bare bone but it will get you started:
ProgressBar
At the moment, when the user clicks the button, it will display the drawable (and animate) for you but you will have to dismiss the loading animation manually once you've finished with your background task via:
_progressButton.stopLoadingAnimation();
This will dismiss any animation for you and display any previous text that was there. The only thing missing it that there is no text whilst the animation is in progress, but I think you can hack something together for that. I plan on extending this a little further so that it will allow you to call something like _progressButton.setProgress(10) and then set the percentage that is done. I may even make it thread safe.
You should also be able to style it exactly how you have shown in your screenshot.
To use the code, simply download the project, there's sample code there that is pretty much self explanatory.
I have a task to create 10 questions where a user should be able to input the answer by using a touch keypad. The user should be able to cycle through questions by pressing a button called "N" Once the the user answers all of the questions a total score out of 10 would be displayed.
Im not asking for an answer but how to approach such a task.
So far ive created a keypad consisting of numbers and i can get 1 question to work and display whether the user entered correct or incorrect information but trying to get more then 1 question to work is messing up.
I had a thought and know that i can create 10 seperate activities for 10 questions but thats slightly crazy.
Can someone give me guidance on how to approach such as task
note im pretty new to android.
Thank You
All views have a visibility attribute (android:visibility); what would look nice is every time the user presses next is to simply hide the old view and make the next one show up (Can even add a fading transition to make it look fun as well)
In XML you can set them all to android:visibility="gone" to begin with, and then in your code, set it to objectName.setVisibility(0) to make it visible, and objectName.setVisibility(8) to hide it completely again.
Here is one approach (not necessarily the best):
*Wherever your question is (hopefully a TextView), get a reference of it in your activity.
*When the user answers a question, output if it was correct or not (A toast?)
*Change the TextView to the next question
You can programmatically add and remove views. You could create an empty view with just a linear layout then add and modify the existing views as you need them.
Are you writing it as a native Android app? A web-app (HTML, Javascript) could do what you're asking, and could be turned into an native app with embedded webkit view.