I'm trying to animate my imagebuttons by making them wobble when clicked.
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonStart:
buttonStart.startAnimation(wobble);
Intent i;
i = new Intent(this, CityRendActivity.class);
startActivity(i);
break;
}
}
When I click my Start button, it very slowly starts to wobble, but only appears to shake back and forth like 3 times, and a lot slower than I have defined in the xml file. When I remove the lines that contain the intent code, the wobble works perfectly and the button shakes 5 times quickly (though now of course it doesn't load up my other activity).
So why would the code after the wobble animation effect how it's run? Doesn't each line get resolved individually before proceeding? I don't understand how loading up a different activity would effect the animation that I've set up. Any thoughts? Thanks.
Try adding animation listener for your animation and onAnimationEnd() start your Activity Intent.
wobble.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationStart(Animation animation){}
#Override
public void onAnimationRepeat(Animation animation){}
#Override
public void onAnimationEnd(Animation animation){
startActivity(new Intent(YourCurrentActivity.this, CityRendActivity.class)); // if the code is in Fragment, then replace YourCurrentActivity.this by getActivity()
}
});
Actually In this code. animation and starting a new activity will execute at a same time. that will disturb the animation in between. so delay the starting of activity with your animation time. so that animation will completed and then your Activity will start.hope this work for you.
new Handler().postDelayed(new Runnable(){
#Overide
void run(){
startActivity(new Intent(this, CityRendActivity.class));
}
},time_ofanimation);
Related
Hello I am making boxing countdown timer and I have the ring that plays at the end and start of each round. I would like to make "end" button but when I am using Intent like bellow I go to previously activity but mp3 files still working in background. How to end all tasks and go to previously activity without any mp3 and tasks in background?
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder dialog=new AlertDialog.Builder(Main3Activity.this);
dialog.setMessage("Czy napewno chcesz wyjść z trenignu?");
dialog.setPositiveButton("Tak", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(Main3Activity.this, Main5Activity.class);
startActivity(intent);
}
});
dialog.setNegativeButton("Nie", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialogg=dialog.create();
dialogg.show();
}
});`
removeEventListener()
use HTML DOM removeEventListener() Method
btn1.removeEventListener("click", function(){});
see: https://www.w3schools.com/JSREF/met_element_removeeventlistener.asp
Your question looks incomplete or incorrect to me.
If you want to go to previous activity from the activity stack, then you should not use intent to start the previous activity, rather finish the current Activity.
If you want to stop playing the bell ring once the user is not interacting with the Main3Activity or when Main3Activity is not visible to the user. Then best practice is to write a code in the onStop() method of the Main3activity to check for the object which is responsible for playing the sound and safely call stop or destroy the object.
simply finish the activity which can destroy the instance of activity and everything that is running on that particular activity. simply write this line after startActivity
finish();
Hey guys I'm trying to make a function that quit my application when onClick on a Button but it doesn't work.
Would you take a look and let me now what's wrong with the code please ?
Here is the code :
public void addListenerOnButtonLeave()
{
quitButton = (ImageView) findViewById(R.id.quitButton);
quitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
moveTaskToBack(true);
}
});
}
I put this function into the overrided onCreate().
Thanks for your help guys.
You may also use finishActivity (int requestCode)
Force finish another activity that you had previously started with
startActivityForResult(Intent, int).
Probably some of your activities are running in background. When you have switched between activities, you have not finished them.
First finish those activities by adding
ActivityName.this.finish()
just before you sail to another activity, and then use
getActivity().finish();
System.exit(0);
to quit the app triggered where needed.
So I am developing Android application in which I have a Home screen and another screen with the content of the app. On home screen I have a button which navigates the user to the content's screen (actually it is image view but used as a button).
When clicked this button should start animation of itself being clicked and then start the activity of the content screen.
But it actually works pretty bad and slow, when I click the button the animation starts but it slows down (lags). I tried changing the event from click to tap and lots of other possible solutions but none worked.
I tried commenting "startActivity" method and this fixed it, the animation was running smoothly. Obviously I need the startActivity method, so what is the best approach here, how can I fix this. Can putting startActivity in different thread work, I'm not sure how to do this if so as well
This is my code:
((Button)findViewById(R.id.btnPlay)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
//shake.reset();
findViewById(R.id.btnPlay).startAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
Intent i;
#Override
public void onAnimationStart(Animation animation) {
i = new Intent(v.getContext(), GameActivity.class);
startActivity(i);
}
#Override
public void onAnimationEnd(Animation animation) {
((Button) findViewById(R.id.btnPlay)).setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
});
shake is an animation loaded from custom xml file. I have the same animation in the content screen and it runs smoothly, of course there is no startActivity there. Here is the code:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="70"
android:fromDegrees="-5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="5"
android:repeatMode="reverse"
android:interpolator="#android:anim/linear_interpolator"
android:toDegrees="5" />
<translate
android:fromXDelta="-10"
android:toXDelta="10"
android:repeatCount="5"
android:repeatMode="reverse"
android:interpolator="#android:anim/linear_interpolator"
android:duration="70" />
Move these lines of code in onAnimationEnd()
i = new Intent(v.getContext(), GameActivity.class);
startActivity(i);
UPDATE
I've just tried this code and it works well for me:
Animation animation = AnimationUtils.loadAnimation(MyApplication.getAppContext(), R.anim.shake);
public void onClick(View v) {
if (v.getId() == R.id.btnPlay) {
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent(getActivity(), ArticleActivity.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
v.startAnimation(animation);
}
}
If the activity starts when the animation of the button starts, there could be too many things (like: button animation, execution of activity lifecycle methods including the lifecycle methods of the fragments, the animation used for transition between activities) which are executed on the main thread => the button animation could get stuck (the animation won't be smooth).
I am trying to implement simple back button on activity to go to previous activity, but some weird behavior is happening when I am calling method finish() - the problem is I have to press back button TWO times? Why is this happening? In the back method i have only finish(). What is the other way to go to previous saved activity without instantiate a completely new Intent?
public void back1 (View view){
this.finish();
}
This is second try with the same result:
ImageButton buttonback = (ImageButton) findViewById(R.id.imageButton6);
buttonback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
You are most likely starting the activity twice.
When pressing back you are finishing one, and the other one is coming forward. Check your onCreate and onResume for anything that may be using intents or starting any activity
#Override
public void onBackPressed() {
super.onBackPressed();
}
add that to your code the super call closes the activity no need to call finish()
If you just want the devices back button to function, you dont need to override the onBackPressed method in Activity.
If you want to place a custom button in view and set back action to that button, then you need to write button clicklistener to the same
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
I want the background music to repeat as long as the user stays in a particular screen. Is there a function in the MediaPlayer class that allows you to do the above task?
button1.setOnClickListener(new View.OnClickListener() {
#Overridepublic void onClick(View v) {
Intent intent = new Intent(context,kids_quiz.class);
startActivity(intent);
player=MediaPlayer.create(Games.this,R.raw.macdonald);
player.start();
}
});
Just start it when you are in that Activity, setLooping to true, and when the user leaves the screen destroy the mediaPlayer in Activity.onDestroy(and maybe onPause depending on what you want to accomplish) method.
Remember it is important to destroy a mediaPlayer when you're done with it as it consumes a lot of resources.