Android: Making a simple animation set - android

I am making an interactive walkthrough for one of my apps, and in a couple of spots an alpha animation and some various other UI changes (such as checkbox pressed states) would be really great.
The walkthrough has a back and next button. The basic idea is that the next button would start the animation (maybe as a thread?), and if at any point in the animation the back button was pressed, that the animation would stop.
I have looked into the built in Android animation library, but have sort of seen a lot left to be desired. My next thought was a thread, but I know I can't change the UI from an outside thread. Also I want to leave the UI thread open for my back button listener.
Any thoughts on how these simple animations could be achieved?

You're right you can't update UI elements directly from another thread. But you can do this indirectly with Handlers. Handlers are basically a type of IPC that allows you to queue up messages to the UI for proceessing. So what you do is basically create a Handler in your Activity and pass this handler to your process thread. When there's something you want to update in the UI from the process thread just do mHandler.sendEmptyMessage(UPDATE_SOME_VIEW).
Take a look at handlers here.

Related

Exactly what operations can only be done on the user interface thread?

So I understand that any changes to the UI need to be on the main thread for an Android application. And also, you should use other threads to do work so that the UI doesn't freeze up. But some of the work I want to do is preparing UI elements which will be shown later on. I want to get those things ready on a separate thread and then enable a button once its done - that way the user won't be able to access it until it's ready BUT they'll be able to use the main page quickly.
Exactly what operations count as changing the UI? I want to do as much preparation in the background so that the first part of the app ready can be shown ASAP while other parts are still loading.
For example, it seems like findViewById is fine, but what about creating/modifying Views, setting listeners, setId, setEnabled and so on? If I newly create a Button which hasn't been added to a parent, can I setText on it in a background thread?

How to update UI from GLSurfaceView Android

I am working on a game and I am stumped on how to update my TextView that holds the score. In my Activity I have a TextView for the score and a GLSurfaceView. When the user gets puts in the game I want to be able to update the TextView in the same Activity.
I have tried using inflaters and Casting to type Activity as well as a few others. I know it should be possible however I cannot figure out how to achieve this.
It's a bit unclear why you're having trouble. You should be able to update the TextView as you would in any other Activity, whether or not a GLSurfaceView happens to be present. You just have to make sure that you issue the updates from the main UI thread, not the GLSurfaceView renderer thread. An easy way to do this is to post a message to a Handler on the UI thread's Looper.
One example is Grafika's "record GL app" Activity. It uses a plain SurfaceView, rather than a GLSurfaceView, but the idea is the same. The renderer thread periodically posts messages to the main UI thread with the current frame rate, which is placed in a TextView.

Is it necessary to use a separate thread when using SurfaceView?

In Android, I am using a SurfaceView. It is inside a FrameLayout, to draw a couple of things on a transparent layer over the top of a general XML layout (with standard textViews, buttons etc.) The drawing does not involve very intensive computation, and does not animate, it only updates in response to button presses.
All the examples I have seen of SurfaceView use a separate thread for drawing, and then close down that thread in OnSurfaceDestroyed.
My code works without using a separate thread, but it does crash/freeze occasionally, especially when switching between orientations/applications.
So my question is, do I need to use an extra thread to prevent these crashes. And if not, is there any other specific thing I should do in OnSurfaceDestroyed? (I'd rather not post all my code here, just looking for a simple yes/no response and reasons in a couple of sentences).
You don't need to have a separate thread, but it's often a good idea.
For example, take a look at Grafika's "multi-surface test" Activity. It has three overlapping SurfaceViews that are rendered from the UI thread. If you click on the "bounce" button, it starts a new thread to control the animation, because it's simpler to do that way (it can sit in a loop and draw, instead of having to post timed draw events to the UI looper). The bounce thread stops when the Activity is paused. Note the code doesn't do anything in surfaceDestroyed().
The interaction between SurfaceView and the Activity lifecycle can be tricky. A discussion can be found here.
(It can be tricky to get everything right.)

In android, why will my button animation only clear from onClick()?

I wanted to animate a button, so I used this answer:
https://stackoverflow.com/a/4852468/559525
and got it animating on the first attempt. But then when I tried to stop the animation from various places in my application, I found that it will ONLY stop the animation if I call view.clearAnimation() from inside the onClick() callback.
My first guess is this has to do with multi threading synchronize issues, but I know about the UI thread and I was pretty sure I was calling the clearAnimation() method from approved places in the main UI thread.
My other thought was that the UI needed to be invalidated or refreshed? But I tried putting this call right before doing an invalidate on my main layout and that didn't help. I know my invalidate is working properly because it works to update other button attributes like color.
Thanks for any advice!

Android: On clicking Button or TextView my application showing ANR

i am trying to do some heavy task on clicking a button and i want to change the state of the button changed instantly when i click that button but its not happening. Its changing only when the task get completed.I guess i should use multi threading as i am using interaction with webservices too.
any better solution please.
Yes, you shoud better do yoour heavy task in AsyncTask, which is designed for performing tasks in background and notifying UI thread when it's needed.
Look at this article about threading from Google and use examples from it to rewrite your app.
You could put the part of the code for changing the button before the heavy lifting code in the onClickListener, then it would do the UI part first.

Categories

Resources