Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) User Input Getting Text from an EditText

Get an int input from user and Toast it

Now that I know how to get String, how do i get an int and use that value to do some math and Toast a new int value to the user?

2 Answers

Sai Sushanth Durvasula
Sai Sushanth Durvasula
2,759 Points

//Using EditText

EditText editText= (EditText)findViewById(R.id.editText);

//where Id of EditText in layout is editText if its different use that

String numAsString = editText.getText();

int num =Integer.parseInt(numAsString);

//do some math with num

Now output it using Toast as follows:

Toast.makeText(getApplicationContext(), "Result="+num, Toast.LENGTH_SHORT).show();

Thank you very much