mmmartinnn mmmartinnn - 3 months ago 9
Java Question

How to use 2+ ToggleButtons to Get the Buttons' Total Value?

I'm trying to make an app (my first) that shows the value of binary numbers in a decimal display. I decided to use a

ToggleButton
with an image of a 1 or a 0 showing whether it is pressed or not. I managed to link '1' up to a
TextView
so that when when the
ToggleButton
is pressed it shows "Binary equals: 1" below it and likewise for 0.

I thought adding a second button would be easy but after 5 (10?) hours of Googling and experimenting I have no idea how to do that.

I have tried case breaks but as I have only been programming for two weeks (udacity Android Development for Beginners so far) I have no idea where to go next or if I'm on the right track—were
ToggleButtons
a bad idea, for example?

I think what I need to do is this:

1) If
toggle1
is selected, make
valueOfOnes
= 1, else
valueOfOnes
= 0

2) If
toggle2
(and eventually 4, 8, 16...) is selected make
valueOfTwos
= 2 else = 0

3) Make a method that adds
valueOfOnes
to
valueOfTwos
etc

4) Show that value in
Textview


Hope the code below doesn't look too messy. It's tidier than my brain was earlier on...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:padding="16dp"
tools:context="com.example.android.binary02.MainActivity">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="3">

<ToggleButton
android:id="@+id/toggle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/check"
android:focusable="false"
android:focusableInTouchMode="false"
android:textOff=""
android:textOn="" />

<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/check"
android:focusable="false"
android:focusableInTouchMode="false"
android:textOff=""
android:textOn="" />
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Binary equals: "
android:textSize="50sp" />

<TextView
android:id="@+id/decimal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp" />
</LinearLayout>
</LinearLayout>

CHECK XML

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, show one -->
<item android:drawable="@drawable/one"
android:state_checked="true" />
<!-- When not selected, show two-->
<item android:drawable="@drawable/zero"
android:state_checked="false"/>

</selector>


MainActivity.java:

package com.example.android.binary02;

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;
TextView decimalAnswer;
int valueOfOnes = 1;
int valueOfTwos = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggle1 = (ToggleButton) findViewById(R.id.toggle1);
toggle1.setOnCheckedChangeListener(this);

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

@Override
public void onCheckedChanged (CompoundButton compoundButton, boolean oneSelected) {
if(oneSelected) {
int valueOfOnes = 1;
addValues(valueOfOnes);
}
else
{
int valueOfOnes = 0;
addValues(valueOfOnes);

}
}

/** If this method is commented out and relevant changes made to addValues then one button works fine
*
* @param compoundButton
* @param oneSelected
*/
@Override
public void onCheckedChangedTwo (CompoundButton compoundButton, boolean twoSelected) {
if(twoSelected) {
int valueOfTwos = 2;
addValues(valueOfTwos);
}
else
{
int valueOfTwos = 0;
addValues(valueOfTwos);
}
}


/**
* Adds values together.
*
* If I delete the int valueOfTwos here and replace with +1 or +0 then the app works (but only for one button it looks like both buttons perform the same action)
*
*/
public void addValues(int valueOfOnes, int valueOfTwos) {
int totalValues;
totalValues = valueOfOnes + valueOfTwos;
displayDecimalAnswer(totalValues);
}

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


EDITED: In addition to Saran's answer below would attempting to incorporate thisanswer be a good path to follow?

Answer

In the end a combination of Saran's answer below, MRX's here and some lazy obstinacy from me got it to work.

@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);
    }