Am quite new to Android, I could not found a thread on how to start new activity on anim end.
Here is my code:
public class Intro extends Activity {
AnimationDrawable anim = new AnimationDrawable();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
ImageView iv =(ImageView) findViewById(R.id.imageView1);
iv.setBackgroundResource(R.anim.animation);
anim = (AnimationDrawable) iv.getBackground();
iv.post(new Runnable(){
public void run(){
anim.start();
}
});
}
}
As far as I know there is no callback mechanism to determine when an AnimationDrawable has finished an iteration. What you can do is to query the number of frames and the delay of each frame. Sum that up and make a delayed post with that duration. In that callback you can start your second activity.
And note that it is a bit dangerous to start the animation the way you do it. The docs say:
It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.
Related
I was wondering how to make an animation repeat after comeback on the specific page. My animation basically translates a RelativeLayout a few pixels down when you visit a certain activity. But if I then click on a button that sends me on a different page and then hit the back button to return, the TranslationAnimation doesn't start again.
Here is the code:
RelativeLayout r1;
r1 = findViewById(R.id.r1);
TranslateAnimation a = new TranslateAnimation(0,0,-10f,0);
a.setDuration(800);
a.setFillAfter(true);
r1.startAnimation(a);
How exactly do I make this animation restart every single time I visit my activity?
Make separate method in activity
void myanimation(){
TranslateAnimation a = new TranslateAnimation(0,0,-10f,0);
a.setDuration(800);
a.setFillAfter(true);
r1.startAnimation(a);
}
then call the method inside of activity onResume
#Override
public void onResume(){
super.onResume();
myanimation();
}
use this inside your onResume() method. OnResume calls every time when you interact with your activity.
I'm having a strange problem to do with stopping my android app. On my phone I have a home button and a back button, now when I go into my app after pressing the home button, the program loads data from the internet as expected, but when I go into my app after pressing the back button, the data doesn't load. I've debugged it to an extent, and have found out that the only difference is that the back button calls the onCreate() method. I'm quite confused to why this is is happening.
Here's some of my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("DAP", "Created");
setContentView(R.layout.activity_ltc);
getActionBar().setTitle("LTC Charts");
getActionBar().setLogo(
getResources().getDrawable(R.drawable.new_litecoin_logo_large));
TextView textView = (TextView) findViewById(R.id.ltcdata);
textView.setText("Loading data...");
TimerTask timer = new TimerTask() {
#Override
public void run() {
parseJSON();
}
};
Timer time = new Timer();
time.schedule(timer, 500, 85);
}
"when I go into my app after pressing the back button, the data doesn't load."
If you have already launched your app, the Activity will be paused (and onPause is called) when you navigate away from it. When you navigate back to the app, the same activity instance is resumed (and onResume is called).
See http://developer.android.com/training/basics/activity-lifecycle/index.html
This is because your activity hasnt been destroyed. What you should do is put something into the onresume of your activity to get the data again when you come back to the activity. If you want the data to be destroyed for sure and the user never leaves the activity you can destroy everything in onpause.
As others said, override onResume().
A common pattern would be to extract common initialisation code into a method, and call it from where needed:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
#Override
protected void onResume(){
super.onResume();
init();
}
private void init(){
setContentView(R.layout.activity_ltc);
getActionBar().setTitle("LTC Charts");
// ....
}
This could be because your activity is not set correctly at AndroidManifest.xml .
Make sure the activity name is the right one. If you inserted your activity into some package include that name as well . for example I have my activitu which is called "SettingsActivity" (usually the default is MainActivity) set in a package called Activities :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<activity android:name=".Activities.SettingsActivity">
...
Use the on resume function below.
#Override
public void onResume(){
//will be executed onResume
}
Im trying to make a timer for a game, and I'm stuck atm.
This is my code so far. I made a new activity for the intent, cause I read somewhere that's one way to do it.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Timer timer = new Timer();
//task = new TimerTask() {
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent("com.loltimer.Timer"));
}
});
};
}
When I press the first button I want to have a timer that goes from 5minutes down to 0. And at 0 I want a sound to be played.
Thanks in advance!
Has got to be the Android count down timer class: http://developer.android.com/reference/android/os/CountDownTimer.html
Timing operations are best handled by a Handler in Android.
http://developer.android.com/reference/android/os/Handler.html
You can wrap the operation you want to run in the future as a Runnable and pass it to postDelayed(). If the operation needs to run repeatedly on that interval, you can call postDelayed() at the end of the Runnable to schedule the next run.
CountdownTimer can also be useful if you need to run some code a finite number of times on a regular interval. This class is actually just a thin wrapper around Handler.
I'm starting a project and I want to reproduce a video in the main activity when the app is executed, when the user presses the video it goes to another activity. If the user press the back button, he is going to the main screen again and reproduces the video from the beginning. The video is located in the raw directory.
The problem is that the videoview is reproducing the video when the activity is first created but not when the user goes back to it from the other activity (In my case the MenuSection activity). The code is really simple but i will paste it anyway:
public class MainActivity extends Activity {
private VideoView mVideoView;
LinearLayout menuSection;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.documentariesandyou));
mVideoView.requestFocus();
mVideoView.setMediaController(null); //i dont want the controls of the videoview.
mVideoView.start();
menuSection = (LinearLayout) findViewById(R.id.menuSection);
menuSection.setOnClickListener(new menuSectionListener());
}
class menuSectionListener implements OnClickListener {
public void onClick(View v) {
Intent staticActivityIntent = new Intent(MainActivity.this, MenuSection.class);
startActivity(staticActivityIntent);
}
}
}
The MenuSection is just an activity that shows a textview like "Hello world", so I'm not pasting it.
Move mVideoView.start(); to onResume() instead of onCreate() as:
#Override
protected void onResume() {
super.onResume();
mVideoView.start();
}
see Managing the Activity Lifecycle onResume() is called from your Activity when Activity is Already Running
call video.pause() on onPause() overrided method of your activity and call video.resume() on onResume() method of your activity.
Move mVideoView.start(); to onStart(), instead of onCreate().
See Activity Lifecycle in the developer docs for more info on how the lifecycle of Activities work.
I am not certain, but you may also need to move the setVideoURI(); to onStart() as well.
The example is pretty straightforward: i want to let the user know about what the app is doing by just showing a text (canvas.drawText()). Then, my first message appears, but not the other ones. I mean, i have a "setText" method but it doesn't updates.
onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(splash); // splash is the view class
loadResources();
splash.setText("this");
boundWebService();
splash.setText("that"):
etc();
splash.setText("so on");
}
The view's text drawing works by doing just a drawText in onDraw();, so setText changes the text but doesn't show it.
Someone recommended me replacing the view with a SurfaceView, but it would be alot of trouble for just a couple of updates, SO... how the heck can i update the view dinamically at runtime?
It should be quite simple, just showing a text for say 2 seconds and then the main thread doing his stuff and then updating the text...
Thanks!
Update:
I tried implementing handler.onPost(), but is the same story all over again. Let me put you the code:
public class ThreadViewTestActivity extends Activity {
Thread t;
Splash splash;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
splash = new Splash(this);
t = new Thread(splash);
t.start();
splash.setTextow("OA");
try { Thread.sleep(4000); } catch (InterruptedException e) { }
splash.setTextow("LALA");
}
}
And:
public class Splash implements Runnable {
Activity activity;
final Handler myHandler = new Handler();
public Splash(Activity activity) {
this.activity=activity;
}
#Override
public void run() {
// TODO Auto-generated method stub
}
public synchronized void setTextow(final String textow) {
// Wrap DownloadTask into another Runnable to track the statistics
myHandler.post(new Runnable() {
#Override
public void run() {
TextView t = (TextView)activity.findViewById(R.id.testo);
t.setText(textow);
t.invalidate();
}
});
}
}
Although splash is in other thread, i put a sleep on the main thread, i use the handler to manage UI and everything, it doesn't changes a thing, it only shows the last update.
I haven't hit this yet, but I think the usual pattern is to do lengthy initialization in a background thread, and use Handler.post() to update the UI. See http://developer.android.com/reference/android/widget/ProgressBar.html for a different, but possibly related, example.
Also see this answer, especially the first paragraph:
The problem is most likely that you
are running the splash screen (some
sort of Dialog such as ProgressDialog
I assume) in the same thread as all
the work being done. This will keep
the view of the splash screen from
being updated, which can keep it from
even getting displayed to the screen.
You need to display the splash screen,
kick off an instance of AsyncTask to
go download all your data, then hide
the splash screen once the task is
complete.
Update (based on your update and your comment): You are not supposed to update the UI in any thread except the one where your Activity is created. Why is it impossible for you to load your resources in a background thread?
First: onCreate is executed on main UI thread of application so no UI updates until you leave it. Basically you need one thread to execute long running tasks and some mechanism to push updates into the UI.
Most usual approach is to extend AsyncTask see this link for further info
i suppose that your view is an extended view and you call onDraw for drawing the view, so, maybe the view isnĀ“t 'refresh' their state, so try this
onCreate(Bundle bundle) {
setContentView(splash); // splash is the view class
loadResources();
splash.setText("this");
splash.invalidate();
boundWebService();
splash.setText("that"):
splash.invalidate();
etc();
splash.setText("so on");
splash.invalidate();
}