Murat Kaya
Murat Kaya

Reputation: 193

How to use TextWatcher to automatically get values from edittext and multiply them?

The app force shuts when i enter a number in edittext. I want the app to automatically calculate the BMI when values are entered without pressing on a button.

I know nothing about TextWatcher yet i researched and came up with this code. Its not working though. Whats wrong with it?

public class BodyMassIndex extends Activity implements View.OnClickListener,
    TextWatcher {

double result;
EditText age, height, weight;
ToggleButton sex, cmin, kglb;
TextView tvResult;
double heightInt = 1;
double weightInt = 0;
int ageInt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bmi);
    initialize();

    sex.setOnClickListener(this);
    cmin.setOnClickListener(this);
    kglb.setOnClickListener(this);


}

private void initialize() {
    // TODO Auto-generated method stub

    age = (EditText) findViewById(R.id.etAge);
    height = (EditText) findViewById(R.id.etHeight);
    weight = (EditText) findViewById(R.id.etWeight);
    sex = (ToggleButton) findViewById(R.id.tbSex);
    cmin = (ToggleButton) findViewById(R.id.tbCMIN);
    kglb = (ToggleButton) findViewById(R.id.tbKGLB);
    tvResult = (TextView) findViewById(R.id.tvResult);
    age.addTextChangedListener(this);
    height.addTextChangedListener(this);
    weight.addTextChangedListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch (v.getId()) {
    case R.id.tbCMIN:
        heightInt = heightInt * 0.0254;
        break;
    case R.id.tbKGLB:
        weightInt = weightInt * 0.45359237;
        break;
    }

}

@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    calculate();
}

private void calculate() {
    // TODO Auto-generated method stub
     Editable H = height.getText(), W = weight.getText();

    if(H != null)
        heightInt = Double.parseDouble(H.toString());
    if (W != null)
        weightInt = Double.parseDouble(W.toString());

    result = weightInt / (heightInt * heightInt);
    String textResult = "Your BMI is " + result;
    tvResult.setText(textResult);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub


}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

    }
}

'

Upvotes: 0

Views: 1156

Answers (2)

ihsan kocak
ihsan kocak

Reputation: 1581

From http://developer.android.com/reference/android/text/TextWatcher.html about afterTextChanged method: "but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively....you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) "

Upvotes: 1

ARMAGEDDON
ARMAGEDDON

Reputation: 959

you already have :

 height = (EditText) findViewById(R.id.etHeight);
    weight = (EditText) findViewById(R.id.etWeight);

only change calculate() method:

private void calculate()
{
//remove the Editable thing declaration...
heightInt = Double.parseDouble(height.getText().toString());
wightInt = Double.parseDouble(weight.getText().toString());
result = weightInt / (heightInt * heightInt);
String textResult = "Your BMI is " + result;
tvResult.setText(textResult);

}

P.S: Here we're assuming that EditText contains only a number and not letters. Letters will force close the app. So you'll have to check for that.

Upvotes: 0

Related Questions