How to perform basic calculations on EditText fields containing numerical values in Android Studio
up vote
0
down vote
favorite
I'm new to Android Studio and programming in general, but wanted to create a simple application that performs calculations based on the numbers inputted. The idea is to have 4 EditText
fields that support decimal numerals where only 3 out of 4 fields require input for the calculation to work. After inputting 3 out of the 4 fields, my application will detect which of the 4 fields have an empty value and proceed with the other 3 inputs to output the correct number. When the "Calculate" Button
is pressed, I want to have a TextView
field display the final number.
Performing a simple calculation using just one EditText
, TextView
, and a Button
is working, but can't seem to get the null field detection working when I add more code.
Appreciate any input you can give!
Edit:
To clarify, my simple calculation will be:
editText * editText2 = editText3 * editText4
Essentially, only 3 out of 4 values need to be supplied by the user and the null value will be calculated.
For example:
editText = 2
editText2 = 4
editText3 = null
editText4 = 1
The app will determine that editText3
was not supplied, and therefore, attempt to calculate its value using the above formula. In this example, the value of editText3
will be computed to 8 and displayed in the textView
field after the user hits the Calculate button
.
The code I'm using after Vero Gorena's suggestion is (although I'm still having problems compiling!):
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView);
editText1=findViewById(R.id.editText);
editText2=findViewById(R.id.editText2);
editText3=findViewById(R.id.editText3);
editText4=findViewById(R.id.editText4);
}
// Detects whether a field is null
public String getTheNumber(View view){
if(editText1.getText().length()>0){Toast.makeText(this,"1 Has data",Toast.LENGTH_LONG).show();}
if(editText2.getText().length()>0){Toast.makeText(this,"2 Has data",Toast.LENGTH_LONG).show();}
if(editText3.getText().length()>0){Toast.makeText(this,"3 Has data",Toast.LENGTH_LONG).show();}
if(editText4.getText().length()>0){Toast.makeText(this,"4 Has data",Toast.LENGTH_LONG).show();}
return null;
}
// Calculates and displays the result
private void calculateResult() throws NumberFormatException {
// Calculates the result
int result = editText1 * editText2 = editText3 * editText4;
// Displays the result
textView.setText(result.toString());
// Displays result to 2 decimal places
textView.setText(String.format("%1.2f", result));
}
}
Edit 2:
Just realized that my formula needs to be broken up into 4 cases:
If editText = null, then result = editText3 * editText4 / editText2
If editText2 = null, then result = editText3 * editText4 / editText
If editText3 = null, then result = editText * editText2 / editText4
If editText4 = null, then result = editText * editText2 / editText3
android android-studio
add a comment |
up vote
0
down vote
favorite
I'm new to Android Studio and programming in general, but wanted to create a simple application that performs calculations based on the numbers inputted. The idea is to have 4 EditText
fields that support decimal numerals where only 3 out of 4 fields require input for the calculation to work. After inputting 3 out of the 4 fields, my application will detect which of the 4 fields have an empty value and proceed with the other 3 inputs to output the correct number. When the "Calculate" Button
is pressed, I want to have a TextView
field display the final number.
Performing a simple calculation using just one EditText
, TextView
, and a Button
is working, but can't seem to get the null field detection working when I add more code.
Appreciate any input you can give!
Edit:
To clarify, my simple calculation will be:
editText * editText2 = editText3 * editText4
Essentially, only 3 out of 4 values need to be supplied by the user and the null value will be calculated.
For example:
editText = 2
editText2 = 4
editText3 = null
editText4 = 1
The app will determine that editText3
was not supplied, and therefore, attempt to calculate its value using the above formula. In this example, the value of editText3
will be computed to 8 and displayed in the textView
field after the user hits the Calculate button
.
The code I'm using after Vero Gorena's suggestion is (although I'm still having problems compiling!):
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView);
editText1=findViewById(R.id.editText);
editText2=findViewById(R.id.editText2);
editText3=findViewById(R.id.editText3);
editText4=findViewById(R.id.editText4);
}
// Detects whether a field is null
public String getTheNumber(View view){
if(editText1.getText().length()>0){Toast.makeText(this,"1 Has data",Toast.LENGTH_LONG).show();}
if(editText2.getText().length()>0){Toast.makeText(this,"2 Has data",Toast.LENGTH_LONG).show();}
if(editText3.getText().length()>0){Toast.makeText(this,"3 Has data",Toast.LENGTH_LONG).show();}
if(editText4.getText().length()>0){Toast.makeText(this,"4 Has data",Toast.LENGTH_LONG).show();}
return null;
}
// Calculates and displays the result
private void calculateResult() throws NumberFormatException {
// Calculates the result
int result = editText1 * editText2 = editText3 * editText4;
// Displays the result
textView.setText(result.toString());
// Displays result to 2 decimal places
textView.setText(String.format("%1.2f", result));
}
}
Edit 2:
Just realized that my formula needs to be broken up into 4 cases:
If editText = null, then result = editText3 * editText4 / editText2
If editText2 = null, then result = editText3 * editText4 / editText
If editText3 = null, then result = editText * editText2 / editText4
If editText4 = null, then result = editText * editText2 / editText3
android android-studio
3
"can't seem to get the null field detection working " - sorry but I'm not sure I understand the problem. Could you share some code snippet to illustrate the situation?
– 0X0nosugar
Nov 19 at 20:23
1
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 at 20:29
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm new to Android Studio and programming in general, but wanted to create a simple application that performs calculations based on the numbers inputted. The idea is to have 4 EditText
fields that support decimal numerals where only 3 out of 4 fields require input for the calculation to work. After inputting 3 out of the 4 fields, my application will detect which of the 4 fields have an empty value and proceed with the other 3 inputs to output the correct number. When the "Calculate" Button
is pressed, I want to have a TextView
field display the final number.
Performing a simple calculation using just one EditText
, TextView
, and a Button
is working, but can't seem to get the null field detection working when I add more code.
Appreciate any input you can give!
Edit:
To clarify, my simple calculation will be:
editText * editText2 = editText3 * editText4
Essentially, only 3 out of 4 values need to be supplied by the user and the null value will be calculated.
For example:
editText = 2
editText2 = 4
editText3 = null
editText4 = 1
The app will determine that editText3
was not supplied, and therefore, attempt to calculate its value using the above formula. In this example, the value of editText3
will be computed to 8 and displayed in the textView
field after the user hits the Calculate button
.
The code I'm using after Vero Gorena's suggestion is (although I'm still having problems compiling!):
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView);
editText1=findViewById(R.id.editText);
editText2=findViewById(R.id.editText2);
editText3=findViewById(R.id.editText3);
editText4=findViewById(R.id.editText4);
}
// Detects whether a field is null
public String getTheNumber(View view){
if(editText1.getText().length()>0){Toast.makeText(this,"1 Has data",Toast.LENGTH_LONG).show();}
if(editText2.getText().length()>0){Toast.makeText(this,"2 Has data",Toast.LENGTH_LONG).show();}
if(editText3.getText().length()>0){Toast.makeText(this,"3 Has data",Toast.LENGTH_LONG).show();}
if(editText4.getText().length()>0){Toast.makeText(this,"4 Has data",Toast.LENGTH_LONG).show();}
return null;
}
// Calculates and displays the result
private void calculateResult() throws NumberFormatException {
// Calculates the result
int result = editText1 * editText2 = editText3 * editText4;
// Displays the result
textView.setText(result.toString());
// Displays result to 2 decimal places
textView.setText(String.format("%1.2f", result));
}
}
Edit 2:
Just realized that my formula needs to be broken up into 4 cases:
If editText = null, then result = editText3 * editText4 / editText2
If editText2 = null, then result = editText3 * editText4 / editText
If editText3 = null, then result = editText * editText2 / editText4
If editText4 = null, then result = editText * editText2 / editText3
android android-studio
I'm new to Android Studio and programming in general, but wanted to create a simple application that performs calculations based on the numbers inputted. The idea is to have 4 EditText
fields that support decimal numerals where only 3 out of 4 fields require input for the calculation to work. After inputting 3 out of the 4 fields, my application will detect which of the 4 fields have an empty value and proceed with the other 3 inputs to output the correct number. When the "Calculate" Button
is pressed, I want to have a TextView
field display the final number.
Performing a simple calculation using just one EditText
, TextView
, and a Button
is working, but can't seem to get the null field detection working when I add more code.
Appreciate any input you can give!
Edit:
To clarify, my simple calculation will be:
editText * editText2 = editText3 * editText4
Essentially, only 3 out of 4 values need to be supplied by the user and the null value will be calculated.
For example:
editText = 2
editText2 = 4
editText3 = null
editText4 = 1
The app will determine that editText3
was not supplied, and therefore, attempt to calculate its value using the above formula. In this example, the value of editText3
will be computed to 8 and displayed in the textView
field after the user hits the Calculate button
.
The code I'm using after Vero Gorena's suggestion is (although I'm still having problems compiling!):
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView);
editText1=findViewById(R.id.editText);
editText2=findViewById(R.id.editText2);
editText3=findViewById(R.id.editText3);
editText4=findViewById(R.id.editText4);
}
// Detects whether a field is null
public String getTheNumber(View view){
if(editText1.getText().length()>0){Toast.makeText(this,"1 Has data",Toast.LENGTH_LONG).show();}
if(editText2.getText().length()>0){Toast.makeText(this,"2 Has data",Toast.LENGTH_LONG).show();}
if(editText3.getText().length()>0){Toast.makeText(this,"3 Has data",Toast.LENGTH_LONG).show();}
if(editText4.getText().length()>0){Toast.makeText(this,"4 Has data",Toast.LENGTH_LONG).show();}
return null;
}
// Calculates and displays the result
private void calculateResult() throws NumberFormatException {
// Calculates the result
int result = editText1 * editText2 = editText3 * editText4;
// Displays the result
textView.setText(result.toString());
// Displays result to 2 decimal places
textView.setText(String.format("%1.2f", result));
}
}
Edit 2:
Just realized that my formula needs to be broken up into 4 cases:
If editText = null, then result = editText3 * editText4 / editText2
If editText2 = null, then result = editText3 * editText4 / editText
If editText3 = null, then result = editText * editText2 / editText4
If editText4 = null, then result = editText * editText2 / editText3
android android-studio
android android-studio
edited Nov 20 at 13:06
asked Nov 19 at 20:17
mcgriff
11
11
3
"can't seem to get the null field detection working " - sorry but I'm not sure I understand the problem. Could you share some code snippet to illustrate the situation?
– 0X0nosugar
Nov 19 at 20:23
1
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 at 20:29
add a comment |
3
"can't seem to get the null field detection working " - sorry but I'm not sure I understand the problem. Could you share some code snippet to illustrate the situation?
– 0X0nosugar
Nov 19 at 20:23
1
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 at 20:29
3
3
"can't seem to get the null field detection working " - sorry but I'm not sure I understand the problem. Could you share some code snippet to illustrate the situation?
– 0X0nosugar
Nov 19 at 20:23
"can't seem to get the null field detection working " - sorry but I'm not sure I understand the problem. Could you share some code snippet to illustrate the situation?
– 0X0nosugar
Nov 19 at 20:23
1
1
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 at 20:29
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 at 20:29
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
This code can help you, since it detects which EditText
has or not a number and Instead of use Toast.makeText
, there you can use your own code.
The function getTheNumber already consider to return a data, in this case a String
.
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView_atXML);
editText1=findViewById(R.id.editText1_atXML);
editText2=findViewById(R.id.editText2_atXML);
editText3=findViewById(R.id.editText3_atXML);
editText4=findViewById(R.id.editText4_atXML);
}
public String getTheNumber(View view){
if(editText1.getText().length()>0){Toast.makeText(this,"1 Has data",Toast.LENGTH_LONG).show();}
if(editText2.getText().length()>0){Toast.makeText(this,"2 Has data",Toast.LENGTH_LONG).show();}
if(editText3.getText().length()>0){Toast.makeText(this,"3 Has data",Toast.LENGTH_LONG).show();}
if(editText4.getText().length()>0){Toast.makeText(this,"4 Has data",Toast.LENGTH_LONG).show();}
return null;
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
This code can help you, since it detects which EditText
has or not a number and Instead of use Toast.makeText
, there you can use your own code.
The function getTheNumber already consider to return a data, in this case a String
.
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView_atXML);
editText1=findViewById(R.id.editText1_atXML);
editText2=findViewById(R.id.editText2_atXML);
editText3=findViewById(R.id.editText3_atXML);
editText4=findViewById(R.id.editText4_atXML);
}
public String getTheNumber(View view){
if(editText1.getText().length()>0){Toast.makeText(this,"1 Has data",Toast.LENGTH_LONG).show();}
if(editText2.getText().length()>0){Toast.makeText(this,"2 Has data",Toast.LENGTH_LONG).show();}
if(editText3.getText().length()>0){Toast.makeText(this,"3 Has data",Toast.LENGTH_LONG).show();}
if(editText4.getText().length()>0){Toast.makeText(this,"4 Has data",Toast.LENGTH_LONG).show();}
return null;
}
}
add a comment |
up vote
0
down vote
This code can help you, since it detects which EditText
has or not a number and Instead of use Toast.makeText
, there you can use your own code.
The function getTheNumber already consider to return a data, in this case a String
.
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView_atXML);
editText1=findViewById(R.id.editText1_atXML);
editText2=findViewById(R.id.editText2_atXML);
editText3=findViewById(R.id.editText3_atXML);
editText4=findViewById(R.id.editText4_atXML);
}
public String getTheNumber(View view){
if(editText1.getText().length()>0){Toast.makeText(this,"1 Has data",Toast.LENGTH_LONG).show();}
if(editText2.getText().length()>0){Toast.makeText(this,"2 Has data",Toast.LENGTH_LONG).show();}
if(editText3.getText().length()>0){Toast.makeText(this,"3 Has data",Toast.LENGTH_LONG).show();}
if(editText4.getText().length()>0){Toast.makeText(this,"4 Has data",Toast.LENGTH_LONG).show();}
return null;
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
This code can help you, since it detects which EditText
has or not a number and Instead of use Toast.makeText
, there you can use your own code.
The function getTheNumber already consider to return a data, in this case a String
.
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView_atXML);
editText1=findViewById(R.id.editText1_atXML);
editText2=findViewById(R.id.editText2_atXML);
editText3=findViewById(R.id.editText3_atXML);
editText4=findViewById(R.id.editText4_atXML);
}
public String getTheNumber(View view){
if(editText1.getText().length()>0){Toast.makeText(this,"1 Has data",Toast.LENGTH_LONG).show();}
if(editText2.getText().length()>0){Toast.makeText(this,"2 Has data",Toast.LENGTH_LONG).show();}
if(editText3.getText().length()>0){Toast.makeText(this,"3 Has data",Toast.LENGTH_LONG).show();}
if(editText4.getText().length()>0){Toast.makeText(this,"4 Has data",Toast.LENGTH_LONG).show();}
return null;
}
}
This code can help you, since it detects which EditText
has or not a number and Instead of use Toast.makeText
, there you can use your own code.
The function getTheNumber already consider to return a data, in this case a String
.
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView_atXML);
editText1=findViewById(R.id.editText1_atXML);
editText2=findViewById(R.id.editText2_atXML);
editText3=findViewById(R.id.editText3_atXML);
editText4=findViewById(R.id.editText4_atXML);
}
public String getTheNumber(View view){
if(editText1.getText().length()>0){Toast.makeText(this,"1 Has data",Toast.LENGTH_LONG).show();}
if(editText2.getText().length()>0){Toast.makeText(this,"2 Has data",Toast.LENGTH_LONG).show();}
if(editText3.getText().length()>0){Toast.makeText(this,"3 Has data",Toast.LENGTH_LONG).show();}
if(editText4.getText().length()>0){Toast.makeText(this,"4 Has data",Toast.LENGTH_LONG).show();}
return null;
}
}
answered Nov 19 at 21:28
Vero Gorena
213
213
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382025%2fhow-to-perform-basic-calculations-on-edittext-fields-containing-numerical-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
"can't seem to get the null field detection working " - sorry but I'm not sure I understand the problem. Could you share some code snippet to illustrate the situation?
– 0X0nosugar
Nov 19 at 20:23
1
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 at 20:29