get Integer value from EditText Android

48,563

Solution 1

you can achieve this as

int val = Integer.parseInt( num.getText().toString() );

and than pass val to the method addone(val);

Solution 2

Get it beautiful

   yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (checkEmptyFileds(txt_birey_adet, txt_limit)) {
                int girilenSayi = Integer.parseInt(txt_birey_adet.getText().toString());
                int limit = Integer.parseInt(txt_limit.getText().toString()); 
            } 
        }
    });


private boolean checkEmptyFileds(EditText txt_birey_adet, EditText txt_limit) {
    boolean onay = true;
    if (txt_birey_adet.getText().toString().equals("")) {
       // Util.getInstance(OzelHesap.this).bounceEffect3(txt_birey_adet);
        onay = false;
    } else if (txt_limit.getText().toString().equals("")) {
        //Util.getInstance(OzelHesap.this).bounceEffect3(txt_limit);
        onay = false;
    }
    return onay;
}
Share:
48,563
user4030877
Author by

user4030877

Updated on April 15, 2020

Comments

  • user4030877
    user4030877 about 4 years

    This is my activity.xml

    <EditText
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/inputnumber"
     android:inputType="number" />
    

    this is mainactivity.java :

    EditText num = (EditText) findViewById(R.Id.inputnumber) ;
    

    the question is how can I get the integer value from this field as an integer and use it for example in a custom class I made like this to add 1 to the value of this input field?

    my customclass.java

    public static int addone(int a) 
    {
      int b = a+1;
      return b;
    }
    
  • pankajanand18
    pankajanand18 over 9 years
    when parsing Int make sure the format of the string by eight of 2 ways. Either you set the input format as Integer only or do a try catch while parsing the string.
  • user4030877
    user4030877 over 9 years
    thanks about the solution but I used that already and when I start my app and it reaches to this point it would be "unfortunately the app stopped working..."
  • ivan.mylyanyk
    ivan.mylyanyk over 9 years
    @user4030877 could you please post the whole source file?