I am wondering how to convert an edittext input to an int, I have the user input a number, it than divides it by 8.
MainActivity.java
@SuppressWarnings("unused")
public void calcSpeed(View view)
{
setContentView(R.layout.activity_speed);
final TextView mTextView = (TextView) findViewById(R.id.textView3);
mTextView.setText("You should be getting: " +netSpedCalcd);
}
activity_main.xml
<EditText
android:id="@+id/editText1"
android:inputType="number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="62dp"
android:ems="10" >
I had the same problem myself. I'm not sure if you got it to work though, but what I had to was:
The third line of code caused the app to crash without using the .getText() before the .toString().
Just for reference, here is my XML:
Try the line below to convert editText to integer.
Try this,
You can use parseInt with try and catch block
Or you can create your own tryParse method :
and use it in your code like:
Note: The following code without try/catch will throw an exception
Or
You can use like this