Simple calculator program not showing proper output - java

I have some simple code where I input two numbers in EditText fields, add them together, and show them in a TextView.
Here is my code:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:visibility="invisible"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="21dp"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignRight="#+id/editText2"
android:text="Clear"
android:onClick="Clicked" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText2"
android:layout_marginTop="35dp"
android:text="Total"
android:onClick="Clicked" />
And here is my Clicked method:
public void Clicked(View v) {
int total;
EditText t1= (EditText)findViewById(R.id.editText1);
EditText t2= (EditText)findViewById(R.id.editText2);
TextView tv= (TextView)findViewById(R.id.textView1);
if(v.getId()==R.id.button1)
{
total= Integer.parseInt(t1.getText().toString()+t2.getText().toString());
tv.setText(total);
tv.setVisibility(1);
}
else if (v.getId()==R.id.button2)
{
t1.setText("");
t2.setText("");
}
}
I input two numbers and when I press clear it works fine but the total button does not add. Instead, this happens:
It might be a problem with my integer casting or a problem with logic.

Change this:
total= Integer.parseInt(t1.getText().toString()+t2.getText().toString());
To this:
int v1 = Integer.parseInt(t1.getText().toString());
int v2 = Integer.parseInt(t2.getText().toString());
total = v1 + v2;

Its stopped because you may be setting a Int value to setText of TextView. You need to convert it to String
textView.setText(CharSequence text)
int v1 = Integer.parseInt(t1.getText().toString());
int v2 = Integer.parseInt(t2.getText().toString());
total = v1 + v2;
tv.setText(total+""); //this converts total to string
//tv.setText(total.toString()); //can use either

Related

Text not appearing but exists, Trying to make a simple app that calculates average

Been trying to make a simple app that can calculate the average of 8 numbers(with decimals). I already made the layout and made an initial project that adds two numbers which when i tested is working fine but when i decided to continue on my original goal, make an app that can calculate the average of 8 numbers(with decimal), a problem occured. When i tested it on my phone, i typed some numbers on the number field, it moves indicating that something is being typed but what i typed didn't appear. At first my hunch is that the font color is white which is not, but it still doesn't show. Please help.
my layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/sub"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub1" />
<TextView
android:id="#+id/sub1"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/sub2"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub" />
<TextView
android:id="#+id/sub4"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub2" />
<TextView
android:id="#+id/sub5"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub4" />
<TextView
android:id="#+id/sub6"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub5" />
<TextView
android:id="#+id/sub7"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub6" />
<TextView
android:id="#+id/sub8"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub7" />
<EditText
android:id="#+id/num"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:editable="true"
android:ems="10"
android:inputType="numberDecimal"
android:textColor="#00FF0000"
app:layout_constraintStart_toEndOf="#+id/sub1"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/num1"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub"
app:layout_constraintTop_toBottomOf="#+id/num" />
<EditText
android:id="#+id/num2"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub2"
app:layout_constraintTop_toBottomOf="#+id/num1" />
<EditText
android:id="#+id/num3"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub4"
app:layout_constraintTop_toBottomOf="#+id/num2" />
<EditText
android:id="#+id/num4"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub5"
app:layout_constraintTop_toBottomOf="#+id/num3" />
<EditText
android:id="#+id/num5"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub6"
app:layout_constraintTop_toBottomOf="#+id/num4" />
<EditText
android:id="#+id/num6"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub7"
app:layout_constraintTop_toBottomOf="#+id/num5" />
<EditText
android:id="#+id/num7"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub8"
app:layout_constraintTop_toBottomOf="#+id/num6" />
<Button
android:id="#+id/btnAvr"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="12dp"
android:text="Get Average"
android:textSize="8sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvAnswer" />
<TextView
android:id="#+id/tvAnswer"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="190dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="30dp"
android:text="Average"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/num7" />
My Java Code
package com.example.cardmkii;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView sub;
private TextView sub1;
private TextView sub2;
private TextView sub4;
private TextView sub5;
private TextView sub6;
private TextView sub7;
private TextView sub8;
private EditText numb;
private EditText numb1;
private EditText numb2;
private EditText numb3;
private EditText numb4;
private EditText numb5;
private EditText numb6;
private EditText numb7;
private Button average;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sub = (TextView) findViewById(R.id.sub);
sub1 = (TextView) findViewById(R.id.sub1);
sub2 = (TextView) findViewById(R.id.sub2);
sub4 = (TextView) findViewById(R.id.sub4);
sub5 = (TextView) findViewById(R.id.sub5);
sub6 = (TextView) findViewById(R.id.sub6);
sub7 = (TextView) findViewById(R.id.sub7);
sub8 = (TextView) findViewById(R.id.sub8);
numb = (EditText) findViewById(R.id.num);
numb1 = (EditText) findViewById(R.id.num1);
numb2 = (EditText) findViewById(R.id.num2);
numb3 = (EditText) findViewById(R.id.num3);
numb4 = (EditText) findViewById(R.id.num4);
numb5 = (EditText) findViewById(R.id.num5);
numb6 = (EditText) findViewById(R.id.num6);
numb7 = (EditText) findViewById(R.id.num7);
average = (Button) findViewById(R.id.btnAvr);
result = (TextView) findViewById(R.id.tvAnswer);
average.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double number = Double.parseDouble(numb.getText().toString());
double number1 = Double.parseDouble(numb1.getText().toString());
double number2 = Double.parseDouble(numb2.getText().toString());
double number3 = Double.parseDouble(numb3.getText().toString());
double number4 = Double.parseDouble(numb4.getText().toString());
double number5 = Double.parseDouble(numb5.getText().toString());
double number6 = Double.parseDouble(numb6.getText().toString());
double number7 = Double.parseDouble(numb7.getText().toString());
double average = (number + number1 + number2 + number3 + number4 + number5 + number6 + number7)/8;
result.setText("Answer: " + String.valueOf(average));
}
});
}
}
Set height in every edittext to wrap content
eg.
android:layout_height="wrap_content"
and set textcolor dark currently i run your layout in white background it not see any text.
so use visible color for text

How to write if else to get value from edit text android

I have this 3 types of edit text input.
I want to compare the 3 edit text to get the FreeTime.
xml codes
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/morningLabel"
android:id="#+id/MorningLabel"
android:textStyle="bold"
android:layout_alignTop="#+id/textView2"
android:layout_alignRight="#+id/MorningText"
android:layout_alignEnd="#+id/MorningText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Working Hour(s)"
android:id="#+id/textView3"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Afternoon"
android:id="#+id/textView2"
android:textStyle="bold"
android:layout_alignTop="#+id/textView5"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Evening"
android:id="#+id/textView5"
android:textStyle="bold"
android:layout_marginBottom="41dp"
android:layout_above="#+id/AfternoonText"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=" 6.01 a.m. \n to \n12.00 p.m."
android:id="#+id/morningTime"
android:textStyle="italic"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/textView3"
android:layout_toStartOf="#+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=" 12.01 p.m. \n to \n 6.00 p.m."
android:id="#+id/afternoonTime"
android:textStyle="italic"
android:layout_alignTop="#+id/morningTime"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=" 6.01 p.m. \n to \n12.00 a.m."
android:id="#+id/eveningTime"
android:textStyle="italic"
android:layout_alignTop="#+id/afternoonTime"
android:layout_alignLeft="#+id/EveningText"
android:layout_alignStart="#+id/EveningText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:id="#+id/btnNext"
android:layout_alignParentBottom="true"
android:layout_marginBottom="38dp"
android:layout_alignRight="#+id/eveningTime"
android:layout_alignEnd="#+id/eveningTime"
android:layout_alignLeft="#+id/morningTime"
android:layout_alignStart="#+id/morningTime"
android:textStyle="bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/MorningText"
android:hint="0-6"
android:layout_marginBottom="22dp"
android:autoText="true"
android:background="#ffd1d1d1"
android:editable="false"
android:textSize="#dimen/abc_text_size_display_1_material"
android:layout_above="#+id/morningTime"
android:layout_toLeftOf="#+id/textView3"
android:layout_alignLeft="#+id/morningTime"
android:layout_alignStart="#+id/morningTime"
android:text="#string/MorningInput"
android:singleLine="false"
android:textAlignment="center"
android:numeric="integer" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/AfternoonText"
android:hint="0-6"
android:autoText="true"
android:background="#ffd1d1d1"
android:editable="false"
android:textSize="#dimen/abc_text_size_display_1_material"
android:layout_alignTop="#+id/MorningText"
android:layout_alignLeft="#+id/afternoonTime"
android:layout_alignStart="#+id/afternoonTime"
android:layout_alignRight="#+id/afternoonTime"
android:layout_alignEnd="#+id/afternoonTime"
android:text="#string/AfternoonInput"
android:textAlignment="center"
android:numeric="integer" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/EveningText"
android:hint="0-6"
android:autoText="true"
android:background="#ffd1d1d1"
android:editable="false"
android:textSize="#dimen/abc_text_size_display_1_material"
android:layout_alignBaseline="#+id/AfternoonText"
android:layout_alignBottom="#+id/AfternoonText"
android:layout_alignRight="#+id/textView5"
android:layout_alignEnd="#+id/textView5"
android:text="#string/EveningInput"
android:textAlignment="center"
android:numeric="integer"
android:layout_alignLeft="#+id/textView5"
android:layout_alignStart="#+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/timeLabel"
android:id="#+id/TimeText"
android:layout_above="#+id/btnNext"
android:layout_alignLeft="#+id/btnNext"
android:layout_alignStart="#+id/btnNext" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="c"
android:onClick="calculateClickHandler"
android:id="#+id/btnc"
android:layout_above="#+id/TimeText"
android:layout_toRightOf="#+id/afternoonTime"
android:layout_toEndOf="#+id/afternoonTime"
android:layout_marginBottom="42dp" />
This is the java code. I think I put the wrong if else. How can I solve this? Anyone help:
public class WorkingHour extends Activity
{
public void calculateClickHandler(View view)
{
if (view.getId() == R.id.btnc)
{
Intent i=new Intent(this, Result.class);
// get the references to the widgets
EditText MorningInput = (EditText)findViewById(R.id.MorningText);
EditText AfternoonInput = (EditText)findViewById(R.id.AfternoonText);
EditText EveningInput = (EditText)findViewById(R.id.EveningText);
TextView TimeText = (TextView)findViewById(R.id.TimeText);
// get the users values from the widget references
int morninghour = Integer.parseInt(MorningInput.getText().toString());
int afternoonhour = Integer.parseInt(AfternoonInput.getText().toString());
int eveninghour = Integer.parseInt(EveningInput.getText().toString());
// calculate the free time
int freeTimeValue = calculateFreeTime(morninghour,afternoonhour,eveninghour);
// interpret the meaning of the bmi value
String freetimeInterpretation = interpretFT(freeTimeValue);
// now set the value in the result text
TimeText.setText(freeTimeValue + "-" + freetimeInterpretation);
SharedPreferences sp2 = getSharedPreferences("name2",MODE_PRIVATE);
SharedPreferences.Editor editor=sp2.edit();
editor.putString("rslt 3",TimeText.getText().toString());
editor.commit();
}
}
// the formula to calculate the free time
private int calculateFreeTime (int morninghour, int afternoonhour, int eveninghour)
{
if (((6- morninghour)> (6-afternoonhour)) && ((6- morninghour)> (6-eveninghour)))
{
return (int) (6 - morninghour);
}
else if (((6- afternoonhour)> (6-morninghour)) && ((6- afternoonhour)> (6-eveninghour)))
{
return (int) (6 - afternoonhour);
}
else if (((6- eveninghour)> (6-morninghour)) && ((6-eveninghour)> (6-afternoonhour)))
{
return (int) (6 - eveninghour);
}
return 0;
}
This code:
if (freetime < )
{
return "morning";
}
Is definitely wrong and should not even compile. You are saying here: if freetime is smaller than nothing... do something. You have to put a value in it (these are for example):
if (freetime < 6)
{
return "morning";
}
else if (freetime < 12)
{
return "Afternoon";
}
Also, there is no need to cast this return value because it are both integers:
return (int) (6 - morninghour);
^^
If you want to make your code a little better (mainly for readability), you can optimize this:
morninghour = 6 - morninghour;
afternoonhour = 6 - afternoonhour;
eveninghour = 6 - eveninghour;
if ((morninghour > afternoonhour) && (morninghour > eveninghour))
{
return morninghour;
}
else if ((afternoonhour > morninghour) && (afternoonhour > eveninghour))
{
return afternoonhour;
}
else if ((eveninghour > morninghour) && (eveninghour > afternoonhour))
{
return eveninghour;
}
return 0;

Summing totals to an edit text output and preventing negatives

I have been stuck, regrettably, in a simple math problem.
I am creating a coffee application, as seen below, that takes the "Quantity" and multiplies it by the price to create a sub-total for each drink drink. It will then take all sub-totals from the drinks and add them together for the output at the bottom which would be updated automatically.
I have worked on the code to add or subtract from the 0 (I still can't figure out a way to prevent it from going below 0, if anyone has an idea, help is appreciated).
I have it so that the first 2 buttons (in pink) work right now for Espresso and Macchiato.
Here is my Java file
package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class DrinkMenu extends Activity {
EditText quantity, quantity2, total;
Button button, plus1, minus1, plus2, minus2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
// area for the espresso
plus1 = (Button) findViewById(R.id.button6);
minus1 = (Button) findViewById(R.id.button7);
quantity = (EditText) findViewById(R.id.editText2);
// area for the macchiato
plus2 = (Button) findViewById(R.id.button8);
minus2 = (Button) findViewById(R.id.button9);
quantity2 = (EditText) findViewById(R.id.editText4);
//subtotal for espresso
//subtotal for macchiato
total = (EditText) findViewById(R.id.editText9);
plus1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1+1;
quantity.setText(Integer.toString(inum1));
}
});//plus1 button
minus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1-1;
quantity.setText(Integer.toString(inum1));
}
});//minus1 button
plus2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2+1;
quantity2.setText(Integer.toString(inum2));
}
});//plus2 button
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
quantity2.setText(Integer.toString(inum2));
}
});//minus2 button
// Here is where I think I should place the sub-total multiplied by the prices
// and will be out put to the total = espresso_sub_total*espress_price +
// macchiato_sub_total*macchiato_price + and so on for the other drinks
}
}
Now, It may be imperative that I set my Total price to a Text View rather than Edit Text also, thoughts?
So after implementing ideas from both users #useruser3249477 and #Shobhit I have gotten the numbers to stop going below 0 and above 10, but then I tried to add the total together in the Total area of '0'. I have updated code below for both java and the xml file. It crashes as I press the '+' button.
updated Java source code:
package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class DrinkMenu extends Activity {
EditText quantity, quantity2;
//TextView total;
Button button, plus1, minus1, plus2, minus2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
// area for the espresso
plus1 = (Button) findViewById(R.id.button6);
minus1 = (Button) findViewById(R.id.button7);
quantity = (EditText) findViewById(R.id.editText2);
// area for the macchiato
plus2 = (Button) findViewById(R.id.button8);
minus2 = (Button) findViewById(R.id.button9);
quantity2 = (EditText) findViewById(R.id.editText4);
//espresso-sub-total
//macchiato-sub-total
//total = (TextView) findViewById(R.id.textView7);
plus1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1+1;
if (inum1 > 10) return;
quantity.setText(Integer.toString(inum1));
}
});//plus1 button
minus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1-1;
if (inum1 < 0) return;
quantity.setText(Integer.toString(inum1));
}
});//minus2 button
plus2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2+1;
if (inum2 > 10) return;
quantity2.setText(Integer.toString(inum2));
}
});//plus1 buttons
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
if (inum2 < 0) return;
quantity2.setText(Integer.toString(inum2));
}
});
// double subtotal = Double.parseDouble(numb1);
// Here is where I think I should place the sub-total multiplied by the prices
// and will be out put to the total = num1*3;
final TextView total = (TextView) findViewById(R.id.textView7);
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Remove previous price of these items
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal - count*3;
total.setText(newTotal);
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Add the new items price
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal + count*3;
total.setText(newTotal);
}
#Override
public void afterTextChanged(Editable editable) {}
};
quantity.addTextChangedListener(textWatcher);
quantity2.addTextChangedListener(textWatcher);
}
//order button code that is useless to this question.
}
Here is my updated XML file that is updated:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button"
android:layout_marginRight="75dp"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2"
android:background="#FFFFFF"
android:text="Drinks:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/orderbtn"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/orderbtn"
android:background="#FFFFFF"
android:text="Quantity:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24dp"
android:textStyle="bold" />
<Button
android:id="#+id/button"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Espresso"
android:textSize="24dp" />
<Button
android:id="#+id/button2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Macchiato"
android:textSize="24dp" />
<Button
android:id="#+id/button3"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button2"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Con Panna"
android:textSize="24dp" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button4"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Latte"
android:textSize="24dp" />
<Button
android:id="#+id/button6"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/button6"
android:layout_toStartOf="#+id/button6"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<Button
android:id="#+id/button7"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/editText2"
android:layout_toStartOf="#+id/editText2"
android:background="#ffff45df"
android:text="-"
android:textSize="24dp" />
<Button
android:id="#+id/button8"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button2"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText3"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_toStartOf="#+id/button8"
android:background="#FFFFFF"
android:ems="10"
android:inputType="number"
android:text=" 0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText4"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button8"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_toStartOf="#+id/button8"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24sp" />
<Button
android:id="#+id/button9"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/editText3"
android:layout_toStartOf="#+id/editText3"
android:background="#ffff45df"
android:text="-"
android:textSize="24dp" />
<Button
android:id="#+id/button10"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/button8"
android:layout_alignRight="#+id/button8"
android:layout_alignTop="#+id/button3"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<Button
android:id="#+id/button11"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button4"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<Button
android:id="#+id/button12"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button5"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText5"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_above="#+id/button4"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/button10"
android:layout_toStartOf="#+id/button10"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText6"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button11"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/button11"
android:layout_toStartOf="#+id/button11"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText7"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button12"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/button12"
android:layout_toStartOf="#+id/button12"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<Button
android:id="#+id/button14"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/editText6"
android:layout_toStartOf="#+id/editText6"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<Button
android:id="#+id/button15"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/editText7"
android:layout_toStartOf="#+id/editText7"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<Button
android:id="#+id/button16"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/editText5"
android:layout_toStartOf="#+id/editText5"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button16"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button14"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="#+id/button4"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button3"
android:layout_marginTop="32dp"
android:background="#FFFFFF"
android:text="Americano"
android:textSize="24dp"
android:textColor="#000000" />
<Button
android:id="#+id/orderbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#drawable/buttonround"
android:text="Order"
android:textSize="24sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/orderbtn"
android:layout_alignBottom="#+id/orderbtn"
android:layout_alignParentLeft="true"
android:background="#FFFFFF"
android:text="Total: $"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView8"
android:layout_alignBottom="#+id/textView8"
android:layout_alignLeft="#+id/textView"
android:layout_alignRight="#+id/textView6"
android:layout_alignStart="#+id/textView"
android:background="#FFFFFF"
android:text="0"
android:textSize="24sp" />
</RelativeLayout>
Here are some of the errors coming out.
E/AndroidRuntime(369): FATAL EXCEPTION: main
E/AndroidRuntime(369): android.content.res.Resources$NotFoundException: String resource ID #0x0// seems to be here
E/AndroidRuntime(369): at android.content.res.Resources.getText(Resources.java:201)
A simple check will prevent negative values (using the buttons):
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
if (inum2 < 0) return;
quantity2.setText(Integer.toString(inum2));
}
});
To prevent manually entering negative values, you can set android:digits="0123456789" as #ShobhitPuri suggested.
Then for the total you'll need to set TextWatchers:
final EditText total = (EditText) findViewById(R.id.editText9);
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Remove previous price of these items
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal - count*3;
total.setText(newTotal);
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Add the new items price
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal + count*3;
total.setText(newTotal);
}
#Override
public void afterTextChanged(Editable editable) {}
};
// Now set the TextWatcher on every count EditText
// If you have different prices, you'll need multiple TextWatchers
quantity1.addTextChangedListener(textWatcher);
quantity2.addTextChangedListener(textWatcher);
...
"I still can't figure out a way to prevent it from going below 0"
One way is you can add android:digits="0123456789" to your EditText's in xml file. This will prevent user from entering anything but these numbers.
One other way is to do a check in the onClickListener. In this you'll need to check for all the EditText's values using ed.getText() and then check if its integer and is in the range acceptable by you.
One more way is to use addTextChangedListener on EditText. You can listen you what is being inputted there. You can make a check for condition when its less than 0 or an invalid character has been put in. (Its good to limit the keyboard so as to prevent garbage entry at the first place).

Menu actions and sums in Android app

So I have been trying to develop an app that adds a select quantity together in a menu, all while in Android studio/eclipse.
I am listing off drinks and to the right of each drink is a "-" (the minus) and "+" (the plus) signs, which are buttons respectively, with a "0" in the middle.
At the bottom of the screen, I have a "Total" section.
I have a screen shot of what the menu appears as thus far:
I am looking to have the + and - buttons affect the number that lies in the middle of them. So when the "+" is pressed it adds 1 and if pressed again, it adds 1 and so on, the "-" button will take 1 away from the number back to zero if need be.
It should be able to tally the total price of each drink all together and output the sum to the
"$ 0.00" near the total. The "Order" button just takes me to another screen, which will be tackled at a later date.
I have tried searching for a good many hours and there are so many links I am drowning that I can not find any help that sticks.
I know that the Java code should use the ID of the EditText's but I am confused as to how.
I will post what code I have for the xml and java code. If there is also a manifest I have to add, I can not find it.
drinkmenu.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/mainmenulayout"
android:background="#34D6D9">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Drinks:"
android:id="#+id/textView"
android:background="#FFFFFF"
android:textStyle="bold"
android:textSize="24dp"
android:layout_toStartOf="#+id/textView2"
android:layout_toLeftOf="#+id/textView2"
android:layout_marginRight="75dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button"
android:layout_alignEnd="#+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Quantity:"
android:id="#+id/textView2"
android:textSize="24dp"
android:textStyle="bold"
android:background="#FFFFFF"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/orderbtn"
android:layout_alignEnd="#+id/orderbtn" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Espresso"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Macciato"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:textSize="24dp"
android:background="#FFFFFF" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Con Panna"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Americano"
android:id="#+id/button4"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:textSize="24dp"
android:background="#FFFFFF" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Latte"
android:id="#+id/button5"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button6"
android:layout_alignTop="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#ffff45df"
android:textSize="24dp" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText2"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/button6"
android:layout_toStartOf="#+id/button6"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_above="#+id/button2" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button7"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/editText2"
android:layout_toStartOf="#+id/editText2"
android:background="#ffff45df"
android:textSize="24dp" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button8"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText3"
android:layout_toStartOf="#+id/button8"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText4"
android:layout_toStartOf="#+id/button8"
android:text=" 0"
android:textSize="24sp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_alignBottom="#+id/button8" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button9"
android:layout_toStartOf="#+id/editText3"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/editText3" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button10"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button3"
android:layout_alignRight="#+id/button8"
android:layout_alignEnd="#+id/button8" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button11"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button12"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button5"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText5"
android:layout_toStartOf="#+id/button10"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/button10"
android:layout_above="#+id/button4" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText6"
android:layout_toStartOf="#+id/button11"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/button11"
android:layout_alignBottom="#+id/button11" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText7"
android:layout_toStartOf="#+id/button12"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/button12"
android:layout_alignBottom="#+id/button12" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button14"
android:layout_toStartOf="#+id/editText6"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/editText6" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button15"
android:layout_toStartOf="#+id/editText7"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/editText7" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button16"
android:layout_toStartOf="#+id/editText5"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/editText5" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText8"
android:text="Total:"
android:background="#FFFFFF"
android:textSize="24sp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/textView"
android:layout_toStartOf="#+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText9"
android:layout_alignBottom="#+id/editText8"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:text=" $ 0.00"
android:background="#FFFFFF"
android:textSize="24sp"
android:layout_alignTop="#+id/editText8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:id="#+id/orderbtn"
android:textSize="24sp"
android:background="#drawable/buttonround"
android:layout_alignBottom="#+id/editText9"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button16"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button14"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
</RelativeLayout>
java code:
package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class DrinkMenu extends Activity {
Button button;
Button buttton6; Button buttton7; Button buttton8; Button buttton9;
Button buttton10; Button buttton11; Button buttton16; Button buttton14;
Button buttton12; Button buttton15;
// EditText editText2; EditText editText4; EditText editText5;
// EditText editText6; EditText editText7; EditText editText9;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
}
private void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.orderbtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, ThirdPartyPaymentMethod.class);
startActivity(intent);
}
});
}
}
I already have my manifest to allow button actions.
I know there are methods out there, but my searches have ended in my frustration. Any help would be appreciated. I am new to this, I enjoy the front end, but the back end is new.
I'll do a part and for remaining you have to do.
Consider,
- is a button with id 'button1' and '+' has button2
Quantity is a EditText which has id qty.
Right, coming to the code :
public class MainActivity extends Activity {
EditText quantity;
Button plus,minus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
plus = (Button) findViewById(R.id.button1);
minus = (Button) findViewById(R.id.button2);
quantity = (EditText) findViewById(R.id.qty);
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
int inum = num1+1;
quantity.setText(Integer.toString(inum));
}
});
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
int inum = num1-1;
quantity.setText(Integer.toString(inum));
}
});
}
}
For sub total, you just need to multiply quantity and the item price. For the Total Price, you just need to add all the subtotals. Hope it helps!
UPDATE about Catching Zero:
You are getting the quantity here.
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
So, if the number is 0, perform if else
if (num1==0)
{
//Create a alert showing your quantity is zero.
//here i use toast to display
Toast.makeText(this,"Your quantity is Zero. You can't reduce the quantity", Toast.LENGTH_LONG).show();
}
else
{
// do the stuffs here
}
what you want to do is something like this:
Button button = (Button) findViewById(R.id.my_button);
final TextView textView = (TextView) findViewById(R.id.my_text_view);
button.setOnClickListener(new View.OnClickListener() {
#Overrride
public void onClick(View aView) {
textView.setText("Hi");
}
});
You can write this in the onCreate() method of the Activity. Not tested yet, but this will change the text of the TextView to the desired value. Let me know if I have understood your problem correctly.

If statements forcing program to crash

I have asked a few questions before, however I have decided to go back to basics.
I am creating a maths android application, each page has three TextFields that the user can input a number into, I have parsed these TextFields into a double. However, say a user inputs a number into 2 of the TextFields, I would like the program to tell me the value of the third Text field.
At the moment I can get the program to run one calculation, however when I add the if statements in for the remaining two TextFields, the program crashes when I click calculate.
The Key behind the program is to discover which TextField is empty, so that the program can do the correct calculation.
I am currently trying .getText().toString().isEmpty(), but this is not working, I have also tried .length() == 0, however this is not working either.
When I run the emulator and click the calculator button, the program crashes when I add more variables in, however it is fine with just one calculation.
Can anyone help me figure out why the If statements are not working, and if possible can anyone offer a solution?
Sorry if my formatting is off, I'm relatively new to Java, below is the Java
Button calc1 = (Button) findViewById(R.id.current_calculate);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Charge1 = (EditText) findViewById(R.id.number_input_2);
EditText Time1 = (EditText) findViewById(R.id.number_input_3);
EditText current1 = (EditText) findViewById(R.id.number_input_1);
TextView Distances_answer = (TextView) findViewById(R.id.Distances_answer);
double charge = Double
.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
double current = Double.parseDouble(current1.getText()
.toString());
// Time is a class in Java
if (current1.getText().toString().isEmpty()) {
Distances_answer.setText("" + charge * time);
} else if (Time1.length() == 0) {
Distances_answer.setText(" " + charge / current);
}
}
});
I have also supplied the XML in case it is needed
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background1"
android:gravity="fill"
android:orientation="vertical"
android:textColor="#ffffff"
tools:context=".CurrentPage" >
<TextView
android:id="#+id/Current_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginTop="33dp"
android:text="Welcome to the Current Calculation page.
Hint - Use the check boxes to find the values that are missing "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/Equation_letter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/current_calculate"
android:layout_marginTop="37dp"
android:layout_toLeftOf="#+id/current_calculate"
android:text=" HELLO "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/Distances_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Equation_letter"
android:layout_alignBottom="#+id/Equation_letter"
android:layout_alignLeft="#+id/current_calculate"
android:layout_alignParentRight="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<TextView
android:id="#+id/t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Q"
android:layout_alignRight="#+id/Q"
android:layout_below="#+id/Q"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="t ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<CheckBox
android:id="#+id/custom3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/t"
android:layout_alignLeft="#+id/custom2"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<CheckBox
android:id="#+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/number_input_3"
android:layout_alignLeft="#+id/custom3"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Current_hint"
android:layout_alignRight="#+id/Current_hint"
android:layout_below="#+id/Equation_letter"
android:layout_marginTop="25dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/CurrentmainHelp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.31"
android:text="HELP! This equation can be used to calculate the current of an object in amps. The correct equation to discover amps is I=Q/t. Where I = current, Q = charge flowing past a point in the circuit, and t = time taken for the charge to flow. Please note, Q is measure in coulombs and t is measured in seconds, with I being measured in amperes.
Still need more help? "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/Yes_Please"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="36dp"
android:layout_weight="1.31"
android:onClick="CurrentHelp"
android:text="Yes Please"
android:textColor="#ffffff" />
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/current_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/Calculate_Current"
android:textColor="#ffffff" />
<EditText
android:id="#+id/number_input_2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_above="#+id/t"
android:layout_alignLeft="#+id/number_input_1"
android:layout_alignRight="#+id/number_input_1"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/number_input_3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/t"
android:layout_alignLeft="#+id/number_input_2"
android:layout_alignRight="#+id/number_input_2"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" />
<CheckBox
android:id="#+id/custom2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/number_input_1"
android:layout_marginLeft="45dp"
android:layout_toRightOf="#+id/number_input_1"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<EditText
android:id="#+id/number_input_1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_above="#+id/Q"
android:layout_alignRight="#+id/current_calculate"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#ffffff" />
<TextView
android:id="#+id/Q"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Equation_letter"
android:layout_below="#+id/I"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="Q ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/I"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Q"
android:layout_alignRight="#+id/Q"
android:layout_below="#+id/Current_hint"
android:layout_marginTop="65dp"
android:gravity="right|top"
android:text="I ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
I believe the application crashes because you are parsing the contents of your EditText to doubles even though they may be empty. The Double.parseDouble function will throw a NumberFormatException if it cannot parse a double out of the given String. To fix this, perform a check for empty string before trying to parse, or catch the exception.
For example:
double charge = 0.0; // some default value
if (!Charge1.getText().toString().isEmpty()) {
charge = Double.parseDouble(Charge1.getText().toString());
}
double time = 0.0; // some default value
if (!Time1.getText().toString().isEmpty()) {
time = Double.parseDouble(Time1.getText().toString());
}
double current = 0.0; // some default value
if (!current1.getText().toString().isEmpty()) {
current = Double.parseDouble(current1.getText().toString());
}

Categories

Resources