Reputation: 43
I'm having problems when running this. Instead of incrementing my tipAmount
by .01
it goes from 0.10
to 0.11
to 0.01
, but I have no idea why, and I can't seem to see anything wrong with the code or the onClick
logic.
Code
package com.paulk.tipcalc;
import android.R.integer;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
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.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class TipCalc extends ActionBarActivity implements OnClickListener{
public final static String BILL_WITHOUT_TIP = "BILL_WITHOUT_TIP";
public final static String CURRENT_TIP = "CURRENT_TIP";
public final static String TOTAL_BILL = "TOTAL_BILL";
private double billBeforeTip;
private double tipAmount;
private double totalAmount;
//private double billAfterTip = (billBeforeTip * tipAmount);
EditText billBeforeTipET;
EditText tipAmountET;
EditText totalAmountET;
Button increment;
Button decrement;
SeekBar tipSeekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tip_calc);
//if its a new session
if(savedInstanceState == null){
billBeforeTip = 0.0;
tipAmount = 0.10;
totalAmount = 0.0;
//if its a saved session
}else{
billBeforeTip = savedInstanceState.getDouble(BILL_WITHOUT_TIP);
tipAmount = savedInstanceState.getInt(CURRENT_TIP);
totalAmount = savedInstanceState.getDouble(TOTAL_BILL);
}
//pulling through each of the text fields
billBeforeTipET = (EditText) findViewById(R.id.BillEditText);
tipAmountET = (EditText) findViewById(R.id.tipEditText);
totalAmountET = (EditText) findViewById(R.id.totalEditText);
tipSeekBar = (SeekBar) findViewById(R.id.changeTipSeekBar);
increment = (Button) findViewById(R.id.incrementButton);
decrement = (Button) findViewById(R.id.decrementButton);
billBeforeTipET.addTextChangedListener(billBeforeTipListener);
tipAmountET.addTextChangedListener(tipAmountListener);
tipSeekBar.setOnSeekBarChangeListener(tipSeekBarChangeListener);
increment.setOnClickListener(this);
decrement.setOnClickListener(this);
}
private TextWatcher tipAmountListener = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try{
tipAmount = (int) Double.parseDouble(s.toString());
}catch(NumberFormatException e){
tipAmount = 0.10;
}
updateTipandTotalAmount();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
private TextWatcher billBeforeTipListener = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//making sure the correct format is input into the field
try{
billBeforeTip = Double.parseDouble(s.toString());
}catch(NumberFormatException e){
billBeforeTip = 0.0;
}
updateTipandTotalAmount();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
private void updateTipandTotalAmount(){
//converts the double value into a string value
double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
//how the total amount is calculated
double totalAmount = billBeforeTip + (billBeforeTip * tipAmount);
//formating the total amount
totalAmountET.setText(String.format("%.02f", totalAmount));
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putDouble(BILL_WITHOUT_TIP, billBeforeTip);
outState.putDouble(CURRENT_TIP, tipAmount);
outState.putDouble(TOTAL_BILL, totalAmount);
}
private OnSeekBarChangeListener tipSeekBarChangeListener = new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
tipAmount = (tipSeekBar.getProgress()*.01);
tipAmountET.setText(String.format("%.02f", tipAmount));
}
};
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.incrementButton:
tipAmount = tipAmount + 0.01;
tipAmountET.setText(String.format("%.02f", tipAmount));
break;
case R.id.decrementButton:
tipAmount = tipAmount - 0.01;
tipAmountET.setText(String.format("%.02f", tipAmount));
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tip_calc, 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);
}
}
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.paulk.tipcalc.TipCalc" >
<EditText
android:id="@+id/BillEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/billTextView"
android:layout_alignBottom="@+id/billTextView"
android:layout_toEndOf="@+id/billTextView"
android:layout_toRightOf="@+id/billTextView"
android:ems="5"
android:inputType="numberDecimal"
android:text="@string/bill_edit_text" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/billTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="19dp"
android:layout_marginLeft="19dp"
android:layout_marginTop="20dp"
android:text="@string/bill_text_view" />
<TextView
android:id="@+id/tipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/BillEditText"
android:layout_marginStart="34dp"
android:layout_marginLeft="34dp"
android:layout_toEndOf="@+id/billTextView"
android:layout_toRightOf="@+id/BillEditText"
android:text="@string/tip_text_view" />
<Button
android:id="@+id/incrementButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignEnd="@+id/tipEditText"
android:layout_alignRight="@+id/tipEditText"
android:layout_alignTop="@+id/tipTextView"
android:onClick="increaaseTip"
android:text="@string/increment"
android:textSize="12sp" />
<TextView
android:id="@+id/totalTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tipEditText"
android:layout_toEndOf="@+id/billTextView"
android:layout_toRightOf="@+id/billTextView"
android:text="@string/total_text_view" />
<EditText
android:id="@+id/totalEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/totalTextView"
android:layout_marginTop="19dp"
android:layout_toStartOf="@+id/tipTextView"
android:layout_toLeftOf="@+id/tipTextView"
android:ems="5"
android:inputType="numberDecimal"
android:text="@string/total_edit_text" />
<EditText
android:id="@+id/tipEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/incrementButton"
android:layout_marginTop="20dp"
android:layout_toEndOf="@+id/billTextView"
android:layout_toRightOf="@+id/tipTextView"
android:ems="4"
android:inputType="numberDecimal"
android:text="@string/tip_edit_text" />
<Button
android:id="@+id/decrementButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_above="@+id/tipEditText"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:layout_toLeftOf="@+id/incrementButton"
android:layout_toStartOf="@+id/incrementButton"
android:text="@string/decrement"
android:textSize="12sp"
android:onClick="DecreaseTip"/>
<SeekBar
android:id="@+id/changeTipSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/billTextView"
android:layout_alignRight="@+id/BillEditText"
android:layout_alignTop="@+id/tipEditText"
android:progress="15" />
<TextView
android:id="@+id/changeTipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/changeTipSeekBar"
android:layout_alignLeft="@+id/changeTipSeekBar"
android:text="@string/change_tip_text_view" />
</RelativeLayout>
Strings Class
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TipCalc</string>
<string name="action_settings">Settings</string>
<string name="bill_text_view">Bill:</string>
<string name="bill_edit_text"></string>
<string name="decrement">-</string>
<string name="increment">+</string>
<string name="tip_text_view">Tip:</string>
<string name="tip_edit_text">0.10</string>
<string name="total_text_view">Total:</string>
<string name="total_edit_text"></string>
<string name="change_tip_text_view">Tip Changer</string>
</resources>
I just want it so it starts at 0.10 and increments and decrements by 0.01, however ive tried a few things and had no luck, any idea why?
Upvotes: 0
Views: 77
Reputation: 393831
Your problem might be that you are using getInt
to load a double variable from the Bundle
:
tipAmount = savedInstanceState.getInt(CURRENT_TIP);
This would return 0, since you are saving the value as double:
outState.putDouble(CURRENT_TIP, tipAmount);
Change it to:
tipAmount = savedInstanceState.getDouble (CURRENT_TIP);
public int getInt (String key)
Added in API level 1Returns the value associated with the given key, or 0 if no mapping of the desired type exists for the given key
Another problem is in your onTextChanged
method :
tipAmount = (int) Double.parseDouble(s.toString());
If the result of parsing the double < 1, tipAmount will be set to 0 by the casting to int.
Upvotes: 1
Reputation: 152817
In your textwatcher:
tipAmount = (int) Double.parseDouble(s.toString());
You're converting the tip amount to integer. (int)0.11
is 0
and adding 0.01
to it yields 0.01
again.
Upvotes: 1