Challenge: Add a Listener to the TextView


This is my solution for the first Challenge:

    mQuestionTextView = (TextView)findViewById(;

    mQuestionTextView.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            mCurrentIndex=(mCurrentIndex+1)% mQuestionBank.length;


1 Like


The same code is used twice: 1) in button ‘Next’, 2) in TextView listener
So I prefer to move it into separate method

private void moveNext() {
    currentIndex = (currentIndex + 1) % questionBank.length;

With regards to this line:

`mCurrentIndex=(mCurrentIndex+1)% mQuestionBank.length;`

Why is it necessary to include % mQuestionBank.length;?

1 Like

When we click next next next many times, mCurrentIndex value will be whatever plus 1 such as 21 22 23…
Then modulo(%) by mQuestionBank.lenght (.lenght is static method use to get number of members) the value will be not more than 6 questions that mQuextionBank has.