Starting the animation when the activity starts - android

My animation is running on the button press, now i want the animation to start when the activity starts. Following is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animationStart();
Button onButton = (Button) findViewById(R.id.button1);
onButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
animationStart();
}
});
}
private void animationStart() {
ImageView imageanimate = (ImageView) findViewById(R.id.imageView1);
imageanimate.setBackgroundResource(R.drawable.ball_animation);
animation = (AnimationDrawable) imageanimate
.getDrawable();
if (animation.isRunning()) {
animation.stop();
}
animation.start();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
animationStart();
super.onStart();
}
}
I have also tried to initialize the animation in onstart method but it is now working.
I want that when the acitivity starts the animation should play. Anybody could please guide me the right way to do it as it is not working. The animation ONLY works with the button press.

If you want to start animation in onCreate or onStart, you should start animation in handler with 1000-2000 ms postdelay which will give enough time to initialize imageview.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//Start your animation here
}
},1000);
}

I do not test it, I hope it will help you.
protected void onCreate(Bundle savedInstanceState) {
//....
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
animationStart();
}
},0);
//...
}

private boolean mDoStartingAnim;
public void onCreate(Bundle savedInstanceState) {
// ...
mDoStartingAnim= true;
// ...
}
public void onResume() {
if (mDoStartingAnim) {
animationStart();
mDoStartingAnim = false;
}
}
I hope it will help you..

Related

How To Call this Animation in onCreate() in Android?

I am following this link for my animation Requirement.
https://medium.com/#ayushpguptaapg/how-to-add-shine-glare-effect-on-an-imageview-ab3e9e660307
This has done on button click listener. Which is working fine. But i want to do it without button click as this animation starts as the activity load.
I have searched alot, but didn't find any simple solution.
Can anybody tell me the simple solution.
Thanks in advance.
#ismail, i am using your this code, like this:
public class MainActivity extends AppCompatActivity{
Button shinebtn;
ImageView img,shine;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.img);
shine = findViewById(R.id.shine);
shinebtn=findViewById(R.id.button);
doAnimation();
}
public void doAnimation(){
new Thread(new Runnable() {
public void run() {
final Animation animation = new TranslateAnimation(0, img.getWidth()+shine.getWidth(),0, 0);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setRepeatCount(Animation.INFINITE);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
shine.post(new Runnable() {
public void run() {
shine.startAnimation(animation);
}
});
}
}).start();
}
}
just write the code after setting your elements in onCreate()
#Override
protected void onCreate(Bundle SavedInstanceState){
super.onCreate(SavedInstanceState);
setContentView(yor_layout_source);
....
....
}
#Override
public void onResume(){
super.onResume();
//do your animation code here
}
and here your animation function :
public void doAnimation(){
new Thread(new Runnable() {
public void run() {
Animation animation = new TranslateAnimation(0, img.getWidth()+shine.getWidth(),0, 0);
animation.setDuration(550);
animation.setFillAfter(false);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
shine.post(new Runnable() {
public void run() {
shine.startAnimation(animation);
}
});
}
}).start();
}

I have a problem with Intent in my splash screen

i build app when open the app splash screen working but after 3 second Instead of going to another page, it closes the application
my codes are:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
ImageView iv = findViewById(R.id.iv);
Animation myanim = AnimationUtils.loadAnimation(this, R.anim.mytransition);
iv.startAnimation(myanim);
final Intent intent0 = new Intent(Menu.this, Fehrest.class);
Thread timer;
timer = new Thread() {
public void run () {
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
startActivity(intent0);
finish();
}
}
};
timer.start();
}
}
I dont know this will be helping you, maybe you can try change your code like this or you can add your logcat error for making error is clear
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
ImageView iv = findViewById(R.id.iv);
Animation myanim = AnimationUtils.loadAnimation(this, R.anim.mytransition);
iv.startAnimation(myanim);
// redirect to another activity
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(Menu.this, Fehrest.class));
finish();
}
}, 3000);
}
for just test purpose remove finish method from splash Activity. and check your another application close or not. if another Activity close then may you use finish method on another activity.
please try this hope it will help you...
public class MainActivity extends AppCompatActivity {
private ImageView iv;
private static int splashTimeOut=3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.iv);
methodLogin();
}
private void methodLogin() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i=new Intent(getApplicationContext(),Fehrest.class);
startActivity(i);
finish();
}
},splashTimeOut);
Animation myanim = AnimationUtils.loadAnimation(this,R.anim.mytransition);
iv.startAnimation(myanim);
}
}

While press back Button splash screen exits and opens Authentication Activity

I created two Activities "SplashActivity and AuthenticationActivity" When I start the app it loads the splash screen for 5 seconds and then it moves on to AuthenticationActivity. My problem is that when I run the app, it loads the splash screen, but within 5 seconds when I click the back button, SplashActivity exits, and immediately AuthenticationActivity appears in the foreground.
Does anyone know how to make it so when I click the back button, my app exits?
Here's my code so far:
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread splashTimer = new Thread(){
public void run(){
try{
sleep(5000);
startActivity(new Intent(SplashActivity.this,AuthenticationActivity.class));
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
finish();
}
}
};
splashTimer.start();
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
}}
Timer timer;
TimerTask timerTask;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
initialize();
}
private void initialize()
{
timer = new Timer();
timerTask = new TimerTask()
{
#Override
public void run()
{
//Start your activity here
}
};
timer.schedule(timerTask,2500);
}
#Override
public void onBackPressed() {
super.onBackPressed();
timer.cancel();
}
or you can use Handler also
Handler handler;
Runnable runnable;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
initialize();
}
private void initialize()
{
handler = new Handler();
runnable = new Runnable()
{
#Override
public void run()
{
//start your activity here
}
};
handler.postDelayed(runnable, 5000);
}
#Override
public void onBackPressed() {
super.onBackPressed();
handler.removeCallbacks(runnable);
}

how to check if an application is killed during an animation in android

i have to make an application in which it starts with an animation and if we click the back button then it should return back to application manager.But what i have made in it if u click back button during that animation then it goes to application manager but after a second or two the first page(the one after this animation comes up).
Can anyone help??
This is the animation..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load);
im = (ImageView) findViewById(R.id.load_icon);
rotate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.load_page);
rotate.setInterpolator(new LinearInterpolator());
im.startAnimation(rotate);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
}
The first page opens because you have added
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
This launches the activity.For knowing if the animation has stopped use AnimationListener. More details here about animation listener
Android, How to set animation listener for view group?
You just added animation to one image view thats all, you do not doing anything with animation. The problem is, you started one thread to start activity P1 after 3 seconds. That thread only starting P1 activity. Try this and try to avoid killProcess(),
public class LauncherActivity extends Activity {
private Handler mHandler;
private Runnable mRunnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
XmlParserActivity.class);
startActivity(nextPageIntent);
}
};
mHandler.postDelayed(mRunnable, 3000);
}
/* #Override
public void onBackPressed() {
super.onBackPressed();
mHandler.removeCallbacks(mRunnable);
}*/
#Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mRunnable);
}
}
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
}
This is the answer

layout is not shown after waiting

I have three layouts:
Layout1
-->onClick()-->show
Layout2
-->wait three seconds-->show
Layout3
The problem is that Layout2 is not shown. To set the layouts I use
setContentView(int);
The relevant code might be:
public class TrainingActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
final Button inputButton = (Button)findViewById(R.id.inputButton);
inputButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
changeLayouts();
}
});
}
public void changeLayouts() {
setContentView(R.layout.layout2);
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
setContentView(R.layout.layout3);
}
}
My idea was that Android might use something like an "Event-Loop" (like Qt) and my method would block the control to get back to the "Event-Loop" which would make the layout displayed.
But I couldn't find my error.
The problem why your layout2 is not shown is because of TimeUnit.MILLISECONDS.sleep(3000); - what you are doing here is you put your UI thread into sleep, so UI thread cannot process your request to change layout. And when it wakes up - it immediately sets layout3 that's why layout2 is not shown.
You might consider using Handler.postDelayed(Runnable, long) to postpone execution
So this should work as you expected:
public class TrainingActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
final Button inputButton = (Button)findViewById(R.id.inputButton);
inputButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
changeLayouts();
}
});
}
public void changeLayouts() {
setContentView(R.layout.layout2);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.layout3);
}
}, 3000);
}
}
Try this, it will surely work
public void changeLayouts() {
setContentView(R.layout.layout2);
Thread Timer = new Thread(){
public void run(){
try{
sleep(3000);
} catch(InterruptedException e){
e.printStackTrace();
} finally {
setContentView(R.layout.layout3);
}
}
}; Timer.start();
}

Categories

Resources