mmmartinnn mmmartinnn - 3 months ago 15
Android Question

Converting togglebutton boolean to integer values then add together in a textview

I want to use four togglebuttons (eventually eight, twelve or sixteen) and give a value to each depending on which position it has been switched to.

So

toggle1
will give a value of either one or zero and
toggle2
will give a value of either two or zero (tb4 4 or 0, tb8 8 or 0 etc)

I then want to add the current value of all buttons together and show in a text view.

I've just started with the first 2 but am not sure how to get those values into the
displayDecimalAnswer
method. I'm clearly missing something very obvious here as it sounds so simple.

Help me Obi Wan Kenobis you're my only hope(s).

package com.example.android.binary04;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;


import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;


public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {

ToggleButton toggle1;
ToggleButton toggle2;
ToggleButton toggle4;
ToggleButton toggle8;
TextView decimalAnswer;

int totalValues;
boolean toggle1Status;
boolean toggle2Status;
boolean toggle4Status;
boolean toggle8Status;

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


toggle1 = (ToggleButton) findViewById(R.id.toggle1);
toggle2 = (ToggleButton) findViewById(R.id.toggle2);
toggle4 = (ToggleButton) findViewById(R.id.toggle4);
toggle8 = (ToggleButton) findViewById(R.id.toggle8);
toggle1.setOnCheckedChangeListener(this);
toggle2.setOnCheckedChangeListener(this);
toggle4.setOnCheckedChangeListener(this);
toggle8.setOnCheckedChangeListener(this);
decimalAnswer = (TextView) findViewById(R.id.decimal);
}


@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (compoundButton == toggle1) {
if (isChecked) {
toggle1Status = true;
} else {
toggle1Status = false;
}
} else if (compoundButton == toggle2) {
if (isChecked) {
toggle2Status = true;
} else {
toggle2Status = false;
}
}

}
/**
* Here I wanted to give a int value to boolean whether each togglebutton
* is clicked or not. Then add the value of each button together
*/

int valueOfOnes = (toggle1Status) ? 1 : 0;
int valueOfTwos = (toggle2Status) ? 2 : 0;
int answer = valueOfOnes + valueOfTwos;



/**
* Displays decimal answer.
*/
public void displayDecimalAnswer(int answer) {
TextView decimalView = (TextView) findViewById(R.id.decimal);
decimalView.setText(String.valueOf(answer));
}

}

MRX MRX
Answer
     @Override 
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
    if (compoundButton == toggle1) {
        if (isChecked) {
            toggle1Status = true;
        } else { 
            toggle1Status = false;
        } 
    } else if (compoundButton == toggle2) {
        if (isChecked) {
            toggle2Status = true;
        } else { 
            toggle2Status = false;
        } 
    } 
 displayDecimalAnswer();
} 

    /** 
     * Displays decimal answer. 
     */ 
    public void displayDecimalAnswer() {
       int valueOfOnes = (toggle1Status) ? 1 : 0;//Since toggle1Status is class level variable it will be accessibible
       int valueOfTwos = (toggle2Status) ? 2 : 0;
       int answer = valueOfOnes + valueOfTwos;

        TextView decimalView = (TextView) findViewById(R.id.decimal);
        decimalView.setText(""+answer);
    } 
Comments