Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting EditText to int? (Android)

I am wondering how to convert an EditText input to an int, I have the user input a number, which then 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" >
like image 446
user2101454 Avatar asked Sep 10 '25 15:09

user2101454


2 Answers

you have to used.

String value= et.getText().toString();
int finalValue=Integer.parseInt(value);

if you have only allow enter number then set EditText property.

android:inputType="number"

if this is helpful then accept otherwise put your comment.

like image 172
Harshid Avatar answered Sep 13 '25 00:09

Harshid


Use Integer.parseInt, and make sure you catch the NumberFormatException that it throws if the input is not an integer.

like image 34
Jason Sankey Avatar answered Sep 13 '25 00:09

Jason Sankey