How to perform basic calculations on EditText fields containing numerical values in 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
add a comment |
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 '18 at 20:23
1
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 '18 at 20:29
add a comment |
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 '18 at 13:06
mcgriff
asked Nov 19 '18 at 20:17
mcgriffmcgriff
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 '18 at 20:23
1
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 '18 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 '18 at 20:23
1
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 '18 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 '18 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 '18 at 20:23
1
1
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 '18 at 20:29
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 '18 at 20:29
add a comment |
1 Answer
1
active
oldest
votes
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 21:28
Vero GorenaVero Gorena
214
214
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.
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 '18 at 20:23
1
Edittext.getText ().toString ().equals (""); means there is nothing in the edittext
– JRowan
Nov 19 '18 at 20:29