How to use `onSaveInstanceState`? - java

What do I want to achieve?
I want to save values of x, y and score, so when the user rotates the screen then the values are retrieved and not regenerated.
What do I have?
Probably correct onSaveInstanceState code that isn't working probably because of some #Override coming into conflict with it
What do I need?
Some fix that will help me save values of variables
Code
MainActivity.java (without onSaveInstanceState)
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private EditText textBox;
private int z;
public int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
textBox = findViewById(R.id.editText);
textBox.requestFocus();
setActual();
}
private void setActual() {
Random random = new Random();
int x = random.nextInt(10) + 1; //you get a random number between 0 and 9 then add 1 to get 1 - 10, no need to use array..
int y = random.nextInt(10) + 1;
z = x * y;
String hintStr = x + " × " + y;
((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
}
public void onCheck(View view) {
int answer = Integer.parseInt(textBox.getText().toString());
String res = "Incorrect";
if (answer == z) {
res = "Correct";
score++;
Log.d("TAG", "Correct."); // right answer
textBox.setText("");
setActual(); // let's regenerate values
} else if (score == 0) {
Log.d("TAG", "Incorrect."); // wrong answer
textBox.setText("");
setActual();
((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
} else {
Log.d("TAG", "Incorrect."); // wrong answer
textBox.setText("");
score--;
setActual();
}
((TextView) findViewById(R.id.scoreText)).setText("Score:" + ' ' + score);
((TextView) findViewById(R.id.result_text_view)).setText(res);
}
}
MainActivity.java (with onSaveInstanceState)
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private EditText textBox;
private int z;
public int score = 0;
private int x;
private int y;
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("x", x);
savedInstanceState.putInt("y", y);
savedInstanceState.putInt("score", score);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox = findViewById(R.id.editText);
textBox.requestFocus();
setActual();
if (savedInstanceState != null) {
x = savedInstanceState.getInt("x");
y = savedInstanceState.getInt("y");
score = savedInstanceState.getInt("score");
}
}
private void setActual() {
Random random = new Random();
x = random.nextInt(10) + 1; //you get a random number between 0 and 9 then add 1 to get 1 - 10, no need to use array..
y = random.nextInt(10) + 1;
z = x * y;
String hintStr = x + " × " + y;
((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
}
public void onCheck(View view) {
int answer = Integer.parseInt(textBox.getText().toString());
String res = "Incorrect";
if (answer == z) {
res = "Correct";
score++;
Log.d("TAG", "Correct."); // right answer
textBox.setText("");
setActual(); // let's regenerate values
} else if (score == 0) {
Log.d("TAG", "Incorrect."); // wrong answer
textBox.setText("");
setActual();
((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
} else {
Log.d("TAG", "Incorrect."); // wrong answer
textBox.setText("");
score--;
setActual();
}
((TextView) findViewById(R.id.scoreText)).setText("Score:" + ' ' + score);
((TextView) findViewById(R.id.result_text_view)).setText(res);
}
}

You should consider calling super.onSaveInstanceState()
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
// save your state
super.onSaveInstanceState(outState);
}
Also, to avoid recreating the activity everytime the screen is rotated you could handle config change yourself by adding this to your Activity declaration in the app manifest :
android:configChanges="keyboardHidden|orientation|screenSize"
And override :
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
It could save time for the user if your app doesn't need to be recreated after orientation or screenSize changes...

Are you sure values are not retrieved?
I believe the issue here is that you always call setActual() in onCreate() which generates x and y values and assign z value to the generated x and y (even when savedInstanceState != null)
better way to handle things is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox = findViewById(R.id.editText);
textBox.requestFocus();
if (savedInstanceState != null) {
x = savedInstanceState.getInt("x");
y = savedInstanceState.getInt("y");
score = savedInstanceState.getInt("score");
z = x * y;
String hintStr = x + " × " + y;
((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
} else { setActual(); }
}
You should also call super.onSaveInstanceState(savedInstanceState) like that
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("x", x);
savedInstanceState.putInt("y", y);
savedInstanceState.putInt("score", score);
}

Related

Android Java : Error while running countdown timer

I need your help to find an error in my "test" android application.
Until I create the second function "setProgessBar" the app runs without error, but now the error occur everytime. To comment the function call out has no effect.
Here is the code:
package my.timer;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Timer1 extends AppCompatActivity {
private int arrayPosition = 0;
private long timeValue = 0;
private boolean indicator = false;
private double progress;
private final String[] actions = {"Phase1" , "Phase2" , "Phase3" , "Phase4"};
private final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer1);
final Button startButton = (Button) findViewById(R.id.startButton);
final TextView text1 = (TextView) findViewById(R.id.textView3);
final TextView text2 = (TextView) findViewById(R.id.actionParameter);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text2.setText(actions[arrayPosition]);
text1.setText("" + timeValue + "");
new CountDownTimer(240000, 100) {
public void onTick(long millisUntilFinished) {
timeValue = (millisUntilFinished / 1000) % 4;
text1.setText("" + timeValue + "");
if (timeValue == 3 && indicator){
if (arrayPosition < 3){
arrayPosition++;
} else {
arrayPosition = 0;
}
indicator = false;
setProgressBar(arrayPosition);
text2.setText(actions[arrayPosition]);
}
if (timeValue == 1){
indicator = true;
}
}
public void onFinish() {
text1.setText("Geschafft :)");
}
}.start();
}
});
}
private void setProgressBar(int progressStatus) {
switch (progressStatus){
case 0:
progress = progress + 0.25;
break;
case 2:
progress = progress - 0.25;
break;
}
progressBar.setProgress((int)progress);
}
}
Many thanks in advance
Tim

Android Studio - Need help looping code every second

I need a system that will execute this code every second. I've been looking for a while and still can't find any ways that work. I'm quite new to Java programming and programming for android so any help will be greatly appreciated.
Values n = new Values();
double num = n.getNum();
double adder = n.getAdder();
if(adder>=1) {
num += (adder * 0.1);
}
TextView t = (TextView) findViewById(R.id.textView1);
t.setText(num + "");
n.setNum(num);
Also, I'm importing values from a Values class that looks like this,
package com.example.durtle02.durtle02;
public class Values {
double num;
double cost = 30;
double adder;
public double getNum() {
return num;
}
public void setNum(double num) {
this.num = num;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public double getAdder() {
return adder;
}
public void setAdder(double adder) {
this.adder = adder;
}
}
EDIT
MainActivity.java
package com.example.durtle02.durtle02;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.sql.Time;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//making game values accessible
final Values n = new Values();
//Loading The button for adding
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double num = n.getNum();
num++;
TextView tt = (TextView) findViewById(R.id.textView1);
n.setNum(num);
tt.setText(n.getNum() + "");
}
});
final Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double num = n.getNum();
double cost = n.getCost();
double adder = n.getAdder();
if (num >= cost) {
num -= cost;
cost = cost * 1.35;
adder++;
cost = Math.round(cost);
n.setNum(num);
n.setCost(cost);
n.setAdder(adder);
TextView tl = (TextView) findViewById(R.id.textView3);
tl.setText(n.getCost() + "");
TextView tk = (TextView) findViewById(R.id.textView4);
tk.setText(n.getAdder() + " Adders");
TextView tt = (TextView) findViewById(R.id.textView1);
tt.setText(n.getNum() + "");
TextView tp = (TextView) findViewById(R.id.textView2);
tp.setText((n.getAdder() * 0.1) + "/s");
}
}
});
countDownTimer.start();
}
// Do something each second in a time frame of 60 seconds.
CountDownTimer countDownTimer = new CountDownTimer(60000, 100) {
public void onTick(long millisUntilFinished) {
Values n = new Values();
double num = n.getNum();
double adder = n.getAdder();
if(adder>=1) {
num += (adder * 0.1);
}
num++;
n.setNum(num);
}
public void onFinish() {
countDownTimer.start(); // restart again.
}
};
/*
//---------------------------------------------------------------
Values n = new Values();
double num = n.getNum();
double adder = n.getAdder();
if(adder>=1) {
num += (adder * 0.1);
}
TextView t = (TextView) findViewById(R.id.textView1);
t.setText(num + "");
n.setNum(num);
//--------------------------------------------------------------
*/
}
I think you can use CountDownTimer, something like this:
// Do something each second in a time frame of 60 seconds.
CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
// For every second, do something.
doSomething();
}
public void onFinish() {
countDownTimer.start(); // restart again.
}
}.start();
If your activity is in the foreground, the following code will do.
Handler mHandler= new Handler()
final Runnable runnable = new Runnable() {
#Override
public void run() {
// do your stuff here, called every second
mHandler.postDelayed(this, 1000);
}
};
// start it with:
mHandler.post(runnable);

Stucked on Loops, not sure what is the wrong

I'm a just a beginner to android & java. I'm trying to build an app that will tell you if you win or lose base on the Martingale on gambling.
My concept is, you can set your money, target and the minimum bet.
for example is if I set my current money is 1000, and my target is to get 1100, and the minimum bet is 100, the app will auto run the function for example 10 times and calculate the win rate.
now I'm stucked, the android studio shows no error, however, whenever i try to emulate the app, it show "unfortunately, app has stopped" everything seem just fine to me, i don't know how to find the error... please guide me. million thanks
package com.example.android.gambling;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
EditText cMoney = (EditText) findViewById(R.id.money);
double currentMoney = Double.parseDouble(cMoney.getText().toString());
EditText target = (EditText) findViewById(R.id.target);
double theTarget = Double.parseDouble(target.getText().toString());
EditText bet = (EditText) findViewById(R.id.bet);
double minBet = Double.parseDouble(bet.getText().toString());
boolean findRate = calRate(currentMoney, theTarget, minBet);
public void seeRate(View view) {
TextView textview = (TextView)findViewById(R.id.textView);
textview.setText("You " + winPercentage());
}
public boolean calRate(double currentMoney, double theTarget, double minBet) {
while (currentMoney>minBet){
boolean win = winRate();
if (win){
currentMoney += minBet;
minBet = minBet;
}
else {
currentMoney -= minBet;
minBet *= 2;
}
if (currentMoney>=theTarget){
return true;
}
}
return false;
}
private boolean winRate() {
double d = Math.random();
if (d < 0.5)
return true;
else
return false;
}
public int winPercentage (){
int numberWin = 0;
for (int i=0; i<=10; i++){
boolean win = calRate(currentMoney, theTarget, minBet);
if (win){
numberWin = numberWin + 1;
}
}
return numberWin/10*100;
}
}
Edit(1)
package com.example.android.gambling;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText cMoney = (EditText) findViewById(R.id.money);
double currentMoney = Double.parseDouble(cMoney.getText().toString());
EditText target = (EditText) findViewById(R.id.target);
double theTarget = Double.parseDouble(target.getText().toString());
EditText bet = (EditText) findViewById(R.id.bet);
double minBet = Double.parseDouble(bet.getText().toString());
boolean findRate = calRate(currentMoney, theTarget, minBet);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void seeRate(View view) {
TextView textview = (TextView)findViewById(R.id.textView);
textview.setText("You " + winPercentage());
}
public boolean calRate(double currentMoney, double theTarget, double minBet) {
while (currentMoney>minBet){
boolean win = winRate();
if (win){
currentMoney += minBet;
minBet = minBet;
}
else {
currentMoney -= minBet;
minBet *= 2;
}
if (currentMoney>=theTarget){
return true;
}
}
return false;
}
private boolean winRate() {
double d = Math.random();
if (d < 0.5)
return true;
else
return false;
}
public int winPercentage (){
int numberWin = 0;
for (int i=0; i<=10; i++){
boolean win = calRate(currentMoney, theTarget, minBet);
if (win){
numberWin = numberWin + 1;
}
}
return numberWin/10*100;
}
}
Do this:
private EditText cMoney;
private double currentMoney;
private EditText target;
private double theTarget;
private EditText bet;
private double minBet;
private boolean findRate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cMoney = (EditText) findViewById(R.id.money);
currentMoney = Double.parseDouble(cMoney.getText().toString());
target = (EditText) findViewById(R.id.target);
theTarget = Double.parseDouble(target.getText().toString());
bet = (EditText) findViewById(R.id.bet);
minBet = Double.parseDouble(bet.getText().toString());
findRate = calRate(currentMoney, theTarget, minBet);
}
You can't put the code like this outside of any method/constructor, unless it's static/initializer block and there's definitely no reason to use them here.
Also, I see that your parameter name in calRate is currentMoney same as your variable, try to avoid this, it may be confusing for someone else reading your code, for you and maybe even for compiler if you go too far.
Put this:
EditText cMoney = (EditText) findViewById(R.id.money);
double currentMoney = Double.parseDouble(cMoney.getText().toString());
EditText target = (EditText) findViewById(R.id.target);
double theTarget = Double.parseDouble(target.getText().toString());
EditText bet = (EditText) findViewById(R.id.bet);
double minBet = Double.parseDouble(bet.getText().toString());
boolean findRate = calRate(currentMoney, theTarget, minBet);
Inside brackets of method onCreate() and then export variables theTarget, minBet, findRate and currentMoney into fields so that they can be visible in other parts of this class.

Set empty edittext value to zero

I have created a simple program to try to figure out how to do this. it has two edit text fields (input type number decimal), a text view and a button. I want the sum of the two input fields to be displayed in the text view when the user hits the button. can someone tell me how to set the value of one edit text field to zero if the user left it blank? I have tried many ways but nothing worked.
Edit: i want to achieve this while keeping the hint of edit text as before (without changing the value to zero like " setText("0"); ".
here's my java code (tell me what to add)
package com.example.android.testedittexttozero;
import...
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void calculate(View v) { //the button
final EditText number1 = (EditText) findViewById(R.id.numberFirst);
EditText number2 = (EditText) findViewById(R.id.numberSecond);
TextView total = (TextView) findViewById(R.id.totalTV);
try {
int a = Integer.parseInt(number1.getText().toString());
int b = Integer.parseInt(number2.getText().toString());
int sum = a + b;
total.setText("" + sum);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "One or more field is empty", Toast.LENGTH_LONG).show();
}
}
}
Do you know that in java if you dont initialize a variable with a default value, it's default value be 0?
in short:
try {
int a;//by default, it will be 0;
int b = Integer.parseInt(number2.getText().toString());//let it be 2
int sum = a + b;
total.setText("" + sum);//Ans will be 2 only
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "One or more field is empty", Toast.LENGTH_LONG).show();
}
If you don't enter any value in a, it will be set to as 0. Like if you leave a blank, and enter 2 in second edittext, the ans will be two nevertheless..
public void ZeroingEmpytEditText() {
int f= 0; // Zeroing Factor
if (number1.getText().toString().length() > 0) {
a = Integer.parseInt(number1.getText().toString());
} else {
a=f;
}
if (number2.getText().toString().length() > 0) {
b = Integer.parseInt(number2.getText().toString());
} else {
b=f;
}
int sum = a + b ;
total.setText(sum + "");
}
you can set value 0 of edit text in oncreate method.
like,
public class MainActivity extends AppCompatActivity {
EditText number1,number2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (EditText) findViewById(R.id.numberFirst);
number2 = (EditText) findViewById(R.id.numberSecond);
number1.settext("0");
number2.settext("0");
}
public void calculate(View v) { //the button
TextView total = (TextView) findViewById(R.id.totalTV);
try {
int a = Integer.parseInt(number1.getText().toString());
int b = Integer.parseInt(number2.getText().toString());
int sum = a + b;
total.setText("" + sum);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "One or more field is empty", Toast.LENGTH_LONG).show();
}
}
}
its helpfull for you.

Input Validation not working android studio

For some reason my input Validation is not working. Every time I put calculate it crashes "app" when it should have an error saying that I need to input height/weight. When I do input the numbers it does calculate. Thanks for the help :). I'm new to android studio .
here is my calculation java file
public class BmiFrag extends Fragment implements View.OnClickListener {
Button BmiButton;
private double weight1=0;
private double height1=0;
public static EditText heightIn;
public static EditText weightIn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_bmi, container, false);
BmiButton = (Button) myView.findViewById(R.id.CalculateBmi);
BmiButton.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.CalculateBmi:
weightIn = (EditText)
getActivity().findViewById(R.id.ETtweight);
heightIn = (EditText) getActivity().findViewById(R.id.ETHeight);
final TextView tv4 = (TextView)
getActivity().findViewById(R.id.TFDisplayBmi);
String str1 = weightIn.getText().toString();
String str2 = heightIn.getText().toString();
float weight = Float.parseFloat(str1);
float height = Float.parseFloat(str2) ;
float bmiValue = calculateBMI(weight, height);
String bmiInterpretation = interpretBMI(bmiValue);
tv4.setText(String.valueOf(bmiValue + "-" + bmiInterpretation));
if (TextUtils.isEmpty(str1)) {
weightIn.setError("Please enter your weight");
weightIn.requestFocus();
return;
}
else if (TextUtils.isEmpty(str2)) {
heightIn.setError("Please enter your height");
heightIn.requestFocus();
return;
}
break;
}
}
private float calculateBMI(float weight, float height) {
float bmi= (float) (weight/ (height*height)*4.88);
float total= Math.round(bmi);
return total;
}
private String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return "Severely underweight";
} else if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue < 25) {
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onDestroy() {
}
#Override
public void onDetach() {
super.onDetach();
}
}
Try to change the code like the following,
if (TextUtils.isEmpty(str1)) {
weightIn.setError("Please enter your weight");
weightIn.requestFocus();
return;
}
else if (TextUtils.isEmpty(str2)) {
heightIn.setError("Please enter your height");
heightIn.requestFocus();
return;
}else{
float weight = Float.parseFloat(str1);
float height = Float.parseFloat(str2) ;
float bmiValue = calculateBMI(weight, height);
String bmiInterpretation = interpretBMI(bmiValue);
tv4.setText(String.valueOf(bmiValue + "-" + bmiInterpretation));
}
if (TextUtils.isEmpty(str1)) {
weightIn.setError("Please enter your weight");
weightIn.requestFocus();
return;
}
if (TextUtils.isEmpty(str2)) {
heightIn.setError("Please enter your height");
heightIn.requestFocus();
return;
}
firstly, sort your indentation and variable names out. Never name a variable str1, str2: always meaningful names. Indentation should always be consistent. This will help out but fixing in the future for readability and speed.
You're doing input validation after you actually input and assign things through
calculateBMI() method
That part of your code reads: Lets take text from text fields, interpret the BMI and then see if the textfields are empty

Categories