Android game 3 trials for user - java

Hello it's my first time to develop android color game. However I'd like to put 3 trials in each question. I'm a bit confused how or where to put my while loop in my code. Please have a look on what I have tried so far:
int trial = 0;
private void getCorrectObject() {
List<Integer> objects = new ArrayList<Integer>();
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
objects.add(5);
objects.add(6);
objects.add(7);
objects.add(8);
objects.add(9);
Collections.shuffle(objects);
int correctObject = objects.get(0);
Log.d("test", String.valueOf(correctObject));
while(trial <=3){
trial++;
switch(correctObject)
{
case 1:
bObjectCorrect.setImageResource(R.drawable.stage1_1_object1);
bObjectCorrect.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 2:
bObject1.setImageResource(R.drawable.stage1_1_object1);
bObject1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 3:
bObject2.setImageResource(R.drawable.stage1_1_object1);
bObject2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 4:
bObject3.setImageResource(R.drawable.stage1_1_object1);
bObject3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 5:
bObject4.setImageResource(R.drawable.stage1_1_object1);
bObject4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 6:
bObject5.setImageResource(R.drawable.stage1_1_object1);
bObject5.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 7:
bObject6.setImageResource(R.drawable.stage1_1_object1);
bObject6.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 8:
bObject7.setImageResource(R.drawable.stage1_1_object1);
bObject7.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 9:
bObject8.setImageResource(R.drawable.stage1_1_object1);
bObject8.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
} // Last of switch statement
if(trial == 3){
new AlertDialog.Builder(this)
.setTitle("Game Over")
.setMessage("Sorry you reached your 3rd trial")
.setPositiveButton("Try Again?", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Stage1_2.this, Stage1_1.class);
startActivity(i);
}
})
.setNegativeButton("Back to Menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Stage1_2.this, ShapingColors.class);
startActivity(i);
}
})
.show();
}
} // end of while loop
I really like to finish this thing so I can continue with the game. Any help is truly appreciated. Thanks in advance.
I added a new method called guessedWrong()
private void guessedWrong(){
trial++;
if(trial == 3){
new AlertDialog.Builder(this)
.setTitle("Game Over")
.setMessage("Sorry you reached your 3rd trial")
.setPositiveButton("Try Again?", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Stage1_1.this, Stage1_1.class);
startActivity(i);
}
})
.setNegativeButton("Back to Menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Stage1_1.this, ShapingColors.class);
startActivity(i);
}
})
.show();
}
}

The reason you're confused is that you can't use a loop for this task. When using the Java Android framework, each of your callback functions (for example, an onClick listener, or your Activity's onResume) has to run and finish within one frame of the application. Only after the function returns does your app update the screen. This means that if you're doing something like responding to a series of clicks, you can't do that in a loop. You have to handle each click in a separate call to the callback. for and while loops are appropriate if you want to iterate over a list of items to decide what should happen right now (for example, if you're adding items to a ListView), but you can't iterate over things that happen at different times (such as the user's guesses).
You have to think about your Activity like a state machine. Make trial a member variable (field) of the Activity, which starts out at 0. You might have a function guessedWrong() which increments trial, and goes to the "game over" screen if it's greater than 2. The onClick listener for the wrong answers will call this function. When moving to a new question, reset trial to 0.
You also need to make sure the number of trials (which state you're in) is preserved if your Activity is restarted. The lesson Recreating an Activity in the Android Developers' Training offered by Google shows you how to do this.

Related

How to use If condition in Button intent call?

I have created two activities:
Play
MorpionAI
decide and checkedplayer are both some integers.
How can I make my app so that when the button is clicked the app selects and calls one of the intents based on following:
if(decide==checkedplayer){
public void MorpionAI_Acitivity(View view) {
Clean();
deleteplayer();
Intent play = new Intent(this, MorpionAI.class);
startActivity(play);
}}
else{
public void Play_Activity(View view) {
Clean();
deleteplayer();
Intent play = new Intent(this, Play.class);
startActivity(play);
}}
I tried to do this using following code:
morpionbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (decide==checkedplayer){
Clean();
deleteplayer();
Intent play = new Intent(view.getContext(), Play.class);
startActivity(play);
}
else{
Clean();
deleteplayer();
Intent play = new Intent(view.getContext(), MorpionAI.class);
startActivity(play);
}
}
});
But it did not worked.
In your activity , defined a Context class field :
private Context mContext;
in the onCreate() function of this activity , initialize the field :
mContext = this;
then
morpionbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (decide == checkedplayer){
Clean();
deleteplayer();
Intent play = new Intent(mContext, Play.class);
mContext.startActivity(play);
}
else {
Clean();
deleteplayer();
Intent play = new Intent(mContext, MorpionAI.class);
mContext.startActivity(play);
}
}
});
Hope this helps.

Is it possible to use a same onClick for both AlertDialog and View?

My activity has an accept button witch sends intent to parent through onClickOkayButton method. I have overriden the onBackPressed() method so that it shows an AlertDialog asking the user if he really wishes to leave or if he wants to save the preferences, witch does the exact same action as the accept button.
Is it possible to merge both onClick methods into one so I don't need to copy/paste in case of editing one of those, even if both methods use different parameters?
public void onClickOkayButton(View view) {
// THIS IS THE ACCEPT BUTTON
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!(title.matches("")) || !(title.isEmpty()) || !(title.equals("")) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(this, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onBackPressed() {
if (adapter.getCount() != 0) {
showAlertDialog();
} else {
super.onBackPressed();
}
}
public void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.surveyadd_warn_back_message);
builder.setNegativeButton(R.string.surveyadd_warn_back_save, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// THIS WAS COPIED FROM THE ACCEPT BUTTON
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!(title.matches("")) || !(title.isEmpty()) || !(title.equals("")) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(context, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
});
// UP TO HERE
builder.setPositiveButton(R.string.surveyadd_warn_back_erase, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
You won't be able to use the same callback methods because they are from different interfaces.
What you should do is move all the common code into a third method and just call it from both click handlers like below:
private void setResultAndFinish() {
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!TextUtils.isEmpty(title)) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(context, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
And then the handlers:
public void onClickOkayButton(View view) {
setResultAndFinish();
}
#Override
public void onClick(DialogInterface dialog, int which) {
setResultAndFinish();
}

Logic flow for android game

Hi I'm new to android game development and I'd like to ask you guys regarding the correct logic flow for this android game of mine. So here's my game. It's a pretty simple game which user has to choose the correct color. The game has 3 stages. In each stage, it has 10 questions. In each question, it has a 30 seconds timer. with a question and choices, of course it needs to be randomized. If the user chooses the correct color, it will proceed to the next question. But if the user chooses the wrong color, he/she has only 3 trials to choose, if he/she reaches the 3rd trial, Game is OVER and display a TRY AGAIN button.
Here's a piece of code that I tried:
// I created a custom countdown timer c/o Say
counter = new MyCount(30000,1000);
counter.start();
// Call for correct object
getCorrectObject();
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.pause:
if(mLastResourceId == R.drawable.pause){
pause.setImageResource(R.drawable.resume);
mLastResourceId = R.drawable.resume;
counter.cancel();
} else if (mLastResourceId == R.drawable.resume) {
pause.setImageResource(R.drawable.pause);
mLastResourceId = R.drawable.pause;
counter = new MyCount(s1,1000);
counter.start();
}
break;
}
public class MyCount extends CountDownTimer
{
public MyCount(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish()
{
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
}
TextView tx = (TextView) findViewById(R.id.timer);
#Override
public void onTick(long millisUntilFinished)
{
s1 = millisUntilFinished;
tx.setText("" + millisUntilFinished / 1000);
}
}
What also I'm considering is, if the user chooses the correct answer without the timer has ended, what should I put in onTick method to force the timer to end? And one of the tricky part that I face is the randomization of choices or the objects.
here is also what I've tried so far:
private void getCorrectObject() {
// TODO Auto-generated method stub
List<Integer> objects = new ArrayList<Integer>();
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
objects.add(5);
objects.add(6);
objects.add(7);
Collections.shuffle(objects);
int correctObject = objects.get(0);
Log.d("test", String.valueOf(correctObject));
switch(correctObject)
{
case 1:
bObjectCorrect.setImageResource(R.drawable.tree1);
bObjectCorrect.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 2:
bObject1.setImageResource(R.drawable.tree1);
bObject1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 3:
bObject2.setImageResource(R.drawable.tree1);
bObject2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 4:
bObject3.setImageResource(R.drawable.tree1);
bObject3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 5:
bObject4.setImageResource(R.drawable.tree1);
bObject4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 6:
bObject5.setImageResource(R.drawable.tree1);
bObject5.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 7:
bObject6.setImageResource(R.drawable.tree1);
bObject6.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
}
}
UPDATED: and another thing is, I'm confused where to put my while loop in here for 3 trials.
// I will put 3 trials logic here
while(trial <= 3){
trial++;
switch(correctObject)
{
case 1:
bObjectCorrect.setImageResource(R.drawable.stage1_1_object1);
bObjectCorrect.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 2:
bObject1.setImageResource(R.drawable.stage1_1_object1);
bObject1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 3:
bObject2.setImageResource(R.drawable.stage1_1_object1);
bObject2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 4:
bObject3.setImageResource(R.drawable.stage1_1_object1);
bObject3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 5:
bObject4.setImageResource(R.drawable.stage1_1_object1);
bObject4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 6:
bObject5.setImageResource(R.drawable.stage1_1_object1);
bObject5.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 7:
bObject6.setImageResource(R.drawable.stage1_1_object1);
bObject6.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 8:
bObject7.setImageResource(R.drawable.stage1_1_object1);
bObject7.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 9:
bObject8.setImageResource(R.drawable.stage1_1_object1);
bObject8.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
} // Last of switch statement
if(trial == 3){
new AlertDialog.Builder(this)
.setTitle("Game Over")
.setMessage("Sorry you reached your 3rd trial")
.setPositiveButton("Try Again?", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Stage1_2.this, Stage1_1.class);
startActivity(i);
}
})
.setNegativeButton("Back to Menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Stage1_2.this, ShapingColors.class);
startActivity(i);
}
})
.show();
}
} // Last of while loop
I'd really love to hear your suggestions. Any help from you is truly appreciated. Thanks in advance!
You don't need to put anything in onTick to handle this case. After you've called cancel() (which you do in your click handler), onTick() won't be called again.

CountDownTimer calls double method

I don't know how to explain better. I have this timer, and after it finish counting it should call another class (popup) and after that other function in the same class where the counter is.
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
Intent i = new Intent(getApplicationContext(), PogresanOdgovor.class);
i.putExtra("tacanOdgovor", tacanOdg);
startActivity(i);
mHandler.postDelayed(mLaunchTask,2200);
brojacPogresnihOdgovora++;
}
After first pass, my score is 2 instead of 1, then 6, the 14...This delayed method is simply the next question:
Runnable mLaunchTask = new Runnable() {
public void run() {
nextQuestion();
brojacVremena.start();
}
};
I call exactly the same method as the one in onFinish() when user answer wrong and it works fine.
MyCount brojacVremena = new MyCount(6000, 1000);
final OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
Answer ans = (Answer) v.getTag();
if (ans.isCorrect) {
brojacVremena.cancel();
brojacTacnihOdgovora = brojacTacnihOdgovora + 5;
Intent i = new Intent("rs.androidaplikacijekvizopstekulture.TACANODGOVOR");
startActivity(i);
mHandler.postDelayed(mLaunchTask,1200);
}
else{
brojacVremena.cancel();
brojacPogresnihOdgovora++;
Intent i = new Intent(getApplicationContext(), PogresanOdgovor.class);
i.putExtra("tacanOdgovor", tacanOdg);
startActivity(i);
mHandler.postDelayed(mLaunchTask,2200);
}
};
I found my error. I called my counter twice. Here:
nextQuestion();
brojacVremena.start();
and below in the very same nextQuestion method:
public void nextQuestion() {
brojacVremena.start();
.
.
.
I don't know how that happened.

Dialog Box disapears without being clicked

public void onClick(View v) {
if(a){
Intent i = new Intent();
if(type.equals("x")){
showErrorAlert("string");
i = new Intent(Activity1.this, Activity2.class);
i.putExtra("label", var);
i.putExtra("label1", var2);
startActivity(i);
}
else if(type.equals("y")){
i = new Intent(Activity1.this, Activity3.class);
i.putExtra("label2", var3);
}
//startActivity(i);
}
else startActivity(new Intent(Activity1.this, Activity4.class));
}
});
private void showErrorAlert(String errorMsg){
AlertDialog errorDialog = new AlertDialog.Builder(this).create();
errorDialog.setTitle("title");
errorDialog.setMessage(errorMsg);
errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
errorDialog.show();
}
So what happens is the error alert shows, but it will immediately close and the next activity will appear. I want the activity to not start until after "Okay" is selected.
Try this
public void onClick(View v) {
if(a){
Intent i = new Intent();
if(type.equals("x")){
showErrorAlert("string");
}
else if(type.equals("y")){
i = new Intent(Activity1.this, Activity3.class);
i.putExtra("label2", var3);
}
//startActivity(i);
}
else startActivity(new Intent(Activity1.this, Activity4.class));
}
});
private void showErrorAlert(String errorMsg){
AlertDialog errorDialog = new AlertDialog.Builder(this).create();
errorDialog.setTitle("title");
errorDialog.setMessage(errorMsg);
errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("label", var);
i.putExtra("label1", var2);
startActivity(i);
}
});
errorDialog.show();
}
Move your startActivity(i); to inside if (or) else (or) both blocks depending on where you need.
Place startActivity(i); inside the onClick method in your dialog like this:
private void showErrorAlert(String errorMsg){
AlertDialog errorDialog = new AlertDialog.Builder(this).create();
errorDialog.setTitle("title");
errorDialog.setMessage(errorMsg);
errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
startActivity(i);
}
});
errorDialog.show();
}

Categories