Skip to content Skip to sidebar Skip to footer

How Do I Convert The Value Of A Textview To Integer

I am designing a basic BMI calculator and I have the calculator working, but I need to convert the calculated answer from a TextView which is a double to an integer to enable me r

Solution 1:

First, The conversion is NOT from textview to int. textView aint a datatype. If you want the result in int use parseInt() method. int i = Integer.parseInt(value).

[EDIT]

Try this, this will work.

TextViewtv= (TextView) findViewById(R.id.tv);
EditTextet= (EditText) findViewById(R.id.et);

try {
doubled= Double.valueOf(String.valueOf(et.getText()));
tv.setText(String.valueOf( (int) d));
}catch (NumberFormatException e){
Toast.makeText(MainActivity.this, "Enter valid number.", Toast.LENGTH_SHORT).show();
}

I have simplified it for your understanding. The code can still be optimized.

Post a Comment for "How Do I Convert The Value Of A Textview To Integer"