Chapter 2 Challenge Solution


I am going through this book learning android for the first time and am going to try to do every challenge as efficiently and “correclty” as possible (correct in the sense that it doesn’t use muddled code and seems like something Google would actually implement themselves.

Here is my solution to Chapter 2 Challenge of Adding a Listener to TextView:

First i tried simply copying the code from the Next Button applying it the mQuestionTextView like so (Bold code is what i added to the chapters tutorial code):

mQuestionTextView = (TextView)findViewById(; mQuestionTextView.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View V){ mCurrentIndex = (mCurrentIndex+1)%mQuestionBank.length; updateQueston(); }

But after running this code it didn’t seem to respond at all to clicking. So i did some googling around and saw that the TextView XML widget has a “clickable” attribute that can be set to True. I believe this is the correct way to proceed so i added it to my xml to get this:

<TextView android:id="@+id/question_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" android:clickable="true"/>

And voila! It worked!

On to the next Challenge: Add a Previous Button.

This one seems simple enough, just add another button and copy the code from next button into its onClick function, except decrement the current index instead of increment.

I started with the xml widget.

Creating a new button below the next button won’t get the desired UI because its parent would be the Vertical Linear Layout. So instead i created a horizontal layout just like our True/False Buttons are in and put the next button inside it along with a new Previous Button. But since we want the previous button the left we need to put it above the next button, instead of below. I also decided to add the left arrow and assumed that since we used the drawableRight attribute for next we should use drawableLeft for the previous, my assumption was correct.

I added this line to the strings xml file:

<string name="prev_question_button">Previous</string>

And this was my new layout xml code:





But for some reason it refuses to resolve the resource @string/prev_question_button, it says it cannot resolve it despite having the other resource in the strings xml. Quite a mystery. Even worse! If i just type plain text into the that attribute (i know its cheating), and i run that application, the button doesn’t even show up! It’s like it doesn’t even exist…

Please help!

EDIT: After closing Android Studio (i much prefer it over eclipse) and making a brand new project with copying the code into it, it seems to resolve fine. Now the code that i used. going backwards is a bit different than fowards, you can decrement like going next increments, but once you his mCurrentIndex = 0 you need to loop back to the top index. so i used an if statement to do this:

mPrevButton = (Button)findViewById(; mPrevButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { if (mCurrentIndex == 0) { mCurrentIndex = mQuestionBank.length - 1; } else { mCurrentIndex = mCurrentIndex - 1; } updateQueston(); } });

This worked perfectly.

Lastly the ImagButtons were easy. I simply changed the buttons data type in the java file to ImageButton (which requires me to organize my imports) and then change the cast to (ImageButton) instead of (Button) when i used findViewById.

This worked as well

Challenge: Complete.


I did the same but get the error: “XML document structures must start and end within the same

How come?