Calculator: OnClick (button) = value 1

0 Marco Falanga · September 24, 2015
Hi guys. How are u?
I'm developing a calculator but I have some 
problems. I'm using a GriddleLayout for buttons 
And operator and a textview to show values 
And operations. 
First problem: how can I "give" to the button1 
The value "1"? And how can I do to show it in
the textview? 
For the moment this is the biggest problem, then
I'll ask u guys more (sorry ????????).
Can u help me? 
Thank you for all.

And sorry for my English.

Post a Reply

Replies

Oldest  Newest  Rating
+2 Milan Obrenovic · September 24, 2015
use a property text to set a text to the button.
to show it in a textview, use a method getText().
0 Bruno Laurence · September 24, 2015
First of all make an array of buttons, and then assign values like this :


buttonArray = new JButton[9];

// Create numeric Jbuttons
for (int i = 0; i
0 Marco Falanga · September 24, 2015
okay but i mean: i press on the button 1, then there is on the textview "1".
then i press the button "plus" and the Textview will show "1+". then for exapmle i press on the button 2 and on the textview there is "1+2". after that i press the button "=" and the textview will show:
"1+2="
"3"

did i explain myself well? sorry for the english. :D

this is what i did since today:
but its not what i want to do


public class CalcMain extends ActionBarActivity {
private TextView calcme;
/* private Button one;
private Button plus;
private Button results;
private Button two;

//variabili
double a = 0, result = 0;*/

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

private void Scanning() {
/*one = (Button) findViewById(R.id.button1);
two = (Button) findViewById(R.id.button2);
plus = (Button) findViewById(R.id.buttonplus);
results = (Button) findViewById(R.id.buttonresult);*/
calcme = (TextView) findViewById(R.id.textView);
calcme.setText("0");

}

static boolean control=true;
public void num_Clicked(View sender) {
Button bt = (Button) sender;
if (calcme.getText().length() > 7) return;
if (control) {

calcme.setText(bt.getText());
control = false;
} else {
calcme.append(bt.getText());
}
}

static int accumulator = 0;
static short operationtodo=0;
public void op_Clicked(View sender)
{
Button bt = (Button)sender;
switch (operationtodo)
{
case 0:
accumulator+=Integer.parseInt(calcme.getText().toString());
break;
case 1:
accumulator-=Integer.parseInt(calcme.getText().toString());
break;
case 2:
accumulator*=Integer.parseInt(calcme.getText().toString());
break;
case 3:
accumulator /= Integer.parseInt(calcme.getText().toString());
break;
}
calcme.setText(Integer.toString(accumulator));
if(bt.getText().toString().equals("+")) operationtodo = 0;
if(bt.getText().toString().equals("-")) operationtodo = 1;
if(bt.getText().toString().equals("/")) operationtodo = 2;
if(bt.getText().toString().equals("*")) operationtodo = 3;
//if(bt.getText().toString().equals(".")) operationtodo = 0;
control = true;

}
0 Marco Falanga · September 25, 2015
(i dont know how to "cit" that :D)
Bruno Laurence
""""First of all make an array of buttons, and then assign values like this :



buttonArray = new JButton[9];

// Create numeric Jbuttons
for (int i = 0; i


Bruno I didnt understand what you mean. Firstly i have to make an array of buttons, but then i didnt get it. Could you explain me please? (sorry i just started with android and java)

thank you :)
0 Bruno Laurence · September 25, 2015
buttonArray = new JButton[9];
// Create numeric Jbuttons
for (int i = 0; i
+1 Bob Kako · September 25, 2015
Hi, I am new, too, but maybe this will help.  

If you want to print the equation to a TextView then you will have to store the values somewhere.  You can use an array, I used ArrayList since you don't know how long the user will keep entering values and operators (+-/*).  It would resemble something like this...

// create the arraylist
public ArrayList<String> listOfValues = new ArrayList<String>();
public TextView displayEquation;

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

// I used EditText for a number so I am grabbing it here
value = (EditText) findViewById(R.id.firstOperatorEditText);

// get the TextView from your activity
displayEquation = (TextView) findViewById(R.id.equationTextView);

}


//when the user clicks on +  , you will need an onClick for each button

public void onClickPlus(View view){

// this gets the value from a EditText
listOfValues.add(value.getText().toString());

// since we clicked on the + button we know it's an add
listOfValues.add("+");

printEquation();


}


public void printEquation(){

String listFromArray = " ";

     for (int i = 0; i < listOfValues.size(); i++) {
          listFromArray += listOfValues.get(i);
     }

     displayEquation.setText(listFromArray);

}

... if you want to actually do the math you can just pull from the arraylist and perform calculations endlessly if you want.

Does that make sense?  I think that should work for you.
0 Marco Falanga · September 27, 2015
Thank you Bruno and Bob.
The only one problem is: im doing this app in Java to understand how do that in android. 
I didn't understand how i can create the arraylist of numbers for each buttons. 
Bruno after: for (int i = 0; i.....) what should i do? Thank you,
Bob what you did is so interesting, but here:

// create the arraylist
public ArrayList<String> listOfValues = new ArrayList<String>();
public TextView displayEquation;

i dont understand what do i have to do. could you explain me?
(sorry im studying java slowly :D) thank you guys :P
0 Bob Kako · September 28, 2015
Yeah, it's not going to be quick now that I am thinking about it.  You will have to loop thru the ArrayList and keep a running sum and the previous value from the list.  If you pull up a + then you will have to move to the next value and do the addition with the sum and the value you moved to next.  Oh, and you will have to cast your String to Int  (Integer.parseInt(listFromArray))  if you get a number since it's in the ArrayList. Hope I didn't make it more confusing.
0 Marco Falanga · September 29, 2015
oh thank you Bob. i tell you my problems:

  1. thank to you i can see on the display all the "text" like "2+4+3...". but im trying to do that with buttons without use the keyboard. 

  2. and if i click on "=" the text i wrote has to go "up" (/n) and under that the result with which you can carry on with calculation.


do you think is possible?

thank you so much :)
  • 1

Java / Android Development

107,003 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator