Android button animations synchronize with timing - android

I need an advice how to create some animations I want to add in my buttons. Actually I have the animation code, the thing which I need is how to set properly the timing of each one. Here is what I tried already :
fest.setVisibility(View.INVISIBLE);
handler.postDelayed(new Runnable() {
#Override
public void run() {
fest.setVisibility(View.VISIBLE);
fest.startAnimation(anim);
handler.removeCallbacks(this);
}
}, 500);
This is the things which I did for 7 buttons. First I set the visibility to invisible because I want to achieve the effect that they are appearing after 5 miliseconds after onCreate and for every next button I am increasing the delay time with 5 miliseconds so every of them to appear after the previos one. But the problem in this code is that when the next handler starts for the second button for example, the previos button is getting invisible for a part of the seconds and shows again(I hope someone understand what I mean).
Any suggestions for a bette implementation of something like that?
Thanks in advance!

So here is the thing which fixed that problem. I used this for every button and it's working as I want :
final Handler festHandler = new Handler();
festHandler.postDelayed(new Runnable() {
#Override
public void run() {
Animation anim = AnimationUtils.loadAnimation(Menu.this, R.anim.fadein);
fest.setVisibility(View.VISIBLE);
fest.startAnimation(anim);
festHandler.removeCallbacks(this);
}
}, 400);

Related

How can I add a timer to show a design layout for a few seconds and go back in android studio

I need to show a layout(constrainLayout) for a few second and then switch back to the old layout(constrainLayout), these layout are in the same xml file. I have my mainlayout and I have a errorlayout that shows what error I got from the system, I want to show my errorlayout on the screen for like 3 second and than go back to the main layout so the user can keep doing his thing. I tried using sleep methode but I couldn't find how I could do that. I want to do something like u can see down below first make the layouts visible and invisible and then wait for a few second and reverse them back again. this code is just an example
findViewById(R.id.designLayout).setVisibility(View.INVISIBLE);
findViewById(R.id.errorLayout).setVisibility(View.VISIBLE);
TextView errorcode = findViewById(R.id.errorCode);
errorcode.setText(message);
Thread.sleep(100);
findViewById(R.id.designLayout).setVisibility(View.VISIBLE);
findViewById(R.id.errorLayout).setVisibility(View.INVISIBLE);
Use Handler for this
findViewById(R.id.designLayout).setVisibility(View.INVISIBLE);
findViewById(R.id.errorLayout).setVisibility(View.VISIBLE);
TextView errorcode = findViewById(R.id.errorCode);
errorcode.setText(message);
And After this use Handler
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 500ms
findViewById(R.id.designLayout).setVisibility(View.VISIBLE);
findViewById(R.id.errorLayout).setVisibility(View.INVISIBLE);
}
}, 500);

How to wait button clicks for a while in android

I am developing an application for blinds.
I have 4 screen sized buttons (overlapped). Every step of program one button will be clickable and every button has more than one job.
My program starts with a voice (Android TTS engine). Like "please touch screen to do x". After this step I want to wait 3 seconds for button click, if button is not clicked vocalize "please touch screen to do y" and wait 3 seconds again for job y. (x and y is first button's jobs).
Button should do one of them according to touching screen. But how can I wait 3 seconds for button click and continue to vocalize next options and wait 3 seconds again.
If first button is clicked, it will disappear-button 2 will be clickable- and TTS engine will start to vocalize second buttons options. Application will be work like this but I am stuck in waiting button clicks part.
I would advise you to use android.os.Handler instead. In your case you could do something like this:
public void onCreate() {
this.handler = new Handler()
playTheVoiceOfThingX()
viewToTap.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
doThingX();
}
});
handler.postDelayed(new PrepareThingYTask(), 3000);
}
class PrepareThingYTask() implements Runnable {
viewToTap.setOnClickListener(new OnClickListener(){
#Override
public void onClick() {
doThingY();
}
});
handler.postDelayed(new PrepareThingZTask(), 3000);
}
class PrepareThingZTask() implements Runnable {
....
}
A good reminder, the runnable executed by the handler can be executed in UIThread, so no heavy work on it, or create a different looper to run it.
Regards
You could solve your problem with busy wait.
So after you first vocalized "please touch screen..." you would start a background thread which waits for a specific amount of time, like so:
new Thread(new Runnable() {
public void run() {
Thread.sleep(3000);
runOnUiThread(new Runnable() {
#Override
public void run() {
//vocalize
}
});
}
}).start();
As you can see, from within the thread a new runnable is started after 3 seconds which again runs on the UI Thread. This, because I think I remember that you should make such sound-things (depending on your method of how to play the file / sound) only from the UI Thread.
However, this is just an idea and I could not test-run my code!
But I hope I inspired you!
Regards
Me

Cannot hide button using setVisibility and Handler

I am having a problem I've been working for too long now. I'm trying to show a button, and after a delay, hide it.
birdBubble.setVisibility(vis);
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
birdBubble.setText("blalb alba");
handler.postDelayed(this, 2000);
birdBubble.setVisibility(invis);
}
});
birdBubble.setVisibility(invis);
i am doing all of this in a AsyncTask because i need to show a sequence of buttons. What it happens is that at the beginning the button is shown and after 2 seconds, the text is changed, but the button doesn't turn INVISIBLE. Any ideas? If you need more code, let me know. Thanks!
set the visilibility like following
birdBubble.setVisibility(View.VISIBLE);
birdBubble.setVisibility(View.INVISIBLE);
birdBubble.setVisibility(View.GONE);

Shorter delay in android slideshow

So im making a slideshow in android that i want to move very fast to look almost as though an animation. Bellow is the code that i am using but i want it go move faster(smaller delay) but i cant make the postDelay any smaller. How would i do this? If its not possible what would a better way to do this be?
private Runnable runnable = new Runnable() {
public void run() {
myslideshow();
handler.postDelayed(this, 1);
}
};
private void myslideshow()
{
if (position < imageIDs.length){
iv.setImageResource(imageIDs[position]);
position++;
}
else{
iv.setImageResource(R.drawable.logo);
}
}
Could the Android Animation API help instead?
You can use a gallery. On click of some button or anything you can run a for loop to
display next element in the gallery till the last element (image in your case) is displayed

How to set an image with a delay

I have an "Activity" with three images. When one image is clicked, all the images are to switch to another picture. Is there a way to make the switches such that there is a 2 second delay before the images actually change (i.e. a 2 second delay in every 'for loop' below)? I am trying to do this with a timer but it does not actually to the delay when I run my program:
protected void onCreate(Bundle savedInstanceState) {
image1.setOnClickListener(this);
#Override
public void onClick(View arg0)
{
do_switches();
}
private void do_switches()
{
//loop through all images and change them
for(int j=1 ;j<=3; j++)
{
final int curr2=current;
final Handler handler = new Handler();
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
switch(curr2){
case 1:
image1.setImageResource(ImageArray[1]);
break;
case 2:
image2.setImageResource(ImageArray[2]);
break;
case 3:
image3.setImageResource(ImageArray[3]);
break;
}
}
});
}
}, 2000);
}
}
I have also tried using just SystemClock.sleep(2000) instead of the timer but I that didnt work either.I also tried setting up a Thread with a try/catch with no luck or maybe I didn't implement it properly. Is there a way to put this delay on every iteration of my for loop?
Thanks
Not one of best option, but still you can try CountDownTimer.
http://developer.android.com/reference/android/os/CountDownTimer.html
You can use handler.postDelayed(Runnable r, long timeInMillis). Make your runnable that changes the pictures and then call postDelayed() and pass in the runnable and 2000 for the time delay.
Edit: Ahh I see what you are trying to do. As far as I know you aren't going to be able to make a for loop pause for 2 seconds each time through. You can get the same effect though if you chain the postDelayed() calls. Just set up the next runnable and call postDelayed() on it inside the first one, and same for the third one from inside the second one. You will end up with the same functionality as a for loop that pauses for 2 seconds each iteration.

Categories

Resources