Link to home
Start Free TrialLog in
Avatar of Computer Guy
Computer Guy

asked on

Updating Text Issue

Hi,

I have a tip calculator app in Android - I have coded it so I have the option to split the total and tip by x number of guests.

I have a textview with the default text of "Tip". If the number of guests changes - I want the text to be "Split Tip".

In my app, I have a textWatcher

   
private TextWatcher numGuestTextWatcher = new TextWatcher() 
   {
      @Override
      public void onTextChanged(CharSequence s, int start, 
         int before, int count) 
      {         
         // convert billEditText's text to a double
         try
         {
            numGuests = Integer.parseInt(s.toString());
         } // end try
         catch (NumberFormatException e)
         {
            numGuests = 1; // default if an exception occurs
         } // end catch 
         updateTextFields();
      } //

Open in new window

      
Then the updateTextFields method:
        
	     private void updateTextFields()
   {
	   if (numGuests > 1) {
		   tipTextView.append("Split Tip");
	   }
   }

Open in new window

 
 But when I change the value back to 1, the default text does not change back to "Tip" which is what I set the default string text to.

Any clue why?
Avatar of CPColin
CPColin
Flag of United States of America image

You don't have any code that fires when numGuests is less than or equal to 1. If you put an else block in that updateTextFields() method, you should be able to get the behavior you're looking for.
Avatar of Computer Guy
Computer Guy

ASKER

Like this:

{
         if (numGuests > 1) {
               tipTextView.append("Split Tip");
else
tipTextView.append("Tip");

         }
   }
ASKER CERTIFIED SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial