boolean variable always equals false - java

When opening the menu in my application it shows an extra option depending on the state of the game (paused or not started) however the controlling variable for what the button should do always returns as false.
I have attempted splitting the if statements into separate statements and performing the processing that was accepting the hit to performance but no changes happened to the outputs, upon hovering over the else if statement within the btnMenu method onClick it says:
Condition '!paused' is always 'true' when reached
related code below:
Button btnStart;
Button btnMenu;
Button btnQuit;
Button btnShop;
Button btnSettings;
ImageView swipeDetector;
boolean menuToggle = false;
int gameRunning = 0;
boolean paused = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//calling object from activity_main.xml
btnStart = (Button) findViewById(R.id.startButton);
btnMenu = (Button) findViewById(R.id.menuButton);
btnQuit = (Button) findViewById(R.id.quitBtn);
btnShop = (Button) findViewById(R.id.shopButton);
btnSettings = (Button) findViewById(R.id.settingsBtn);
swipeDetector = (ImageView) findViewById(R.id.swiperReader);
btnQuit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(gameRunning == 1) {
gameRunning = 0;
menuToggle = false;
paused = false;
//set game variables back to defaults
btnQuit.setVisibility(View.GONE);
btnSettings.setVisibility(View.GONE);
btnShop.setVisibility(View.VISIBLE);
btnStart.setVisibility(View.VISIBLE);
}
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gameRunning = 1;
menuToggle = false;
paused = false;
btnStart.setVisibility(View.GONE);
btnShop.setVisibility(View.GONE);
}
});
//broken, for some reason gameRunning always == 0
btnMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (gameRunning == 1){
paused = true;
}
if (!menuToggle && paused){
btnStart.setVisibility(View.GONE);
btnSettings.setVisibility(View.VISIBLE);
swipeDetector.setVisibility(View.GONE);
btnQuit.setVisibility(View.VISIBLE);
menuToggle = true;
//broken if statement
}else if(!menuToggle && !paused){
btnStart.setVisibility(View.GONE);
btnSettings.setVisibility(View.VISIBLE);
swipeDetector.setVisibility(View.GONE);
menuToggle = true;
} else {
paused = false;
btnSettings.setVisibility(View.GONE);
btnQuit.setVisibility(View.GONE);
btnStart.setVisibility(View.GONE);
swipeDetector.setVisibility(View.VISIBLE);
if(gameRunning == 0) {
btnStart.setVisibility(View.VISIBLE);
}
menuToggle = false;
}
}
});
There was no problems with the program as i was using a code together feature with some friends at university the current version of the application would not upload to the virtual device and so after hours of trying to find a solution to some very basic code it turned out that it was an issue with Floobits code together. Sorry for the confusion.
the solution as mentioned in the first comment was that this was stating my the time the else if statement was reached the only possible scenario would be the value would be true not that the value is always true throughout the code.

You are declaring a boolean condition that must be true, remove the !menuToggle from both the if and else if and it becomes more clear. Often, when I get into these situations I spend time cleaning up my code first and that often helps me sort out the issue. Here is an example of the last onClick method cleaned up.
public void onClick(View view) {
if (!menuToggle) {
btnStart.setVisibility(View.GONE);
btnSettings.setVisibility(View.VISIBLE);
swipeDetector.setVisibility(View.GONE);
menuToggle = true;
if(paused) {
btnQuit.setVisibility(View.VISIBLE);
}
} else {
btnSettings.setVisibility(View.GONE);
btnQuit.setVisibility(View.GONE);
btnStart.setVisibility(View.GONE);
swipeDetector.setVisibility(View.VISIBLE);
if(gameRunning == 0) {
btnStart.setVisibility(View.VISIBLE);
}
paused = false;
menuToggle = false;
}
}

Related

TextView refuses to change text

So I'm making a game in Android Studio where you are supposed to tap the circle as many times as you can in 60 seconds.I have specific TextView's for time and the score that you achieved BUT! before the first tap the time says "Tap to start" so that the timer starts when the user want's and not on it's own.
My problem is that i have a Reset button under the circle where the user can restart back to the start the score is saved and the time should say "Tap to start" but the counter just freezes and does nothing until the user tap's again and it start's as normal.I can't figure out why it won't change it's value to "Tap to start" when i told it to do so in the On click listener from the Reset button
the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final Button button = (Button) findViewById(R.id.button);
final TextView txtview2 = (TextView) findViewById(R.id.textView2);
final TextView txtview = (TextView) findViewById(R.id.textView);
updatetime(txtview2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(started == false)
{
reverseTimer(60,txtview2,button,txtview);
started = true;
}
score++;
randnum = r.nextInt(10 - 0) + 0;
if(randnum == 0)
button.setBackground(getResources().getDrawable(R.drawable.circle));
else if(randnum == 1)
button.setBackground(getResources().getDrawable(R.drawable.circle1));
else if(randnum == 2)
button.setBackground(getResources().getDrawable(R.drawable.circle2));
else if(randnum == 3)
button.setBackground(getResources().getDrawable(R.drawable.circle3));
else if(randnum == 4)
button.setBackground(getResources().getDrawable(R.drawable.circle4));
else if(randnum == 5)
button.setBackground(getResources().getDrawable(R.drawable.circle5));
else if(randnum == 6)
button.setBackground(getResources().getDrawable(R.drawable.circle6));
else if(randnum == 7)
button.setBackground(getResources().getDrawable(R.drawable.circle7));
else if(randnum == 8)
button.setBackground(getResources().getDrawable(R.drawable.circle8));
else if(randnum == 9)
button.setBackground(getResources().getDrawable(R.drawable.circle9));
button.setText(String.valueOf(score));
}
});
Button reset_button = (Button) findViewById(R.id.button3);
reset_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Reset(txtview2,button);
started = true;
}
});
}
My reset function:
public void Reset(TextView txt,Button button)
{
updatetime(txt);
score = 0;
button.setText(String.valueOf(score));
button.setBackground(getResources().getDrawable(R.drawable.circle));
}
set high score is actuall just set score...
public void updatetime(TextView txt)
{
txt.setText("Time : Tap to start");
}
public void sethighscore(TextView txt)
{
txt.setText("Score: " + String.valueOf(score));
}
My timer that counts from 60 to 0:
public void reverseTimer(int Seconds,final TextView tv,final Button button,final TextView txt2){
CountDownTimer CountDownTimer1 = new CountDownTimer(Seconds* 1000+1000, 1000) {
public void onTick(long millisUntilFinished) {
if(clicked)
{
this.cancel();
clicked = false;
if(score > highscore)
sethighscore(txt2);
Reset(tv,button);
started = false;
}
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clicked = true;
}
});
seconds = seconds % 60;
tv.setText("Time : " + String.valueOf(minutes)
+ ":" + String.valueOf(seconds));
}
public void onFinish() {
if(score > highscore)
sethighscore(txt2);
tv.setText("Completed");
Reset(tv,button);
}
}.start();
}
What happening is that textView is defined in main thread i.e UI thread. Setting text in it wont work on another thread.what u have to do is make a handler object.
Handler handler=new Handler(getApplicationContext().getMainLooper());
now the place where u want to do the textView.setText("string"); add this code there.
handler.post(new Runnable() {
#Override
public void run() {
textView.setText("string");
}
});

CountDownTimer not initiating in if statement

My problem is towards the bottom, I have a boolean, ongoing, which is false. It is supposed to become true with a button press, then initiate a countdown timer, but the timer is not initiated. The timer works placed elsewhere in the code. Sorry if it's a complete mess I'm new to this.
public int counter;
public boolean ongoing = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//0-59 number picker to represent seconds
final NumberPicker secPicker = (NumberPicker) findViewById(R.id.secPicker);
secPicker.setMaxValue(59);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnSet = (Button) findViewById(R.id.btnSet);
Button btnPause = (Button) findViewById(R.id.btnPause);
final TextView label = (TextView) findViewById(R.id.txtCount);
//sets the time chosen to timer with button press btnSet
btnSet.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
counter = secPicker.getValue();
label.setText(String.valueOf(counter));
}
}
);
//starts timer from selected time, from number picker, with btnStart
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ongoing = true;
}
});
//disables, not worried about this yet
btnPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ongoing = false;
}
});
//supposed to turn on a timer with button press btnStart, but it does nothing this is my problem.
if (ongoing == true) {
//any replacement for 9999999, as an infinite value?
new CountDownTimer(999999999, 1000) {
public void onTick(long millisUntilFinished) {
label.setText(String.valueOf(counter));
counter--;
if (counter == -1) {
counter = 0;
cancel();
}
}
public void onFinish() {
}
}.start();
}
}
}
if (ongoing == true) // this will never work for you
You have putted above code inside oncreate before you click anything it will get call.
Either you should write below code inside btnStart click event or create one method and call from it.
//supposed to turn on a timer with button press btnStart, but it does nothing this is my problem.
if (ongoing == true) {
//any replacement for 9999999, as an infinite value?
new CountDownTimer(999999999, 1000) {
public void onTick(long millisUntilFinished) {
label.setText(String.valueOf(counter));
counter--;
if (counter == -1) {
counter = 0;
cancel();
}
}
public void onFinish() {
}
}.start();
}
Hope these help you.

Java global int

In an Android Studio app, I have following code:
boolean clicked = true;
Button btnumb1 = (Button) this.findViewById(R.id.numb1);
Button btnumb2 = (Button) this.findViewById(R.id.numb2);
Button btnumb3 = (Button) this.findViewById(R.id.numb3);
Button btnumb4 = (Button) this.findViewById(R.id.numb4);
btnumb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clicked = false;
}
});
Button Start = (Button) this.findViewById(R.id.Start);
Start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clicked == false) {
Random rand = new Random();
int n = rand.nextInt(4) + 1;
TextView textView = (TextView) findViewById(R.id.randOutput);
textView.setText("" + n);
}
else{
Toast.makeText(getApplicationContext(), "You have to choose a number", Toast.LENGTH_LONG).show();
}
}
});
The idea is that when one of the 4 buttons is clicked, the int clicked is set to 1 and so the final button can only be clicked when it is 1.
But the code doesn't work like this; int clicked = 0; can't be accessed in other public void.
If one of the numbers 1,2,3,4 is clicked then final button can be clicked
The fix: Put public static boolean clicked = false;
under your public class YourClassName { line.
Reasoning: You need to learn how to scope your variables properly. You declared boolean clicked inside the onCreate() function, so the variable is gone after onCreate() is done running.
You should have put the clicked variable inside the class scope level, via public boolean clicked or public static boolean clicked so that even after the function returns, the value is saved.
I highly recommend a beginners java course or textbook before continuing on with your project(s).
Java does not have any concept of global variables. There is either an instance variable or a class variable. To define Global Variable you can make use of static Keyword.
static boolean clicked = true;
Button btnumb1 = (Button) this.findViewById(R.id.numb1);
Button btnumb2 = (Button) this.findViewById(R.id.numb2);
Button btnumb3 = (Button) this.findViewById(R.id.numb3);
Button btnumb4 = (Button) this.findViewById(R.id.numb4);
btnumb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clicked = false;
}
});
Button Start = (Button) this.findViewById(R.id.Start);
Start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clicked == false) {
Random rand = new Random();
int n = rand.nextInt(4) + 1;
TextView textView = (TextView) findViewById(R.id.randOutput);
textView.setText("" + n);
}
else{
Toast.makeText(getApplicationContext(), "You have to choose a number", Toast.LENGTH_LONG).show();
}
}
});
Yes the variable int clicked = 0; has to be declared final before it is accessible in the other public void functions.
Try this declared a class say ClassClicked
public class ClassClicked {
int clicked;
public int getClicked() {
return clicked;
}
public void setClicked(int clicked) {
this.clicked = clicked;
}
}
Then changed
int clicked = 0;
to
final ClassClicked clicked = new ClassClicked();
clicked.setClicked(0);
And
clicked = 1;
to
clicked.setClicked(1);
Finally
clicked == 1
to
clicked.getClicked() == 1

setPressed is not working

I have this in my code and the .setPressed doesn't work:
Button btndesligado = (Button) findViewById(R.id.button12);
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});
What is wrong? It doesn't give me any error, it just doesn't work when I open it and click it. It was suposed to be pressed after I press once.
Android is changing the setPressed both before and after your onClickEvent
so change your code this code
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});
to this
btndesligado.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// show interest in events resulting from ACTION_DOWN
if (event.getAction() == MotionEvent.ACTION_DOWN) return true;
// don't handle event unless its ACTION_UP so "doSomething()" only runs once.
if (event.getAction() != MotionEvent.ACTION_UP) return false;
btndesligado.setPressed(true);
som = false;
vibrar = false;
return true;
}
});
It seems nothing wrong with your codes, It's working as you did, It sets the View's internal state to "pressed", or false to reverts the View's internal state from a previously set "pressed" state. And you've put that inside onClick of button means it acts only after you press the button,
If you want it pressed or revert then do like this,
Button btndesligado = (Button) findViewById(R.id.button12);
btndesligado.setPressed(true);
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
som = false;
vibrar = false;
}
});
Try this way.
btndesligado.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});

How can I write "if button clicked" in an If statement in android studio?

I am building a memory game with four cards(2x2). These four cards have an onClick named "cards". This onClick consists of an If statement that flips the cards back if they are not the same, and keeps them if they are the same.
The front image of the card is the same for the 4, but the back has different images.My problem is that I want the cards to flip, but they already have an onClick. So how can I write "if button clicked" in an If statement or is there another solution?
EDIT:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button1.setVisibility(View.INVISIBLE);
pic1 = (ImageView) findViewById(R.id.imageView);
pic2 = (ImageView) findViewById(R.id.imageView2);
pic3 = (ImageView) findViewById(R.id.imageView3);
pic4 = (ImageView) findViewById(R.id.imageView4);
pic1.setImageResource(R.drawable.img1);
pic2.setImageResource(R.drawable.img1);
pic3.setImageResource(R.drawable.img1);
pic4.setImageResource(R.drawable.img1);
pic1.setVisibility(View.VISIBLE);
pic2.setVisibility(View.VISIBLE);
pic3.setVisibility(View.VISIBLE);
pic4.setVisibility(View.VISIBLE);
}
});
}
public void cards(View v) {
if (v.getId() == pic1.getId() ) {
pic1.setImageResource(R.drawable.img2);
pic1.setTag("img2");
} else if (v.getId() == pic2.getId()) {
pic2.setImageResource(R.drawable.img2);
pic2.setTag("img2");
} else if (v.getId() == pic3.getId()) {
pic3.setImageResource(R.drawable.img3);
pic3.setTag("img3");
} else if (v.getId() == pic4.getId()) {
pic4.setImageResource(R.drawable.img3);
pic4.setTag("img3");
}
if (R.drawable.img2 == R.drawable.img2) {
pic1.setImageResource(R.drawable.img2);
pic1.getTag();
pic2.setImageResource(R.drawable.img2);
pic2.getTag();
}
if (R.drawable.img3 == R.drawable.img3) {
pic3.setImageResource(R.drawable.img3);
pic3.getTag();
pic4.setImageResource(R.drawable.img3);
pic4.getTag();
}
if (R.drawable.img2 != R.drawable.img2 || R.drawable.img3 != R.drawable.img3) {
pic1.setImageResource(R.drawable.img1);
pic2.setImageResource(R.drawable.img1);
pic3.setImageResource(R.drawable.img1);
pic4.setImageResource(R.drawable.img1);
}
}
SECOND METHOD I'M TRYING: #Override
public void onClick(View v) {
button1.setVisibility(View.INVISIBLE);
pic1 = (ImageView) findViewById(R.id.imageView);
pic2 = (ImageView) findViewById(R.id.imageView2);
pic3 = (ImageView) findViewById(R.id.imageView3);
pic4 = (ImageView) findViewById(R.id.imageView4);
pic1.setImageResource(R.drawable.img1);
pic2.setImageResource(R.drawable.img1);
pic3.setImageResource(R.drawable.img1);
pic4.setImageResource(R.drawable.img1);
pic1.setVisibility(View.VISIBLE);
pic2.setVisibility(View.VISIBLE);
pic3.setVisibility(View.VISIBLE);
pic4.setVisibility(View.VISIBLE);
if (R.drawable.img2 == R.drawable.img2) {
pic1.setImageResource(R.drawable.img2);
pic2.setImageResource(R.drawable.img2);
}
if (R.drawable.img3 == R.drawable.img3) {
pic3.setImageResource(R.drawable.img3);
pic4.setImageResource(R.drawable.img3);
}
if (R.drawable.img2 != R.drawable.img2 || R.drawable.img3 != R.drawable.img3) {
pic1.setImageResource(R.drawable.img1);
pic2.setImageResource(R.drawable.img1);
pic3.setImageResource(R.drawable.img1);
pic4.setImageResource(R.drawable.img1);
}
}
});
}
public void pic1Click(View v){
pic1.setImageResource(R.drawable.img1);
}
public void pic2Click(View v){
pic2.setImageResource(R.drawable.img1);
}
public void pic3Click(View v){
pic3.setImageResource(R.drawable.img2);
}
public void pic4Click(View v){
pic4.setImageResource(R.drawable.img2);
}
So I think you are looking for a way to figure out which ImageButton the user clicked. You can get this from the view variable being passed to the onClick method.
You also need to keep track of which images have been clicked. You can keep track of this by adding a tag to the image.
public void myClickMethod(View v){
if (v.getId() == pic1.getId() ) {
pic1.setImageResource(R.drawable.img2);
pic1.addTag("img2");
} else if (v.getId() == pic2.getId() {
pic2.setImageResource(R.drawable.img2);
pic2.addTag("img2");
} else if (v.getId() == pic3.getId() {
pic3.setImageResource(R.drawable.img3);
pic3.addTag("img3");
} else if (v.getId() == pic4.getId() {
pic4.setImageResource(R.drawable.img3);
pic4.addTag("img3");
}
Of course you will want to set the tag every time you change the image (and the first time you create the image). So to check if you need a reset, you would need to check that two images have changed away from the default image they started with. You could also do this with global variables, but tags might be a little more intuitive.
You can reference the Android Dev Doc
At first, you need to know what is Event Listeners, and implement it.
After you did it,and then you can Register the Event Listeners with your implementation.
Example Code
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
...
}
Try this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//write your codes here
}
});
}

Categories

Resources