Skip to content Skip to sidebar Skip to footer

Number Range On An Edittext - Only Want Numbers Between 1 And 10

I'm trying to make an app that only allows a user to enter between 1 and 10, I've tried writing my own method where if the number is out of that range, it changes the text views to

Solution 1:

You have issue on this line

int value = Integer.parseInt(num.toString());

change to:

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

Now you call toString() method from Object for EditText object. You have to call getText() method for it as first and after then call toString() method for CharSequence

UPDATE: You find two times the same view with the same ids. Look at the code below:

TextViewtvNum= (TextView) findViewById(R.id.tvNumEnt);
TextViewtvName= (TextView) findViewById(R.id.tvNumEnt);

there should be R.id.tvNumEnt in the second time, I think so...

TextViewtvNum= (TextView) findViewById(R.id.tvNumEnt);
TextViewtvName= (TextView) findViewById(R.id.tvNumEnt);

Solution 2:

The main problem here is that you are trying to get the number in the edittext with: num.toString(), instead you have to use: num.getText().toString()

Post a Comment for "Number Range On An Edittext - Only Want Numbers Between 1 And 10"