0

I have layout in my application like below: When i enter 100 from soft keyboard in edit text it should show correct answer toast automatically and it should allow only 3 numbers to enter in input text.

How to do this?

10 x 10 = ___

i tried with Textwatcher but its not working. When i enter correct answer EditText 10 X 10 should change to next value.

    int min = 0;
int max = 20;
Random r = new Random();
int mRandomOne = r.nextInt(max - min + 1) + min;
int mRandomTwo = r.nextInt(10 - 0 + 1) + 0;
        mFillAnswer = (EditText) findViewById(R.id.fill);
    mFillAnswer.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            int a = Integer.parseInt(mOneValue.getText().toString());
            int b = Integer.parseInt(mTwoValue.getText().toString());

            int val = Integer.parseInt(mFillAnswer.getText().toString());
            if (val == (a * b)) {
                mOneValue.setText(String.valueOf(mRandomOne));
                mTwoValue.setText(String.valueOf(mRandomTwo));
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            mOneValue.setText(String.valueOf(mRandomOne));
            mTwoValue.setText(String.valueOf(mRandomTwo));
        }
    });
5
  • specify where your errors are. some code will help us identify your problem Commented Jan 8, 2017 at 17:11
  • I don't think you would need to do anything inside afterTextChanged method Commented Jan 8, 2017 at 17:18
  • once enter correct value in Edit text ..question should be changed...in my case...10 x 10 = __..once i enter 100 here immeditaley question changed from 10 x 10 to next random question Commented Jan 8, 2017 at 17:22
  • can you make yourself clear. whats the result when you type in 100 ? from what i see it should change to a new question Commented Jan 8, 2017 at 17:26
  • after enter 100 in Edit text, question is not changing...still it shows 10 x 10 mOneValue.setText(String.valueOf(mRandomOne)); mTwoValue.setText(String.valueOf(mRandomTwo)); Commented Jan 8, 2017 at 17:28

1 Answer 1

2
  1. For allowing only 3 numbers use android:maxLength="3" in your EditText

  2. For automatically detecting the correct or incorrect answer use TextWatcher

Update To change the TextView values:

@Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        //To avoid exception
        if(mFillerAnswer.getText().toString().equals("")){return;}

        int a = Integer.parseInt(mOneValue.getText().toString());
        int b = Integer.parseInt(mTwoValue.getText().toString());

        int val = Integer.parseInt(mFillAnswer.getText().toString());
        if (val == (a * b)) {
            //generate and use Random numbers here
            mOneValue.setText(r.nextInt(max - min + 1) + min);
            mTwoValue.setText(r.nextInt(10 - 0 + 1) + 0);

            //to clear edit text
            mFillAnswer.setText("")
        }
    }

You were generating random numbers only once so it was pointless to be expecting newer values while the generation code is outside the TextWatcher scope

Sign up to request clarification or add additional context in comments.

15 Comments

I don't see any problem in your textwatcher code. Why don't you do some logging inside the onTextChanged method if it is being called
after enter 100 in Edit text, question is not changing...still it shows 10 x 10
you have to generate the random values inside if statement not outside the whole TextWatcher code
Yes now it is working,,,,But EditText value is not cleared..it shows previous entered value...Once question change, EdiText field should be empty
add mFillAnswer.setText("0") inside the if statement and you are good to go
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.