This question already has an answer here:
i tried to call the setText() using the float but it dosnt seem to work can somone help me fix the problem?
public class Bmi extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
final EditText h = (EditText)findViewById(R.id.editText2);
final EditText w = (EditText)findViewById(R.id.editText3);
final TextView r = (TextView)findViewById(R.id.textView4);
Button calculate = (Button) findViewById(R.id.button1);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
float height=0;
float weight=0;
float result=0;
height= Float.parseFloat(h.getText().toString());
weight= Float.parseFloat(w.getText().toString());
result = (weight/(height * height));
r.setText(result);
}
});
}
}
im trying to make a simple bmi calculator just as practice but im having issues on making it work
You can use
You should convert your float to a String by
Or the quick and dirty way
If you want it localized (Dot or Comma seperated decimal number)
Just replace
YOURCONTEXT
withMainActivity.this
if you are in the MainActivity orgetActivity()
if you are in a FragmentIf you want to set min or max fraction digits try this:
In order to display float value inside TextView you'll need to convert it to String first.
You can convert any primitive data type(int, float, double,boolean,etc) to String by using String.valueOf(value) method.
Here value is the variable which you want to convert to String.
You can use
Alternatively
Please comment if further help is required.