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

How to get EditText Value as integer and put into TextView without button OnClick method

How to get EditText Value as integer and put into TextView without button OnClick method ?

Hello, Suppose you have declared: private EditText mNumber; You can then declare a String variable, like this: private String mNumberString; You can get the value from the EditText as a String: On the onCreate method, do:

mNumber = (EditText) findViewById(R.id.mNumber);
// Comment, you need to have added your EditText in your xml layout file for the activity for the previous line to //work // To get the String from the EditText do: mNumberString = mNumber.getString(); //Then you can parse this String to an Integer: int value = Integer.parseInt(mNumberString);

I hope this helps.

Thanks Peter, I Tried but it gives me an error :Cannot resolve method '.getString();'

and then I tried this:

    mCurrentIDV = (EditText) findViewById(R.id.twEditTextIDV);
    EditText mCubicCapacity = (EditText)findViewById(R.id.twEditTextCC);
    EditText mOdDiscount = (EditText)findViewById(R.id.percentageOwnDamageEditText);
    EditText mAgeDiscount = (EditText)findViewById(R.id.twAgDis);
    final TextView mAmtLabel = (TextView)findViewById(R.id.netPremiumAmt);

    mCurrentIDVString = mCurrentIDV.getText().toString();
    int currentIDV = Integer.parseInt(mCurrentIDVString);
    mAmtLabel.setText(currentIDV);

Vinod mehta, yes, that's the way to do it: mCurrentIDVString = mCurrentIDV.getText().toString();

I told you to do .getString(), but it's getText().toString(), that was my mistake.

Anyway, does the code run now?

No, Not yet now it gives me another error Caused by: java.lang.NumberFormatException: Invalid int: at java.lang.Integer.invalidInt(Integer.java:138) at java.lang.Integer.parseInt(Integer.java:358) at java.lang.Integer.parseInt(Integer.java:334) at com.bimahelpline.android.ui.TwoWheeler.onCreate(TwoWheeler.java:41)