i have a question game that has time limit. how can i code if else= if he answer it correct he will intent to the next level and if he didnt answer it within 10sec he will be intent in another class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easyone);
a1 = (Button) findViewById(R.id.btn_ea1);
b1 = (Button) findViewById(R.id.btn_eb1);
c1 = (Button) findViewById(R.id.btn_ec1);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"CORRECT!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),EasyTwo.class);
startActivity(intent);
}
});
}
}
*\
new CountDownTimer(3000, 100) {
public void onFinish() {
}
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
}.start();
Related
i have 2 activities. and 2 different background music plays on both. when i press a button on the first activity it will go to this activity and the changing of music goes smoothly but i want to stop background music when back button is pressed. i tried this but it force closes. is there any other way to do this?
public class Categories extends Activity{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.categories);
mp = MediaPlayer.create(Categories.this, R.raw.looney);
mp.setLooping(true);
mp.start();
Button cartoonButton = (Button) findViewById(R.id.cartoon);
cartoonButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchCartoon = new Intent(Categories.this, Cartoon.class);
startActivity(launchCartoon);
}
});
Button superButton = (Button) findViewById(R.id.hero);
superButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent launchSuperheroes = new Intent(Categories.this, SuperHeroes.class);
startActivity(launchSuperheroes);
}
});
Button singerButton = (Button) findViewById(R.id.singer);
singerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchSinger = new Intent(Categories.this, Singer.class);
startActivity(launchSinger);
}
});
Button famousButton = (Button) findViewById(R.id.famous);
famousButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchFamous = new Intent(Categories.this, Famous.class);
startActivity(launchFamous);
}
});
}
#Override
protected void onResume() {
super.onResume();
mp.start();
}
#Override
protected void onPause() {
super.onPause();
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
You can try this code:
#Override
public void onBackPressed() {
if (player.isPlaying()) {
player.stop();
}
player.release();
super.onBackPressed();
}
Hope this help you :)
I'm trying to cancel the countdowntimer on onbackpressed, I put counter.cancel; there but "'count' cannot be resolved" error appears. I think It can't find the countdowntimer!
There is a btn_riazi1_1_1 that can cancel the timer.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_riazi1_1);
//// handling timer
final TextView textic = (TextView) findViewById(R.id.riazi1_1_timer);
final CountDownTimer Count = new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
textic.setText(getResources().getString(R.string.remaintime) + millisUntilFinished / 1000);
}
public void onFinish() {
textic.setText(getResources().getString(R.string.timesup));
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i=new Intent(Riazi1_1.this,Riazi1_2.class);
startActivity(i);
}
}, 1000);
}
};
Count.start();
/////////////////
//// handling button1
Button btn1 = (Button) findViewById(R.id.btn_riazi1_1_1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Count.cancel();
TextView txtwrong = (TextView) findViewById(R.id.txt_wrong_riazi1_1);
txtwrong.setVisibility(View.VISIBLE);
Button btn2 = (Button) findViewById(R.id.btn_riazi1_1_2);
btn2.setBackgroundColor(Color.GREEN);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i=new Intent(Riazi1_1.this,Riazi1_2.class);
startActivity(i);
}
}, 1000);
}
});
}
#Override
public void onBackPressed(){
super.onBackPressed();
Count.cancel(); // error: Count cannot be resolved ///
}
}
You have declared the TImer within onCreate method. So you cannot access it outside of onCreate. Declare it as a member variable.
private CountDownTimer Count; /// declare it as a member variable
and in the onCreate method initialize it like
Count = new CountDownTimer(5000, 1000).....
[A suggestion, use the naming convension. naming a variable starting with capital letter is really confusing]
I need to toast the stopwatch's value
ie.,time taken between start and stop
If i click the stop button it should toast that time duration.
How to do this?
Here i have tried some code
chrono_meter.java
public class Chrono_meter extends Activity {
Chronometer chr;
Button btn_stop_travel;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chronometer_layout);
chr = (Chronometer)findViewById(R.id.chronometer1);
chr.start();
btn_stop_travel = (Button)findViewById(R.id.btn_stop_inspection);
btn_stop_travel.setOnClickListener(mStopListener);
}
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
chr.stop();
}
};
}
Try this
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
chr.stop();
Toast.makeText(Chrono_meter.this, chr.getText(), Toast.LENGTH_LONG).show() ;
}
};
Can a class have getExtra and putExtra at the same time?
Suppose, Class 1 passes data to Class 2. Class 2 passes data to Class 3.
How am I suppose to get Class 2 working?
Class 1
public static final String EXTRA_RADIO="com.example.flash.Mode";
int btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bP= (Button)findViewById(R.id.btnP);
Button bT= (Button)findViewById(R.id.btnP);
final Intent intent = new Intent(Main.this, Mode.class);
bP.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//TODO Auto-generated method stub
btn=1;
intent.putExtra(EXTRA_RADIO, btn);
startActivity(intent);
}
});
bT.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//TODO Auto-generated method stub
btn=2;
intent.putExtra(EXTRA_RADIO, btn);
startActivity(intent);
}
});
}
Class 2
int mode;
int op;
public static final String EXTRA_OP = "com.example.flash.Operator";
public void onStart(){
super.onStart();
mode = getIntent().getIntExtra(Main.EXTRA_RADIO, 0);
setContentView(R.layout.mode);
if (mode==1){
m.setText("Practice");
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//codes
op=1;
}
});
}
if (mode==2){
m.setText("Trial");
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//codes
op=2;
}
});
}
Intent intent = new Intent(Mode.this, Operator.class);
intent.putExtra(EXTRA_OP, op);
startActivity(intent);
}
Then class 3 gets op from class 2. My class 2 doesn't work.
Try adding #Override for your onStart in Class 2.
Can a class have getExtra and putExtra at the same time?
Yes. Looks like you are using getExtra and putExtra for different intent.
here is the code im using...my countdown timer doesnt display 0(zero) after 1...how to do it...I have tried replacing 1000 with 0 in countdown ...but it brought error while debugging...plss hellppppppp
public class play extends Activity {
TextView mTextField;
TextView score;
ToggleButton pause;
CountDownTimer countdown;
private long a;
private long b;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.game);
mTextField=(TextView)findViewById(R.id.tme);
score=(TextView)findViewById(R.id.score);
score.setText("0" );
final ImageButton menu=(ImageButton)findViewById(R.id.menu);
menu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(play.this , GoodwordActivity.class);
startActivity(intent);
}
});
Bundle bundle = getIntent().getExtras();
b= bundle.getInt("a");
a=b;
startCountDownTimer();
pause=(ToggleButton)findViewById(R.id.pause);
pause.setText("");
pause.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (pause.isChecked()) {
pause.setText("");
pause.setBackgroundResource(R.drawable.refresh);
countdown.cancel();
} else {
pause.setText("");
pause.setBackgroundResource(R.drawable.pause);
startCountDownTimer();
}
}
});
}
private void startCountDownTimer( )
{
countdown=new CountDownTimer(a, 1000) {
public void onTick(long millisUntilFinished ) {
a=millisUntilFinished;
mTextField.setText("" + millisUntilFinished/1000);
}
public void onFinish() {
a=b;
startCountDownTimer();
}
}.start();
}
}
I think you have to put the
mTextField.setText("0");
into the onFinish() function.