Reputation: 1
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);
}
}
Upvotes: 0
Views: 1515
Reputation: 547
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);
Upvotes: 0
Reputation: 1034
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);
Upvotes: 2