How do I return an int from EditText? (Android)

JavaAndroidAndroid Edittext

Java Problem Overview


Basically, I want an EditText in Android where I can have an integer value entered into. Perhaps there is a more appropriate object than EditText for this?

Java Solutions


Solution 1 - Java

For now, use an EditText. Use android:inputType="number" to force it to be numeric. Convert the resulting string into an integer (e.g., Integer.parseInt(myEditText.getText().toString())).

In the future, you might consider a NumberPicker widget, once that becomes available (slated to be in Honeycomb).

Solution 2 - Java

Set the digits attribute to true, which will cause it to only allow number inputs.

Then do Integer.valueOf(editText.getText()) to get an int value out.

Solution 3 - Java

First of all get a string from an EDITTEXT and then convert this string into integer like

      String no=myTxt.getText().toString();       //this will get a string                               
      int no2=Integer.parseInt(no);              //this will get a no from the string
			    	 

Solution 4 - Java

You can do this in 2 steps:

1: Change the input type(In your EditText field) in the layout file to android:inputType="number"

2: Use int a = Integer.parseInt(yourEditTextObject.getText().toString());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRob S.View Question on Stackoverflow
Solution 1 - JavaCommonsWareView Answer on Stackoverflow
Solution 2 - JavaCheryl SimonView Answer on Stackoverflow
Solution 3 - JavaPir Fahim ShahView Answer on Stackoverflow
Solution 4 - JavaGirish B.RView Answer on Stackoverflow