We all know how calculator works. I am trying to make a calculator in android studio. The thing is I don't want the user to use their keyboard to give number inputs, instead the user is gonna use the buttons on the app. The picture shown below-
Now the question is how to do this button thing? When user clicks button 1 the edittext should show 1, if pressed 2 then show 2 after the 1 in edittext. I am not having any idea at all. I just want an equation in the edittext like 456+54-56/5 like this. I can probably do the math coding but I can't have the numbers. And I don't know how to take input in the edittext other than the keyboard which is not really a calculator thing.
I couldn't try anything. I kinda did this-
Button C_button = findViewById(R.id.c);
C_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((EditText)findViewById(R.id.et_ans)).setText("0");
}
});
But I know why it doesn't work, it just simply put the string zero all together in the edittext every time I click the zero button. But not working like the keyboard entry. Can somebody help me with this?
You can fetch the string that is already present in the EditText and then concatenate the new number present.
Ex: -
EditText edt = ((EditText)findViewById(R.id.et_ans));
Button one_button = findViewById(R.id.one);
one_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String a=edt.getText().toString();
edt.setText(a+ "1");
}
});