How to Re-Run my ProgressBar after its completes hi activity? - java

I need help with this, I made this calculator, and a progressbar , i need it to when its finish so i can click again and run again, i didnt found a way to do that, and do the same with anothr data or the same data as well
private TextView textResultado;
private TextView editpeso;
private TextView editaltura;
private Button botao;
private ProgressBar pb;
private TextView txt;
int i = 0;
Handler handler = new Handler();
String[] mensagens = {"Preencha todos os campos!"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
iniciar();
botao.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pb.setVisibility(View.VISIBLE);
String peso = editpeso.getText().toString();
String altura = editaltura.getText().toString();
if (peso.isEmpty() || altura.isEmpty()) {
Snackbar snackbar = Snackbar.make(view, mensagens[0], Snackbar.LENGTH_SHORT);
snackbar.setBackgroundTint(getResources().getColor(R.color.white));
snackbar.setTextColor(getResources().getColor(R.color.black));
snackbar.show();
}
new Thread(new Runnable() {
#Override
public void run() {calculadoramm(view);
while (i < 100) {
i += 1;
try {
Thread.sleep(60);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
#Override
public void run() {
pb.setProgress(i);
txt.setText(i + "% Verificando...");
if (i == pb.getMax())
Toast.makeText(MainActivity.this, "Completo!", Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();
}
});
}
private void iniciar() {
textResultado = findViewById(R.id.textResultado);
editpeso = findViewById(R.id.editpeso);
editaltura = findViewById(R.id.editaltura);
botao = (Button) findViewById(R.id.botao);
pb = (ProgressBar) findViewById(R.id.pb);
txt = (TextView) findViewById(R.id.txt);
}
public void calculadoramm(View view) {
double peso = Double.parseDouble(editpeso.getText().toString());
double altura = Double.parseDouble(editaltura.getText().toString());
double resultado = peso / (altura * altura);
//Se
if (resultado <= 18.5) {
textResultado.setText("Abaixo do Peso!");
textResultado.setTextColor(getResources().getColor(android.R.color.holo_blue_bright));
}
if (resultado <= 18.5 || resultado >= 24.9) {
textResultado.setText("Peso normal!");
textResultado.setTextColor(getResources().getColor(android.R.color.holo_green_light));
}
if (resultado <= 25 || resultado >= 29.9) {
textResultado.setText("Excesso de peso!");
textResultado.setTextColor(getResources().getColor(android.R.color.holo_green_light));
if (resultado <= 30 || resultado >= 34.9) {
textResultado.setText("Obesidade nivel I!");
textResultado.setTextColor(getResources().getColor(android.R.color.holo_orange_light));
if (resultado <= 35 || resultado >= 39.9) {
textResultado.setText("Obesidade nivel II!");
textResultado.setTextColor(getResources().getColor(android.R.color.holo_red_light));
if (resultado >= 40) {
textResultado.setText("Obesidade nivel III ou Morbida!");
textResultado.setTextColor(getResources().getColor(android.R.color.holo_red_light));
}
}
}
}
}
}
I've tried to add an Intent to start activity from Main to my results and None seems to work, and updated the code

Related

Java code to show action on x amount of presses?

I have an iOS app that I worked on some time ago, and the function it uses I like, and would like to somehow get it to work on some Android code I have.
At the top of my MainActivity I have
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "nShownLobby";
on my onCreate I have
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
And I am calling this method
changeTextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
long nb_shown_lobby = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getLong("nShownLobby", 0);
++nb_shown_lobby;
if ((nb_shown_lobby % 20) == 0) {
this.turnAround();
}
int random = (int) (Math.random() * manyDifferentStrings.length);
if (random == oldVaue) {
random = (int) (Math.random() * manyDifferentStrings.length);
}
changingText.setText(manyDifferentStrings[random]);
oldVaue = random;
try {
mySound.start();
} catch (NullPointerException e) {
mySound = MediaPlayer.create(MainActivity.this, R.raw.sound);
}
}
private void turnAround() {
//Do something here
Log.i("Do Something ", "");
}
});
The intention is that after every 20 presses, the turnAround() method is called but it does not... It just gets ignored - I am guessing I have missed something?
**According to your question your code should be like this**
private SharedPreferences sharedpreferences;
private SharedPreferences.Editor editor;
private static final String MyPREFERENCES = "nShownLobby";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
editor = sharedpreferences.edit();
changeTextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
int nb_shown_lobby = sharedpreferences.getInt("nShownLobby", 0);
if ((nb_shown_lobby % 20) == 0) {
turnAround();
}
nb_shown_lobby += 1;
editor.putInt("nShownLobby", nb_shown_lobby);
editor.commit();
int random = (int) (Math.random() * manyDifferentStrings.length);
if (random == oldVaue) {
random = (int) (Math.random() * manyDifferentStrings.length);
}
changingText.setText(manyDifferentStrings[random]);
oldVaue = random;
try {
mySound.start();
} catch (NullPointerException e) {
mySound = MediaPlayer.create(MainActivity.this, R.raw.sound);
}
}
});
}
//created this in activity, not in the button onclick
private void turnAround() {
//Do something here
Log.i("Do Something ", "");
}

I have an Issue in a Calculator app - how to fix it?

The Problem I have is how to change last operation by pressing the new operation the New Operation Will Override the previous
package com.zpat.calculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity
extends AppCompatActivity {
private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, badd, bsub, bmul, bdiv, bdot, bans, bclear;
private TextView ans, Info;
private final char ADDITION = '+';
private final char SUBTRACTION = '-';
private final char MULTIPLICATION = '*';
private final char DIVISION = '/';
private final char EQUAL = 0;
private String val1 = "";
private String val2 = "";
private char Action;
boolean isAnsPress = false;
boolean isDotPressed = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupUIViews();
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "1");
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "2");
}
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "3");
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "4");
}
}
});
b5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "5");
}
}
});
b6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "6");
}
}
});
b7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "7");
}
}
});
b8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "8");
}
}
});
b9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "9");
}
}
});
b0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() >= 1) {
TextUtils.isEmpty(a);
}
ans.setText(ans.getText().toString() + "0");
}
});
bdot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isDotPressed) {
ans.setText(ans.getText() + ".");
isDotPressed = true;
}
}
});
badd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
String a = Info.getText().toString().substring(Info.getText().toString().length() - 1, Info.getText().toString().length());
if (Action == ADDITION) {
compute();
Info.setText(String.valueOf(val1) + "+");
ans.setText(null);
}
} catch (Exception ex) {
ex.printStackTrace();
}
isAnsPress = false;
isDotPressed = false;
}
});
bsub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
compute();
Action = SUBTRACTION;
Info.setText(String.valueOf(val1) + "-");
ans.setText(null);
isAnsPress = false;
isDotPressed = false;
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
bmul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
compute();
Action = MULTIPLICATION;
Info.setText(String.valueOf(val1) + "*");
ans.setText(null);
isAnsPress = false;
isDotPressed = false;
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
bdiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
compute();
Action = DIVISION;
Info.setText(String.valueOf(val1) + "/");
ans.setText(null);
isAnsPress = false;
isDotPressed = false;
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
bans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
if (!isAnsPress) {
compute();
Action = EQUAL;
Info.setText(Info.getText().toString() + String.valueOf(val2) + "=" + String.valueOf(val1));
ans.setText(null);
isAnsPress = true;
isDotPressed = false;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
bclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (ans.getText().length() > 0) {
CharSequence name = ans.getText().toString();
ans.setText(name.subSequence(0, name.length() - 1));
} else {
Info.setText(null);
val1 = "";
}
isAnsPress = false;
isDotPressed = false;
}
});
}
private void setupUIViews() {
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.btn1);
b2 = (Button) findViewById(R.id.btn_2);
b3 = (Button) findViewById(R.id.btn_3);
b4 = (Button) findViewById(R.id.btn4);
b5 = (Button) findViewById(R.id.btn_5);
b6 = (Button) findViewById(R.id.btn_6);
b7 = (Button) findViewById(R.id.btn_7);
b8 = (Button) findViewById(R.id.btn_8);
b9 = (Button) findViewById(R.id.btn_9);
b0 = (Button) findViewById(R.id.btn_0);
badd = (Button) findViewById(R.id.btn_Add);
bsub = (Button) findViewById(R.id.btn_Sub);
bmul = (Button) findViewById(R.id.btn_Multiply);
bdiv = (Button) findViewById(R.id.btn_div);
bdot = (Button) findViewById(R.id.btn_dot);
bans = (Button) findViewById(R.id.btn_equal);
bclear = (Button) findViewById(R.id.btn_Clear);
ans = (TextView) findViewById(R.id.Answer);
Info = (TextView) findViewById(R.id.info);
}
private void compute() {
if (val1 != "") {
val2 = ans.getText().toString();
switch (Action) {
case ADDITION:
val1 = String.valueOf(Double.parseDouble(val1) + Double.parseDouble(val2));
break;
case SUBTRACTION:
val1 = String.valueOf(Double.parseDouble(val1) - Double.parseDouble(val2));
break;
case MULTIPLICATION:
val1 = String.valueOf(Double.parseDouble(val1) * Double.parseDouble(val2));
break;
case DIVISION:
val1 = String.valueOf(Double.parseDouble(val1) / Double.parseDouble(val2));
break;
case EQUAL:
break;
}
} else {
val1 = ans.getText().toString();
}
double temp = Double.parseDouble(val1);
int temp2 = (int) temp;
double temp3 = temp - temp2;
if (temp3 == 0) {
val1 = String.valueOf((int) Double.parseDouble(val1));
}
}
}

Disabling onClick until int = something

public class MainActivity extends AppCompatActivity {
int foulCounterA = 0;
int scoreOnePointTeamA = 0;
int periodCount = 0;
private TextView tv1;
private TextView period;
private Button startbtn, cancelbtn;
private ToggleButton togbtn;
private boolean isPaused = false;
private boolean isCanceled = false;
private long remainingTime = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
period = (TextView) findViewById(R.id.period_number);
startbtn = (Button) findViewById(R.id.startBtn);
cancelbtn = (Button) findViewById(R.id.cancelBtn);
togbtn = (ToggleButton) findViewById(R.id.togBtn);
cancelbtn.setEnabled(false);
togbtn.setEnabled(false);
startbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
foulCounterA = 0;
foulCounterB = 0;
displayForTeamAFoul(foulCounterA);
displayForTeamBFoul(foulCounterB);
if (periodCount < 3)
periodCount = periodCount + 1;
else periodCount = 4;
period.setText("Period " + periodCount);
startbtn.setEnabled(false);
cancelbtn.setEnabled(true);
togbtn.setEnabled(true);
isPaused = false;
isCanceled = false;
long millisInFuture = 20000; /////20sec
long countDownInterval = 1000; /////1sec
new CountDownTimer(millisInFuture, countDownInterval) {
#Override
public void onTick(long millisUntilFinished) {
if (isPaused || isCanceled) {
cancel();
} else {
tv1.setText("" + millisUntilFinished / 1000);
remainingTime = millisUntilFinished;
}
}
#Override
public void onFinish() {
startbtn.setEnabled(true);
togbtn.setEnabled(false);
if (periodCount < 4)
tv1.setText("Times up!");
else tv1.setText("Game Over!");
}
}.start();
}
});
togbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (togbtn.isChecked()) {
isPaused = true;
} else {
isPaused = false;
long millisInFuture = remainingTime;
long countDownInterval = 1000; /////1sec
new CountDownTimer(millisInFuture, countDownInterval) {
#Override
public void onTick(long millisUntilFinished) {
if (isPaused || isCanceled) {
cancel();
} else {
tv1.setText("" + millisUntilFinished / 1000);
remainingTime = millisUntilFinished;
}
}
#Override
public void onFinish() {
startbtn.setEnabled(true);
togbtn.setEnabled(false);
if (periodCount < 4)
tv1.setText("Times up!");
else tv1.setText("Game Over!");
}
}.start();
}
}
});
cancelbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isCanceled = true;
period.setText("Period");
tv1.setText("Timer");
startbtn.setEnabled(true);
togbtn.setEnabled(false);
cancelbtn.setEnabled(false);
foulCounterA = 0;
foulCounterB = 0;
periodCount = 0;
displayForTeamAFoul(foulCounterA);
displayForTeamBFoul(foulCounterB);
}
});
}
public void onePointForTeamA(View v) {
scoreTeamA = scoreTeamA + 1;
scoreOnePointTeamA = scoreOnePointTeamA + 1;
displayForTeamA(scoreTeamA);
displayForTeamAOnePoint(scoreOnePointTeamA);
}
public void foulCountForTeamA(View v) {
if (foulCounterA < 5)
foulCounterA = foulCounterA + 1;
else
foulCounterA = 5;
displayForTeamAFoul(foulCounterA);
}
public void displayForTeamAOnePoint(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score_1_point);
scoreView.setText(String.valueOf(score));
}
public void displayForTeamAFoul(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_foul);
scoreView.setText(String.valueOf(score));
}
}
I wanted to make the java code as simple as I can so I've just added the lines for my question. What I'm trying to do is
android:onClick="onePointForTeamA"make this button only clickable when foulCounterA = 5 Failed to add if (foulCounterA = 5); inside public void foulCountForTeamA(View v) {
It gave me an error that way. Says required: boolean, found: int.
What should I do with the code? Any help will be appreeciated
Regarding your concrete question, the syntax of if (foulCounterA = 5); is wrong, because the equation check is have to made by == operator.
So the correct syntax would be if (foulCounterA == 5);
As #OH GOD SPIDERS wrote in the comment, you should check the basics of java operators.
Also I recommend You to search for the answer before asking a new question.

Global Variable in CountDownTimer not changing

Please assist, I have the following code that I have tried executing in diffirent places with the following results:
1) When placed in the on create it keeps resetting to 60 seconds
2) When placed in onResume it seems to work perfectly until the play again button is pressed then it starts counting down from the last onPause position
3) When placed in the playAgain method it crashes
Code:
createTimer Method
public void createTimer(){
countDownTimer = new CountDownTimer(timeRemaining, 1000) {
#Override
public void onTick(long l) {
if (!isFinnish) {
timeRemaining = l;
} else {
timeRemaining = 63200;
}
countDownText.setText(String.valueOf(l/1000-60));
if (l <= 61000) {
timerTextView.setText(String.valueOf(l/1000) + "s");
button0.setEnabled(true);
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
button0.setVisibility(View.VISIBLE);
button1.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
button3.setVisibility(View.VISIBLE);
sumTextView.setVisibility(View.VISIBLE);
countDownText.setVisibility(View.INVISIBLE);
}
if (l/1000 == 30){
if (MainActivity.mplayer7 != null) {
MainActivity.mplayer7.release();
MainActivity.mplayer7 = null;
}
MainActivity.mplayer7 = MediaPlayer.create(getApplicationContext(), R.raw.halfway);
if (MainActivity.muted == false) {
MainActivity.mplayer7.setVolume(0.0f, 0.0f);
}
MainActivity.mplayer7.start();
}
if (l/1000 == 5){
if (MainActivity.mplayer != null) {
MainActivity.mplayer.release();
MainActivity.mplayer = null;
}
MainActivity.mplayer = MediaPlayer.create(getApplicationContext(), R.raw.ticktock);
if (MainActivity.muted == false) {
MainActivity.mplayer.setVolume(0.0f, 0.0f);
}
MainActivity.mplayer.start();
}
}
#Override
public void onFinish() {
timerTextView.setText("0s");
timeRemaining = 63200;
isFinnish = true;
//Log.i("Score", String.valueOf(score));
//Log.i("Number of Questions", String.valueOf(numberOfQuestions));
//Log.i("Percentage", String.valueOf(percentage));
//resultTextView.setText("You scored: " + Integer.toString(score) + "/" + Integer.toString(numberOfQuestions));
if (score > 0) {
percentage = score * 100 / numberOfQuestions;
}
else {
percentage = 0;
}
button0.setEnabled(false);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
playAgainButton.setVisibility(View.VISIBLE);
returnToMenu.setVisibility(View.VISIBLE);
resultDisplayTextView.setVisibility(View.VISIBLE);
resultTextView.setText(" ");
if (percentage > 80) {
resultDisplayTextView.setText("Fantastic Score! " + Integer.toString(percentage) + "%");
if (MainActivity.mplayer3 != null) {
MainActivity.mplayer3.release();
MainActivity.mplayer3 = null;
}
MainActivity.mplayer3 = MediaPlayer.create(getApplicationContext(), R.raw.fireworks);
if (MainActivity.muted == false) {
MainActivity.mplayer3.setVolume(0.0f, 0.0f);
}
MainActivity.mplayer3.start();
}
else if (percentage >= 60 && percentage < 80) {
resultDisplayTextView.setText("Great Score! " + Integer.toString(percentage) + "%");
}
else if (percentage >= 40 && percentage < 60) {
resultDisplayTextView.setText("Average Score! " + Integer.toString(percentage) + "%");
}
else if (percentage > 1 && percentage < 40) {
resultDisplayTextView.setText("Poor Score! " + Integer.toString(percentage) + "%");
if (MainActivity.mplayer4 != null) {
MainActivity.mplayer4.release();
MainActivity.mplayer4 = null;
}
MainActivity.mplayer4 = MediaPlayer.create(getApplicationContext(), R.raw.poor);
if (MainActivity.muted == false) {
MainActivity.mplayer4.setVolume(0.0f, 0.0f);
}
MainActivity.mplayer4.start();
}
else {
resultDisplayTextView.setText("You did not play?");
if (MainActivity.mplayer5 != null) {
MainActivity.mplayer5.release();
MainActivity.mplayer5 = null;
}
MainActivity.mplayer5 = MediaPlayer.create(getApplicationContext(), R.raw.poor);
if (MainActivity.muted == false) {
MainActivity.mplayer5.setVolume(0.0f, 0.0f);
}
MainActivity.mplayer5.start();
}
}
};
}
playAgain Method
public void playAgain (View view) {
score = 0;
isFinnish = false;
numberOfQuestions = 0;
timerTextView.setText("60s");
pointsTextView.setText("0/0");
resultTextView.setText("");
playAgainButton.setVisibility(View.INVISIBLE);
returnToMenu.setVisibility(View.INVISIBLE);
resultDisplayTextView.setVisibility(View.INVISIBLE);
countDownText.setVisibility(View.VISIBLE);
button0.setVisibility(View.INVISIBLE);
button1.setVisibility(View.INVISIBLE);
button2.setVisibility(View.INVISIBLE);
button3.setVisibility(View.INVISIBLE);
sumTextView.setVisibility(View.INVISIBLE);
if (MainActivity.mplayer3 != null) {
MainActivity.mplayer3.release();
MainActivity.mplayer3 = null;
}
if (MainActivity.mplayer4 != null) {
MainActivity.mplayer4.release();
MainActivity.mplayer4 = null;
}
if (MainActivity.mplayer6 != null) {
MainActivity.mplayer6.release();
MainActivity.mplayer6 = null;
}
MainActivity.mplayer6 = MediaPlayer.create(getApplicationContext(), R.raw.countdown);
if (MainActivity.muted == false) {
MainActivity.mplayer6.setVolume(0.0f, 0.0f);
}
MainActivity.mplayer6.start();
countDownTimer.start();
generateQuestion();
}
onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
createTimer();
// THIS CODE IS TO ENABLE THE ICON IN THE TASKBAR ////////////////////
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
//////////////////////////////////////////////////////////////////////
// SET IT TO ONLY POTRAIT VIEW
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Intent intent = getIntent();
countDownText = (TextView) findViewById(R.id.countDownText);
playAgainButton = (Button) findViewById(R.id.playAgainButton);
returnToMenu = (Button) findViewById(R.id.returnToMenu);
sumTextView = (TextView) findViewById(R.id.sumTextView);
resultTextView = (TextView) findViewById(R.id.resultTextView);
resultDisplayTextView = (TextView) findViewById(R.id.resultDisplayTextView);
pointsTextView = (TextView) findViewById(R.id.pointsTextView);
timerTextView = (TextView) findViewById(R.id.timerTextView);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
MainActivity.mAdView = (AdView) findViewById(R.id.adView);
MainActivity.mplayer = MediaPlayer.create(getApplicationContext(), R.raw.ticktock);
MainActivity.mplayer1 = MediaPlayer.create(getApplicationContext(), R.raw.pop);
MainActivity.mplayer2 = MediaPlayer.create(getApplicationContext(), R.raw.wrong);
MainActivity.mplayer3 = MediaPlayer.create(getApplicationContext(), R.raw.fireworks);
MainActivity.mplayer4 = MediaPlayer.create(getApplicationContext(), R.raw.poor);
MainActivity.mplayer5 = MediaPlayer.create(getApplicationContext(), R.raw.poor);
MainActivity.mplayer6 = MediaPlayer.create(getApplicationContext(), R.raw.countdown);
MainActivity.mplayer7 = MediaPlayer.create(getApplicationContext(), R.raw.halfway);
if (MainActivity.muted == false) {
MainActivity.mplayer.setVolume(0.0f, 0.0f);
MainActivity.mplayer1.setVolume(0.0f, 0.0f);
MainActivity.mplayer2.setVolume(0.0f, 0.0f);
MainActivity.mplayer3.setVolume(0.0f, 0.0f);
MainActivity.mplayer4.setVolume(0.0f, 0.0f);
MainActivity.mplayer5.setVolume(0.0f, 0.0f);
MainActivity.mplayer6.setVolume(0.0f, 0.0f);
MainActivity.mplayer7.setVolume(0.0f, 0.0f);
}
MainActivity.displayAds();
mHandler.postDelayed(new Runnable() {
public void run() {
playAgain(playAgainButton);
}
}, 1000);
}
onResume and onPause
#Override
public void onResume() {
super.onResume();
countDownTimer.start();
if (MainActivity.mAdView != null) {
MainActivity.mAdView.resume();
}
}
#Override
public void onPause() {
countDownTimer.cancel();
if (MainActivity.mAdView != null) {
MainActivity.mAdView.pause();
}
super.onPause();
}
I have the global var set and assumed that resetting it in the onFinnish within the countdown timer should reset it but it does not.
Thanks
Fields (what you called Global vars) are are not guranteed to be restored on activity recreation and can be GC anytime. You should rather create var in on create, store them in Bundle onPouse and again recreate them from that Bundle in another onCreate or onResume methods.
Here you have Google Dev's docs about how to handle activity creation, recreation and finalization https://developer.android.com/training/basics/activity-lifecycle/recreating.html
So basicly 1) it resets to 60 secs because it is recreated everytime you get back to your activity (from diffrerent app aor different activity)
2) idk where is your "play again" thing and I am not going to go into that code
3) countDownTimer.start(); probably here it crashes with NPE am i right? (I am reading my crystall ball right now)
Read the full Google's tutorial about acitivity lifecycle and you wont have any troubles fixing your code.
https://developer.android.com/training/basics/activity-lifecycle/index.html

Dialog not dismiss in if condition

I am try to dismiss dialog in if condition true part but it is not worked.
In dialog entered in if condition but not dismiss.in If condition toast message display properly.
public void showIncomingCall() {
int getTotal = 0;
if(showincoming != null && showincoming.isShowing() )
{
//adapter1.notifyDataSetChanged();
//showincoming.dismiss();
return;
}
else {
showincoming = new Dialog(MainActivity.this);
showincoming.requestWindowFeature(Window.FEATURE_NO_TITLE);
showincoming.setContentView(R.layout.custome_dialog);
listdialog = (ListView) showincoming.findViewById(R.id.incoming_list);
//adapter1 = new CustomeListAdapter(MainActivity.this);
listdialog.setAdapter(adapter1);
//adapter1.notifyDataSetChanged();
close = (ImageButton) showincoming.findViewById(R.id.dialog_close);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showincoming.dismiss();
adapter1.notifyDataSetChanged();
}
});
adapter1.notifyDataSetChanged();
for (int i = 0; i < listdialog.getCount(); i++) {
parentView = getViewByPosition(i, listdialog);
String getString = ((TextView) parentView.findViewById(R.id.tvLineStatus)).getText().toString();
if (getString.toString().equals("Idle") || getString.toString().equals("Disconnect") || getString.toString().equals("Dialing")) {
getTotal += 1;
}
}
if (getTotal >= 7) {
showincoming.dismiss();
Toast.makeText(getApplicationContext(),"getTotal" + getTotal,Toast.LENGTH_LONG).show();
adapter1.notifyDataSetChanged();
//adapter1.setNotifyOnChange(true);
}
//Toast.makeText(MainActivity.this,getTotal+"getTotal",Toast.LENGTH_LONG).show();
adapter1.notifyDataSetChanged();
listdialog.invalidateViews();
if(!showincoming.isShowing()) {
showincoming.show();
}
}
}
showincoming = new Dialog(MainActivity.this);
declare this below int getTotal = 0;
i.e outside else statement
then you can get referance of dialog object and can dissmiss dialog. try this and let me know

Categories