How to change button after onclick to loading in android - android

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.

Related

What's the best practice/guideline to display a "please wait" message in Android?

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.

Android dialog : Is there a way to make background android UI clickable while dialog is displayed?

Currently, I am using Libgdx for making app with lots of animation. I am trying to use android Dialog for showing paragraph with html tags through Interface. While, I can change dim color of background UI back to normal with following code.
paragraphDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Is there anything I can do to make background UI work? (Button click, textview focus etc)
And, I have tried GlyphLayout of Libgdx for paragraph. It frequently crashes & also doesn't support html tags. I am using dialogs, as it is pretty easy to use xml layout easily with dialogs. If there are any other options, Please suggest.
Thanks,
your dialog is blocking the entire screen, doesn't matter if the edges/background are somewhat transparent or not 100% alpha, it is still the view that is currently on top. therefor, you can't click on it directly.
i think the best way to achieve your goal is with a fragment. not a dialog.

Splash screen during async

I have an android app which does a lot of background processing on launch so for this I've set a content view with an indeterminate progress bar and then during async onprogressupdate I've set the text of the action being carried out. I would like to instead display my own splash screen with again a textview underneath where I can display the current action. I've researched into using surfaceviews and a thread - I'm not sure however if this is the best way to do this. I plan on displaying a sequence of pngs similar to a boot animation which loops until the async finishes.
So my question is: is a surface view class that implements the runnable the best way to accomplish this or is there a better way?
Thank you in advance!
I'm note quite sure about my answer but I guess surfaceview is a more reliable option for this task. If you use several PNGs instead then you need to cache them for memory purposes and this might cause a little problem for your invalidate method in your animation loop.
You could use a transparent activity as your splash screen which would allow you to do pretty much anything you want whilst it was displayed.
Went for AnimationDrawable in the end which was easy to use and worked amazingly. Credit to vmironov for the idea, unfortunately I can't accept a comment as an answer :(

At what stage does WebView#setBackgroundResource take effect?

I am trying to do something very simple at the initialization of a WebView instance, such that the plain background color is replaced by an image (drawable) until some lengthy operation completes (which then loads a web page, of course). I am trying to accomplish this with:
myWebView.setBackgroundResource(R.drawable.myImage);
Android doesn't complain about anything but it doesn't display the image.
If I replace the above with:
myWebView.setBackgroundColor(Color.parseColor("#123456"));
The background color takes effect without any problem.
What am I missing? What am I doing wrong?
Wow! That was quick (answering my own question):
myWebView.setBackgroundColor(0);
myWebView.setBackgroundResource(R.drawable.myImage);
That's all I had to do. It works!

Custom Splash Screen - Android

Currently my android application shows a black screen with a loading wheel as it processes the user's request i:e as it gets content from the server. I would like to modify this screen to include an icon (image) that fades in and out continuously instead of the loading wheel. Is there any possible way to do it?
Yes, you'll use an Alpha Animation
See here
and here
and lastly here for a good tutorial on Animations with some nice code.
In order to "chain" your animations so that one starts after the other you'll use an Animation listener and start the other one from the onAnimationEnd method callback. Don't forget to put an if statement in there that checks to see if your stuff is done loading otherwise you'll end up with infinite recursion of your fade in and fade out.

Categories

Resources