I am trying to make a simple tip calculator and have an EditText text field where you put in the amount of your total bill, then click on the percent button you want to calculate in tip (say, 10%) and then the onClickListener for that % button will calculate the tip and put the result into a different TextView field below itself.
However, I know I had to change my EditText for my total bill from a string to a double, and included code for that, but I still get the error when I try to multiply the total bill times the tip of 10%. Why?
Here is my code:
package com.example.nonitips;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
public EditText totalBill;
public double billWithTip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
totalBill = (EditText)findViewById(R.id.totalBill);
}
public void onTen (View v) {
Toast.makeText(this, "Tipping at 10%", Toast.LENGTH_SHORT).show();
totalBill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double bill = Double.parseDouble(totalBill.getText().toString());
bill = (totalBill * 0.10) + totalBill; //Error on this line.
}
});
}
public void onFifteen (View v) {
Toast.makeText(this, "Tipping at 15%", Toast.LENGTH_SHORT).show();
}
public void onTwenty (View v) {
Toast.makeText(this, "Tipping at 20%", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Change your code of EditText click:
totalBill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double bill = Double.parseDouble(totalBill.getText().toString());
billWithTip= (bill * 0.10) + billWithTip;
Toast.makeText(this, "Updated billWithTip"+billWithTip, Toast.LENGTH_SHORT).show();
billWithTip.setText(String.valueOf(billWithTip));
}
});
Double to String convertion Example:
double total = 44;
String total2 = String.valueOf(total);
You are trying to multiple EditText with double value. Use following code.
totalBill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double bill = Double.parseDouble(totalBill.getText().toString());
billWithTip= (bill * 0.10) + billWithTip;
}
});
try this one..
totalBill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// double bill = Double.parseDouble(totalBill.getText().toString());
// bill = (totalBill * 0.10) + totalBill; //Error on this line.
try {
String bills= totalBill.getText().toString();
Double protein = Double.parseDouble(bills); // Make use of autoboxing.It's also easier to read.
} catch (NumberFormatException e) {
// p did not contain a valid double
}
if(protein == null || protein.isEmpty()) {
bill = 0.0;
} else {
bills= Double.parseDouble(noKids.getText().toString());
Double protein = Double.parseDouble(bills);
bill = (protein * 0.10) + protein ;
}
}
});
thank you..
try this
totalBill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double bill = Double.parseDouble(totalBill.getText().toString());
bill = (bill * 0.10) + bill );
}
});
Related
I'm a beginner in Android Studio, and I want to make a C button in calc with 2 functions.
How do I do that on one tap the C button erases only one number, and on hold erase all numbers in TextView?
findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtScreen.setText(""); // Clear the screen
// Reset all the states and flags
lastNumeric = false;
stateError = false;
lastDot = false;
You can set an onClickListener and onLongClickListener to achieve this.
cButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String currentText = textView.getText().toString();
if(currentText.length >= 2){
currentText = currentText.substring(0, currentText.length - 2);
}else{
currentText = "";
}
textView.setText(currentText);
}
});
cButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
textView.setText("");
return true;
}
});
Have you looked in to the onClick() and onLongPress() methods?
cancelButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
myTextView.setText("");
return true;
}
});
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
removeOneCharacter();
}
});
private void removeOneCharacter()
{
String textViewValue = myTextView.getText().toString();
if (textViewValue != null && textViewValue.length >= 2)
{
myTextView.setText(textViewValue.substring(0, textViewValue.length() - 2);
}
}
The onClick() method removes on char from the TextView at a time. The onClick() method however removes the entire String from the TextView but only considering if there is already 2 or more characters to prevent an Exception from occurring due to the upper-bound of the substring.
An improvement here could be to add another if function that checks the character length within the Long press and performs a clear if there is only one character remaining.
i have 2 edit Text in my application, 1 button to add the input numbers in the edit Text and 1 text view to display the result. I would like to put a toast message if my edit text tab is empty or null when i click the button. I have searched and tried all the solutions..and nothing seems to work. help please!!
here is my code for adding the two numbers.
MainActivity.java
package com.example.mycalc;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText)findViewById(R.id.editText1);
final EditText editText2 = (EditText)findViewById(R.id.editText2);
Button button1 = (Button)findViewById(R.id.button1);
final TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText(" ");
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
double edit1 = Double.valueOf(editText1.getText().toString());
double edit2 = Double.valueOf(editText2.getText().toString());
double text = edit1 + edit2;
textView1.setText(String.valueOf(text));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
when user clicks button check:
if (editText1.getText().toString().trim().length() <= 0) {
Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
}
trim it to avoid blank spaces.
To create and show a Toast use this code:
Toast.makeText(this, "Please input number", Toast.LENGTH_LONG).show();
In order to work properly, you should replace this code:
if (editText1.equals("")) {
editText1.setError("please input number");
}
if (editText2.equals("")) {
editText2.setError("please input number");
}
with this:
if (editText1.getText().toString().length() == 0 || editText1.getText().toString().length() == 1) {
Toast.makeText(this, "Please input number", Toast.LENGTH_LONG).show();
}
You can try this two function for string empty, not empty or null. It is simple return true or false. It is very use for all projects.
if(isEmpty(edittext.getText().toString())){
// your Toast message if string is empty
}
if(isNotEmpty(edittext.getText().toString())){
// your Toast message if string is not empty
}
public static boolean isEmpty(String str) {
if (str == null)
return true;
else if (str.toString().trim().length() == 0)
return true;
return false;
}
public static boolean isNotEmpty(String str) {
if (str == null)
return false;
else if (str.toString().trim().length() == 0)
return false;
return true;
}
Add to your click listener check
Toast toast = Toast.makeText(MainActivity.this, "Your Message", Toast.LENGTH_SHORT);
toast.show();
Your code will be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText)findViewById(R.id.editText1);
final EditText editText2 = (EditText)findViewById(R.id.editText2);
Button button1 = (Button)findViewById(R.id.button1);
final TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText(" ");
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(editText1.getText().toString().trim().length() == 0 || editText2.getText().toString().trim().length() == 0) {
Toast.makeText(this,"Edittext is null",Toast.LENGTH_SHORT).show();
}
else {
double edit1 = Double.valueOf(editText1.getText().toString());
double edit2 = Double.valueOf(editText2.getText().toString());
double text = edit1 + edit2;
textView1.setText(String.valueOf(text));
}
});
}
}
I want to calculate a value from the data in 2 text edits and show the result in a textview. How can I do it?
BMI calculator:
private TextView deinbmi;
private EditText weight;
private EditText height;
public void calculator ()
{
double ergebnis;
weight=(EditText)findViewById(R.id.text_calculator_weight);
height=(EditText)findViewById(R.id.text_calculator_height);
double a=Double.parseDouble(weight.getText().toString());
double b=Double.parseDouble(height.getText().toString());
ergebnis=a*b;
deinbmi.setText(""+ergebnis);
}
I don't have an idea; I just started with Java. I can program with C++ but just console.
If I search on the internet for this problem, everyone has another version of solving this, but none of these answers worked for me?
Try This Working code.
you Missing the Textview Inside oncreate, Then Add a Button for Getting the Answer
public class Mainactivity extends Activity implements OnClickListener
{
private TextView deinbmi;
private EditText weight;
private EditText height;
Button Answer;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.page1_landing);
weight=(EditText)findViewById(R.id.text_calculator_weight);
height=(EditText)findViewById(R.id.text_calculator_height);
deinbmi=(Textview)findViewById(R.id.text);
Answer=(Button)findViewById(R.id.answerbuttton);
Answer.SetOnclicklistener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.answerbuttton:
calculator ();
break;
}}
public void calculator ()
{
double ergebnis;
double a=Double.parseDouble(weight.getText().toString());
double b=Double.parseDouble(height.getText().toString());
ergebnis=a*b;
deinbmi.setText(""+ergebnis);
}
}
public class calculator extends ActionBarActivity {
private TextView deinbmi;
private EditText weight;
private EditText height;
public void calculator ()
{
double ergebnis;
weight=(EditText)findViewById(R.id.text_calculator_weight);
height=(EditText)findViewById(R.id.text_calculator_height);
double a=Double.parseDouble(weight.getText().toString());
double b=Double.parseDouble(height.getText().toString());
ergebnis=a*b;
deinbmi.setText(""+ergebnis);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_calculator, menu);
//rechnen ?!
Button credits_calculate = (Button)
findViewById(R.id.button_calculate);
credits_calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calculator();
}
});
//back
Button credits_back = (Button)
findViewById(R.id.button_calculator_back);
credits_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
is my activity
i tryed to use your code but
Answer=(Button)findViewById(R.id.answerbuttton);
Answer.SetOnclicklistener(this);
he didnt understand this
Here is a code that i want to make a bmi application..
So i want to make the result display like this 20.30..
How to make the result into 2 decimals places only..
Hope anybody cant help me at this part....
package com.example.bmi;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
EditText displayweight;
EditText displayheight;
EditText displayresult;
Button Calculate;
Button Reset;
double bmi=0;
double valueheight=0;
double valueweight=0;
String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
/*TextView tv = new TextView(this);
tv.setText("BMI Calculator");
setContentView(tv);*/
}
private void initControls() {
displayheight = (EditText)findViewById(R.id.displayheight);
displayweight = (EditText)findViewById(R.id.displayweight);
displayresult = (EditText)findViewById(R.id.displayresult);
Calculate = (Button)findViewById(R.id.calculate);
Reset = (Button)findViewById(R.id.reset);
Calculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }});
Reset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }
private void reset() {
displayheight.setText("");
displayweight.setText("");
displayresult.setText("");
}});
}
private void calculate() {
valueheight = Double.parseDouble(displayheight.getText().toString());
valueweight = Double.parseDouble(displayweight.getText().toString());
Double valueheightmeters;
valueheightmeters = valueheight / 100;
bmi = (valueweight / (valueheightmeters * valueheightmeters));
//txttipamount.setText(Double.toString(bmi));
if(bmi >= 30)
{
result = "Your BMI of " + Double.toString(bmi) + " is OBESE.";
displayresult.setText(result);
}
else if (bmi >= 25)
{
result = "Your BMI of " + Double.toString(bmi) + " is OVERWEIGHT.";
displayresult.setText(result);
}
else if (bmi >= 18.5)
{
result = "Your BMI of " + Double.toString(bmi) + " is IDEAL.";
displayresult.setText(result);
}
else
{
result = "Your BMI of " + Double.toString(bmi) + " is UNDERWEIGHT.";
displayresult.setText(result);
}
}
#Override
public void onClick(View arg0) {
switch (arg0.getId()){
case R.id.reset:
displayresult.setText("");
break;
default:
break;
}
}
}
this topic is discussed very often. Take a look at Round a double to 2 decimal places and see if it helps you.
Just let me know if you have any further questions.
how do i hide the context of the text field and show it when i click
the button
i do not want to hide the whole text filed just the context i wrote inside it
and show ot when i click a button
*
this is a small code*
package com.example.nonachan;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
char a;
char b;
char c;
int i = 0;
char buf;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText t =(EditText)findViewById(R.id.t1);
ImageButton n = (ImageButton)findViewById(R.id.b1);
n.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
a = 'a';
t.setText(t.getText().toString() + a);
// t.setVisibility(View.INVISIBLE);
}
});
ImageButton a = (ImageButton)findViewById(R.id.b2);
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
b = 'b' ;
t.setText(t.getText().toString() + b);
// t.setVisibility(View.INVISIBLE);
i++;
}
});
ImageButton m = (ImageButton)findViewById(R.id.b4);
m.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
c = 'c' ;
t.setText(t.getText().toString() + c);
//t.setVisibility(View.INVISIBLE);
i++;
}
});
Button l = (Button)findViewById(R.id.b3);
l.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// t.setVisibility(View.VISIBLE);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Assuming you have misspelled contents as context in your question (looks very likely)
instead of trying to hide the contents of the EditText, just save it to a variable and set the text of the EditText to empty. Then in your button click just set the text back to the contents in your local variable
Eg
String hiddenText = null;
EditText text = (EditText)findViewById(R.id.t1);
ImageButton hide = (ImageButton)findViewById(R.id.b1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// save and hide
hiddenText = text.getText();
text.setText("");
}
});
ImageButton unhide = (ImageButton)findViewById(R.id.b2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// unhide the text and 'clear' hiddenText
if (hiddenText != null) {
text.setText(hiddenText);
hiddenText = null;
}
}
});
There are several ways of doing it. It depends how you want to do it. These are just some suggestions off the top of my head, you can even use them all together.
You can put any entered text in a separate string and then if invisible set the TextView to show an empty string, if visible set the TextView to the saved string.
You can use the visibility tag.
You can set the text color to the background color.
You can create/remove the TextView on visible/invisible states.
You can replace the text with symbols for secure inputs if you want (for each character in edittext display an asterisk or something) and then change that back to plain text.
There are a hundred and one ways of doing it, if you search around on previously answered questions you will see several different methods.