recheej
recheej

Reputation: 149

How to store Button Text of Buttons in a variable? Android Calculator Project

I've been working on an Android Calculator for a while now in Java, of course, eclipse. In my program, I want to be able to store the text of the android buttons as an an operator (theOperator) to test so I can do some code based on that. I have all my buttons set as some text using xml. Here's an example:

<Button
    android:id="@+id/bMultiply"
    android:layout_width="65dp"
    android:layout_height="65dp"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/bSix"
    //// android:text="*" ///// Right here is where the text of the button is set. 
    android:textSize="40dp"
    android:onClick="onClick"
     />

Now here's my main code:

package rechee.cool;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity {
    /** Called when the activity is first created. */
    public EditText display;
    double total1=0;
    double total2=0;
    char theOperator;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Associate the button variable with the xml reference
        display= (EditText) findViewById(R.id.editText1);
    }
    String display1= display.getText().toString();
    // I want to store the button text of the android button as btnText. How do I do this?
    public void getOperator(String btnText){
        theOperator = btnText.charAt(0);
        double displayValue= Double.parseDouble(display1);
        total1+=displayValue;
        display.setText("");
    }
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.bOne:
            display.append("1");
            break;
            case R.id.bTwo:
            display.append("2");
            break;
            case R.id.bThree:
            display.append("3");
            break;
            case R.id.bFour:
            display.append("4");
            break;
            case R.id.bFive:
            display.append("5");
            break;
            case R.id.bSix:
            display.append("6");
            break;
            case R.id.bSeven:
            display.append("7");
            break;
            case R.id.bEight:
            display.append("8");
            break;
            case R.id.bNine:
            display.append("9");
            break;
            case R.id.bZero:
            display.append("0");
            break;
            case R.id.bPoint:
            display.append(".");
            break;
            case R.id.bClear:
            display.setText("");
            break;
            case R.id.bAdd:
            getOperator(display1);
            //String theOperator= new String("+");
            break;
            case R.id.bEqual:
        }
    }
}

Upvotes: 2

Views: 6033

Answers (2)

Allen
Allen

Reputation: 36

To get the text of a button.

put a variable in your class

public class HelloAndroidActivity extends Activity {
    private String buttonText;
    private Button button;
...

then in the onCreate method get the button and text:

@Override
public void onCreate(Bundle savedInstanceState) {
    button = (Button)findViewById(R.id.bMultiply);
    buttonText = button.getText().toString();
}

good luck

Upvotes: 2

Brian
Brian

Reputation: 8095

I'm sorry but your wording is a bit funny. Were you looking for a way to get the text of the button because you can simply use button.getText(). Not sure if that's what you were looking for, please let me know.

Upvotes: 1

Related Questions