Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) Finishing the User Interface Loading Additional Pages

Kenan Abdulkarim
Kenan Abdulkarim
1,873 Points

Why are the 2 choice buttons in the story activity invisible and do nothing when tapped?

Every time I tap the "START YOUR ADVENTURE BUTTON" to start the next activity the 2 choice buttons are completely invisible and do nothing when tapped. I made sure that my code matches the instructor's (in fact, I even downloaded the zip file and copied the exact same code to be sure) code and the buttons are still invisible (same colour as the background).

Hello,

Could you post the code you are working with for both the Activity and the Layout XML? That will help us in being able to assist you with your issue.

Kenan Abdulkarim
Kenan Abdulkarim
1,873 Points

Thanks for the reply, James here's my code for StoryActivity.java

package kenanabdulkarim.interactivestory.ui;

import android.content.Intent; import android.graphics.drawable.Drawable; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import kenanabdulkarim.interactivestory.R; import kenanabdulkarim.interactivestory.model.Page; import kenanabdulkarim.interactivestory.model.Story; import kenanabdulkarim.interactivestory.model.Page;

public class StoryActivity extends ActionBarActivity {

// title option when creating a new activity only matters when we're running the action bar

// Convention for TAG
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story(); // need to load a page when 1) the activity is created
                                    //                          2) and when a button is tapped on
private ImageView mImageView;
private TextView mTextView;
private Button mChoice1;
private Button mChoice2;
private String mName;
private Page mCurrentPage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);

    Intent intent = getIntent();  // returns the intent that started this activity
    String mName = intent.getStringExtra(getString(R.string.key_name));  // pass in key we used
                                                   // key must be the same name we used to start this activity


    if (mName.length() == 0) {
        mName = getString(R.string.default_name);
    }

    Log.d(TAG, mName);
    Toast.makeText(StoryActivity.this, "Hi " + mName, Toast.LENGTH_SHORT).show();

    mImageView = (ImageView) findViewById(R.id.storyImageView);
    mTextView = (TextView) findViewById(R.id.storyTextView);
    mChoice1 = (Button) findViewById(R.id.choiceButton1);
    mChoice2 = (Button) findViewById(R.id.choiceButton2);

    loadPage(0);
}

private void loadPage(int choice) {
    mCurrentPage = mStory.getPage(choice);

    Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
    // getResources() --> We get the resources for the project through the activity
    // getDrawable() --> And from those resources we are getting a drawable type
    // pate.getImageId() --> then we are passing in this specific ID of the drawable that we want to get
    mImageView.setImageDrawable(drawable);
    // Once we have that drawable we set it as the image drawable for our ImageView

    String pageText = mCurrentPage.getText();
    // Add the name if placeholder included. Won't add if no placeholder (text already has appropriate      //characters needed)
    // %1 = order number of the parameter (we only have 1 parameter so it's just going to be %1
    // NOTE: If we wanted to replace more than 1 thing we could use different numbers instead of 1
    // $s is just for a simple string
    // pageText = String.format(pageText, mName);
    pageText = String.format(pageText, mName);
    mTextView.setText(pageText);

    mChoice1.setText(mCurrentPage.getChoice1().getText());
    // getChoice1() returns a choice object which we can use to get the text
    mChoice2.setText(mCurrentPage.getChoice2().getText());

    mChoice1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // need to get index of new page we want to load
            int nextPage = mCurrentPage.getChoice1().getNextPage();
            loadPage(nextPage);
        }
    });

    mChoice2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int nextPage = mCurrentPage.getChoice2().getNextPage();
            loadPage(nextPage);
        }
    });
}

}

and my activity_story.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:background="#ffffffff" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="kenanabdulkarim.interactivestory.ui.StoryActivity">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/storyImageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:src="@drawable/page0"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/stopAndInvestigate"
    android:id="@+id/storyTextView"
    android:layout_below="@+id/storyImageView"
    android:layout_alignLeft="@+id/storyImageView"
    android:layout_alignStart="@+id/storyImageView"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="15dp"
    android:lineSpacingMultiplier="1.2"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/choiceButton1"
    android:id="@+id/choiceButton1"
    android:background="#ffffffff"
    android:textColor="#ff3a8aec"
    android:layout_above="@+id/choiceButton2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>
    <!-- android:style="@style/whiteButton" -->

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/choiceButton2"
    android:id="@+id/choiceButton2"
    android:background="#ffffffff"
    android:textColor="#ff3a8aec"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/storyTextView"
    android:layout_alignStart="@+id/storyTextView"/>

</RelativeLayout>

Kenan Abdulkarim
Kenan Abdulkarim
1,873 Points

Yes, I've already tried testing the app using a real android device and the same problem occurred. I'll take the GitHub lessons as soon as I can in order to learn how to push everything I have from this project, but in the meantime, do you think there is another way for you to look at my code? Really appreciate the feedback and help!

3 Answers

Hello,

I tried out the code by pasting what you provided into the template provided in the downloads section of the teacher's notes. I was able to get it working by changing the ActionBarActivity to a regular Activity and adding in import android.app.Activity; to the imports. I think using ActionBarActivity might have been messing with the themes so you couldn't see all of the text that you were wanting. Let me know if that helps, if not, we can try some further troubleshooting.

Kenan Abdulkarim
Kenan Abdulkarim
1,873 Points

Hi James,

I tried what you told me and I still have the same problem. Every time I go to the new story activity the 2 choice buttons are completely white without the text on them and when I click on them nothing happens. I tried to download the same version of android studio that the instructor used and copied my code with the changes you mentioned and I still got the same problem. I also tried a bunch of things with no success so I'm almost out of ideas...

Hello,

I'm not too sure myself, could you try posting your code to Github so I can try using everything that you have as is? Also, could you try using a new/different emulator/device? Also, have you tried this on an actual Android device?

Mauro Mazzucco
Mauro Mazzucco
933 Points

I also have the same problem, how did you fixed it??

Ben Deitch
Ben Deitch
Treehouse Teacher

Hey Mauro! You can check your project against the project files in the downloads section. I just tested them and was able to see the buttons. One thing to mention is that the project files are probably using an older version of gradle than you, so I wouldn't compare any gradle files.

Elodie Zemour
Elodie Zemour
8,861 Points

I got a lot of errors while Generating constructors (I' m used to do it manually). Notice the order of the variables in the parenthesis.