Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get Integer value from EditText Android

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;
}
like image 325
user4030877 Avatar asked Sep 12 '14 09:09

user4030877


People also ask

How to get Int value from EditText in Android Studio?

// 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.

How do I get the integer value from EditText in Kotlin?

The Best Answer is you have to used. String value= et. getText(). toString(); int finalValue=Integer.

What is getText Android?

getText() method and the other one is . setText() method that we use to set some pre-written text or string. EditText demo; demo=(EditText)findViewById(R. id.


2 Answers

you can achieve this as

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

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

like image 193
ivan.mylyanyk Avatar answered Oct 13 '22 02:10

ivan.mylyanyk


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;
}
like image 26
Samir Avatar answered Oct 13 '22 01:10

Samir