I have this form for the user to fill up and one of the Edit Text is BMI and I was thinking if it is possible to auto fill that BMI Edit Text after the user has enter the Height and Weight without clicking any button. I have Googled and found such thing called Text Watcher. I tried implementing it in my code but when I run the application the Edit Text for the BMI is still empty and so is the database.
package mdad.project;
import com.example.manandhowproject.R;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
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.Toast;
public class Userreg extends Activity implements TextWatcher {
SQLiteDatabase db;
Button btnNext;
EditText etName, etAge, etHeight, etWeight, etBMI;
double result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userreg);
etName = (EditText) findViewById(R.id.etName);
etAge = (EditText) findViewById(R.id.etAge);
etHeight = (EditText) findViewById(R.id.etHeight);
etWeight = (EditText) findViewById(R.id.etWeight);
etBMI = (EditText) findViewById(R.id.etBMI);
Button button = (Button) findViewById(R.id.btnSignOut);
button.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
Toast.makeText(getApplicationContext(), "Log Out Success ! ", Toast.LENGTH_LONG).show();
Intent msg2 = new Intent(Userreg.this, Login.class);
startActivity(msg2);
}
});
Button btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
String name = etName.getText().toString();
String age = etAge.getText().toString();
int nAge = new Integer(age).intValue();
String height = etHeight.getText().toString();
int nHeight = new Integer(height).intValue();
String weight = etWeight.getText().toString();
int nWeight = new Integer(weight).intValue();
String bmi = etBMI.getText().toString();
String sql = "insert into UserInfo (Name,Age,Height,Weight,BMI) values( '" + name + "','" + nAge + "','" + nHeight + "', '" + nWeight + "', '" + bmi + "')";
String result = updateTable(sql);
Intent msg3 = new Intent(Userreg.this, Regsuccess.class);
startActivity(msg3);
}
});
String sql = "create table if not exists UserInfo (recld integer PRIMARY KEY autoincrement, Name text, Age text, Height text, Weight text, BMI text )";
String result = createDatabase(sql, "UserInf.db");
}
private void calculate() {
String height = etHeight.getText().toString();
int nHeight = new Integer(height).intValue();
String weight = etWeight.getText().toString();
int nWeight = new Integer(weight).intValue();
result = nWeight / (nHeight * nHeight);
String textResult = "" + result;
etBMI.setText(textResult);
}
String createDatabase(String sql, String dbName) {
try {
System.out.println(sql);
db = SQLiteDatabase.openOrCreateDatabase("sdcard/" + dbName, null);
db.beginTransaction();
db.execSQL(sql);
db.setTransactionSuccessful();
db.endTransaction();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
System.out.println(e.toString());
return ("error open DB");
}
return "";
}
String updateTable(String sql) {
try {
System.out.println(sql);
db.beginTransaction();
db.execSQL(sql);
db.setTransactionSuccessful();
db.endTransaction();
} catch (Exception e) {
System.out.println(e.toString());
return ("Error updating DB");
}
return ("DB updated");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, 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);
}
@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
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
calculate();
}
}