ElenJ ElenJ - 4 months ago 16
Android Question

Why can't button display score on the screen?

I am trying create a simple quiz app where depending from user choices in checkBoxes, submitButton displays the result on the screen. When answer is right score increase 1 point, if answer is wrong score decrease 1 point.
I am totaly stuck!

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
int score;
Button submitButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

submitButton = (Button) findViewById(R.id.submitAnswer);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
submitButton.setText(score);
}
});
}

public void checkBoxes(View view) {

CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
boolean hasCheckBox1 = checkBox1.isChecked();

CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
boolean hasCheckBox2 = checkBox2.isChecked();

boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()){
case R.id.checkBox1:
Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
score ++;
break;
case R.id.checkBox2:
Toast.makeText(this,"Sorry, try again!", Toast.LENGTH_SHORT).show();
score --;
break;
}
}
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.allyouask.practicebutton.MainActivity">

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RightAnswer"
android:id="@+id/checkBox1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="179dp"
android:onClick="checkBoxes"/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WrongAswer"
android:id="@+id/checkBox2"
android:layout_below="@+id/checkBox1"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:onClick="checkBoxes"/>

<Button
android:id="@+id/submitAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="20sp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="onClick"/>

</RelativeLayout>

ddb ddb
Answer

you have to call an update method in order to update text on the Button or update its content other way, like this below

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    int score;
    Button submitButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        submitButton = (Button) findViewById(R.id.submitAnswer);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitButton.setText(String.valueOf(score));
            }
        });
    }

    public void checkBoxes(View view) {

        CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        boolean hasCheckBox1 = checkBox1.isChecked();

        CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        boolean hasCheckBox2 = checkBox2.isChecked();

        boolean checked = ((CheckBox) view).isChecked();
        switch(view.getId()){
            case R.id.checkBox1:
                Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
               score ++;
                break;
            case R.id.checkBox2:
                Toast.makeText(this,"Sorry, try again!", Toast.LENGTH_SHORT).show();
                score --;
                break;
        }
        //submitButton.setText(String.valueOf(score));
    }
}