How can I have an animation before I finishing my activity? - android

My scale/zoom animation won't start with the finish(). I hope there's a way to do it without deleting the said statement because it is necessary not to go back to the current activity.
Here's the code:
package com.capstone.mainmobidyx.filipino;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import com.capstone.mainmobidyx.R;
public class F_FilipinoYunit1 extends Activity implements OnClickListener {
Button btnLesson1, btnLesson2, btnLesson3;
Intent lesson1, lesson2, lesson3;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.f_yunit1);
btnLesson1 = (Button) findViewById(R.id.btnY1Lesson1);
btnLesson1.setOnClickListener(this);
btnLesson2 = (Button) findViewById(R.id.btnY1Lesson2);
btnLesson2.setOnClickListener(this);
btnLesson3 = (Button) findViewById(R.id.btnY1Lesson3);
btnLesson3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Animation zoomAnim = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
v.startAnimation(zoomAnim);
switch (v.getId()) {
case R.id.btnY1Lesson1:
lesson1 = new Intent(v.getContext(), SX_Lesson01.class);
startActivity(lesson1);
finish();
break;
case R.id.btnY1Lesson2:
lesson2 = new Intent(v.getContext(),
SX_ScienceLesson02Menu.class);
startActivity(lesson2);
finish();
break;
case R.id.btnY1Lesson3:
lesson2 = new Intent(v.getContext(),
SX_Lesson03Menu.class);
startActivity(lesson2);
finish();
break;
}
}
}
SOLUTION:
Using the method onAnimation end. I can now start the animation before finishing the activity by inserting the codes inside the method.
here is the working code:
#Override
public void onClick(final View v) {
// TODO Auto-generated method stub
final Animation zoomAnim = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
zoomAnim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation anim) {
switch (v.getId()) {
case R.id.btnY1Lesson1:
lesson1 = new Intent(v.getContext(), SX_Lesson01.class);
startActivity(lesson1);
finish();
break;
case R.id.btnY1Lesson2:
lesson2 = new Intent(v.getContext(),
SX_ScienceLesson02Menu.class);
startActivity(lesson2);
finish();
break;
case R.id.btnY1Lesson3:
lesson2 = new Intent(v.getContext(),
SX_Lesson03Menu.class);
startActivity(lesson2);
finish();
break;
}
}
}
public void onAnimationRepeat(Animation arg0) {}
public void onAnimationStart(Animation arg0) {}
});
v.startAnimation(zoomAnim);

Set an animation end listener on the animation and finish() / startActivity() when the animation has ended :
http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
example :
final Animation animation = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation anim) {
// Start your new activity and finish() the current activity here!
}
public void onAnimationRepeat(Animation arg0) {}
public void onAnimationStart(Animation arg0) {}
}
animation.startAnimation()

Related

Reload Activity in Android Studio

Can you please help me about this game that I created, I want to reload the same activity, like restart game in quiz game. I hope you guys can help me, this is the code:
package com.example.splash;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.widget.ImageViewCompat;
import android.content.Intent;
import android.media.Image;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class stagenumbereasy1 extends AppCompatActivity {
Button congratsback,congratshome,congratsforward,reload;
ImageView congrats,tryagain;
Animation fromsmall,fromnothing;
MediaPlayer click,congrats1,die;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_stagenumbereasy1);
click = MediaPlayer.create(this, R.raw.click);
congrats1 = MediaPlayer.create(this, R.raw.congrats);
die = MediaPlayer.create(this, R.raw.die);
final Button butt8=(Button)findViewById(R.id.button8);
final Button butt9=(Button)findViewById(R.id.button9);
final Button butt10=(Button)findViewById(R.id.button10);
final Button butt11=(Button)findViewById(R.id.button11);
final Button back=(Button)findViewById(R.id.button12);
final LinearLayout overbox=(LinearLayout)findViewById(R.id.overbox);
final LinearLayout congratsbox=(LinearLayout)findViewById(R.id.congratsbox);
final LinearLayout tryagainbox = (LinearLayout)findViewById(R.id.tryagainbox);
final LinearLayout trybox=(LinearLayout)findViewById(R.id.trybox);
fromsmall = AnimationUtils.loadAnimation(this, R.anim.fromsmall);
fromnothing = AnimationUtils.loadAnimation(this, R.anim.fromnothing);
congratsbox.setAlpha(0);
overbox.setAlpha(0);
tryagainbox.setAlpha(0);
trybox.setAlpha(0);
congratsback=(Button)findViewById(R.id.congratsback);
congratshome=(Button)findViewById(R.id.congratshome);
congratsforward=(Button)findViewById(R.id.congratsforward);
congrats=(ImageView)findViewById(R.id.congrats);
reload=(Button)findViewById(R.id.reload);
tryagain=(ImageView)findViewById(R.id.try1);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent back=new Intent (stagenumbereasy1.this,stagenumber.class);
click.start();
startActivity(back);
}
});
butt8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
click.start();
congrats1.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
overbox.setAlpha(1);
overbox.startAnimation(fromnothing);
congratsbox.setAlpha(1);
congratsbox.startAnimation(fromsmall);
}
});
congratshome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent home = new Intent(stagenumbereasy1.this, Home.class);
click.start();
startActivity(home);
}
});
congratsforward.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent easy2 = new Intent(stagenumbereasy1.this, stagenumbereasy2.class);
click.start();
startActivity(easy2);
}
});
butt9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reload = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(reload);
finish();
}
});
}
});
butt10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reload = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(reload);
finish();
}
});
}
});
butt11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent refresh = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(refresh);
finish();
}
});
}
});
}
}
Image of my game
I actually create another activity to make it happens but when I click the reload button it is going back to home. This is my thesis game.
I don't know if exists a recommended answer in Android docs. But, I think you can call startActivity() and finish(), like:
// Java
startActivity(new Intent(MyActivity.class));
finish();
// Kotlin
startActivity(Intent(MyActivity::java.class));
finish();
This will create a new instance of the activity and destroy the current.
For better performance, you should make tests with Fragments instead resetting an Activity.
You could try recreate(); which would recreate the activity
You could also try Intent intent = new Intent(MyActivity.this, MyActivity.class);
startActivity(intent);
Just simply use intent flags one is INTENT_FLAGS_NEW_TASK
it restart activity
Intent.setflags()
In your current activity recall it
Intent(main.this,main.class)

back button shows blank screen

When I press the 'back' button of a mobile-phone it shows me a blank page. When I again press the 'back' button it then shows me the main page of the application. I want to get to the mainActivity page on first back press.
mainActivity code:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.style.SuperscriptSpan;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button b1,b2,b3,b4,b5,b6,b7;
EditText e1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.call1);
e1=(EditText)findViewById(R.id.call);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String no=e1.getText().toString();
Intent call=new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:"+no));
startActivity(call);
}
});
b2=(Button)findViewById(R.id.sens_sms);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sendsms=new
Intent(getApplicationContext(),waytosms.class);
startActivity(sendsms);
}
});
b3=(Button)findViewById(R.id.facebook);
b3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,facebook.class);
startActivity(facebook);
finish();
}
});
b4=(Button)findViewById(R.id.gmail);
b4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,gmail.class);
startActivity(facebook);
}
});
b5=(Button)findViewById(R.id.utube);
b5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,utube.class);
startActivity(facebook);
}
});
b6=(Button)findViewById(R.id.google);
b6.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,google.class);
startActivity(facebook);
}
});
b7=(Button)findViewById(R.id.twitter);
b7.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,twitter.class);
startActivity(facebook);
}
});
}
}
This is the Facebook class I have used here with webview to open the Facebook page:
public class facebook extends Activity{
WebView facebook;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook);
facebook=(WebView)findViewById(R.id.webfacebook);
facebook.getSettings().setJavaScriptEnabled(true);
facebook.loadUrl("https://facebook.com");
}
Here I have implemented onbackPressed() and called finished() The onbackpressed method isn't affecting this at all.
#SuppressLint("NewApi")
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
Intent i=new Intent(facebook.this,MainActivity.class);
startActivity(i);
}
}
1> first is main activity page where there are icon of social sites when
i click on facebook icon it will open facebook as 2nd facebookActivity page,3> when i clicked back it will open a blank page and4>. nd when i again press back button it will opened the main activity page...
Change your onBackPressed() method like this:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}

Call New Activity from MAin Activity

I want, that when you click to open a new screen (Activity), but have a problem, the new screen does not open. What changes are made ​​to the code, so that the new window opened?
package com.android.yalt;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
Button btnny;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnny = (Button) findViewById(R.id.ny);
btnny.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ny:
startActivity(NY.class);
break;
default:
break;
}
}
private void startActivity(Class<NY> class2) {
// TODO Auto-generated method stub
}
}
You have to use
startActivity(new Intent(getBaseContext(), NY.class));
or
Intent i = new Intent(getBaseContext(), NY.class);
startActivity(i);
or In a method format:
public void startMyActivity(Class activityToStart){
startActivity(new Intent(getBaseContext(), activityToStart.class));
}
Just as a sidenote, one can replace getBaseContext() with your own context if one wants to e.g. startActivity(new Intent(yourOnGoingActivityGoesHere.this, yourClassGoesHere.class));
Also as a utility:
/*This is not tested but should work */
public class AndroidUtility(){
/**
*Usage: AndroidUtility.startMyActivity(MainActivity.this, NY.class);
*/
public static void startMyActivity(Activity onGoingActivity, Class activityToStart){
onGoingActivity.startActivity(new Intent(onGoingActivity.getBaseContext(), activityToStart.class));
}
}
In your case the easiest would be:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ny:
startActivity(new Intent(getBaseContext(), NY.class));
break;
default:
break;
}
}

Exit app when back pressed

sorry for this again button i ant figure out the correct placement here. i just want it on this class only to exit the app when back pressed but i cant figure out what exactly to write or where to write it. thanks for the help. code of class below.
package com.example.whattodo2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class Title extends Activity {
Button reset, rts;
ImageView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title);
reset = (Button) findViewById(R.id.reset);
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
double rand = Math.random();
if(rand < 0.5){
Intent reset1 = new Intent(Title.this, MainActivity.class);
startActivity(reset1);
} else {
Intent reset2 = new Intent(Title.this, Question36.class);
startActivity(reset2);
}
}
});
rts = (Button) findViewById(R.id.rts);
rts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent rts=new Intent(Title.this,Rts.class);
startActivity(rts);
}
});
final Animation a = AnimationUtils.loadAnimation(this, R.animator.animation);
a.reset();
final ImageView rImage = (ImageView) findViewById(R.id.title);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
layout.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
rImage.startAnimation(a);
func(); //A call to the function.
}
});
}
protected void func() {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.title, menu);
return true;
}
}
Use like this:
public class Title extends Activity {
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
super.onBackPressed();
}
}
hope this will give you some solution.
Before onCreate in all the activities:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent intent=new Intent (Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// finish();
}
return true;
}

Android - RotateAnimation onClick button restart itself

this is the java code to rotate an image when click on button. Image rotate it's perfect but if I click button again when rotation is not ended the animation restart itself, not end the animation. How can I wait the end of the animation? I found Animation.AnimationListener, I think that onAnimationEnd works great for me, but I'm not be able to integrate it in my code... please help me :-)
package com.example.helloword;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class Rotation_test extends Activity {
private float statdegree = (float) 0.0;
private float enddegree = (float) 90.0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rotation_test);
// ---------------------------------------------------------------------------------------------
Button buttonRotateCenter = (Button) findViewById(R.id.rotatecenter);
final ImageView floatingImage = (ImageView) findViewById(R.id.floatingimage);
// AnimationRotation
final Animation animationRotateCenter = new RotateAnimation(statdegree,
enddegree, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animationRotateCenter.setDuration(5000L);
animationRotateCenter
.setInterpolator(new AccelerateDecelerateInterpolator());
// \AnimationRotation
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
floatingImage.startAnimation(animationRotateCenter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_rotation_test, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
You have to take a boolean variable which tracks whether animation is in progress or not. then use this variable in animation listener and button click as below code
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
if(!anim_in_progress)
floatingImage.startAnimation(animationRotateCenter);
}
});
animationRotateCenter.setAnimationListener(new anim_listener());
}
boolean anim_in_progress=false;
class anim_listener implements AnimationListener
{
#Override
public void onAnimationEnd(Animation arg0) {
anim_in_progress=false;
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
anim_in_progress=true;
}
}
Never tried this, but might work
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
if(!floatingImage.hasTransientState())
floatingImage.startAnimation(animationRotateCenter);
}
});

Categories

Resources