0

I've been trying to add a few second delay to my activity swap in my basic app but whenever I try I get errors. I'm basing my attempts off of this previous thread (How to put some delay in calling an activity from another activity?) but have been unsuccessful multiple times. Can anyone help?

Here's my main activity code:

package winfield.joe.wind.v1;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {

    // public var
    private EditText text;

    // default function
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //localise this
        Toast.makeText(this, "onCreate!!", Toast.LENGTH_LONG).show();
        setContentView(R.layout.activity_main);
        // (EditText) = typecast
        text = (EditText) findViewById(R.id.userInput);
    }

        //Will be executed by clicking on the calculate button because we assigned "calc" to the "onClick" Property
        public void calc(View view) {

                    bounce(view, 0.95f);           

            RadioButton toKilometers = (RadioButton) findViewById(R.id.toKilometers);
            RadioButton toKnots = (RadioButton) findViewById(R.id.toKnots);

            if (text.getText().length() == 0) {
                // if the text field is empty show the message "enter a valid number" via toast message
                //ATTENTION:localise this
                Toast.makeText(this, "enter a valid number", Toast.LENGTH_LONG).show();
            } else {

                //int userInput = R.string.userInput;
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                //putExtra("userInput", userInput); 
                startActivity(i);

                // parse input Value from Text Field
                double inputValue = Double.parseDouble(text.getText().toString());
                // convert to...
                if (toKilometers.isChecked()) {
                    text.setText(String.valueOf(convertToKM(inputValue)));
                    // uncheck "to km" Button
                    toKilometers.setChecked(true);
                    // check "to knots" Button
                    toKnots.setChecked(false);
                } else { /* if toKnots button isChecked() */
                    text.setText(String.valueOf(convertToKnots(inputValue)));
                    // uncheck "to knots" Button
                    toKnots.setChecked(true);
                    // check "to km" Button
                    toKilometers.setChecked(false);
                }

            }

        }

    //bounce animation on button on-click
    public void bounce(View view, float amount) {
    ScaleAnimation anim = new ScaleAnimation(amount, 1f, amount, 1f);
    anim.setDuration(750);
    anim.setInterpolator(new BounceInterpolator());
    view.startAnimation(anim);
    }

    private double convertToKM(double inputValue) {
        // convert knots to km
        return (inputValue * 1.8);
    }

    private double convertToKnots(double inputValue) {
        // convert km to knots
        return (inputValue * 0.539956803);
    }

}
Community
  • 1
  • 1
  • At which point do you want to introduce the delay? Do you want to add the delay so that the animation is finished or is it somewhere else? – kha Mar 19 '15 at 15:10
  • The animation only takes a second or so, i just wanted the delay to last say 5 seconds to give the user a chance to read the result (which appears onscreen from 'calc' ) and then go to the second activity. Everything atm works fine, just need to add a delay. Hope that clears it up. – joewinfield91 Mar 19 '15 at 15:15
  • Ah ok. In that case handler.postDelayed() (answer below) is what you're after. – kha Mar 19 '15 at 15:18

2 Answers2

2

You could use an Handler:

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {

            if(...) {
                // start here the other Activity
            }

            else {

                // do something else

            }

        }
    }, YOUR_TIME_OUT);
andreasperelli
  • 1,034
  • 2
  • 11
  • 40
  • Thanks for the answer. I've been looking at Handler but it causes an awful lot of errors when I try implementing it. I tried your way but I get about 5 errors which seems to be because of how my codes laid out. Is there another way would you know? Thanks. – joewinfield91 Mar 19 '15 at 15:16
  • can you show here the code you've used with Handler ? – andreasperelli Mar 19 '15 at 15:17
  • Yes. I've tried the following from another thread (see this posts description) . `new Handler().postDelayed(new Runnable() { @Override public void calc() { //other code here Intent i = new Intent(MainActivity.this, SecondActivity.class); startActivity(i); //other code here } }, 5000);` – joewinfield91 Mar 19 '15 at 15:24
  • Actually, its what you've done above! sorry didn't spot that straight away. It leads to alot of errors though. – joewinfield91 Mar 19 '15 at 15:26
  • Actually in the above code posted by joewinfield91 There is no run method – likith sai Mar 19 '15 at 15:34
  • you should remove the calc() method from the Handler body... it's useless... just call directly Intent i = new Intent(MainActivity.this, SecondActivity.class); startActivity(i); – andreasperelli Mar 19 '15 at 15:34
  • yes @likithsai... it was changed the run() method with the calc() one – andreasperelli Mar 19 '15 at 15:35
  • My errors consist of 12 similar errors like :Syntax error on token ",", ; expected – joewinfield91 Mar 19 '15 at 15:37
  • @joewinfield91 the method inside the Handler should be run() and not calc() – andreasperelli Mar 19 '15 at 15:38
  • Hmm, it doesn't seem to be that easy. I'm seeing an infinite loop of insert/delete }'s at the very end. My code isn't happy one bit with this way. – joewinfield91 Mar 19 '15 at 15:51
0

Try this code

  new Handler().postDelayed(new Runnable() { 
        @Override public void run() { 
            //other code here Intent i = new Intent(MainActivity.this,SecondActivity.class); 
            startActivity(i); //other code here } }, 5000);
likith sai
  • 527
  • 1
  • 6
  • 21