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

Eleni Minadaki
Eleni Minadaki
3,687 Points

How to make Scroll method works in the App?

Hi! have finished the InteractiveStory App lessons and I am trying to make a new app for practice, based in these lessons. I try to put in Scroll method but something goes wrong. If someone knows how to make this, i 'llappreciate it a lot! Below is my code. public class StoryActivity extends AppCompatActivity {

StoryActivity.java public static final String TAG = StoryActivity.class.getSimpleName();

private Story mStory = new Story();

private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;


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


  //Intent intent = getIntent();
   //mName = intent.getStringExtra(getString(R.string.key_name));

    if(mName == null){
        mName = "Friend";
    }

    Log.d(TAG, mName);

    mImageView = (ImageView)findViewById(R.id.storyImageView);
    mTextView = (TextView)findViewById(R.id.storyTextView);
   mPreviousButton = (Button)findViewById(R.id.previousButton);
   mNextButton = (Button)findViewById(R.id.nextButton);
    mScrollView = (ScrollView)findViewById(R.id.SCROLLER_ID);

    loadPage(0);
}
private void loadPage(int choice){
 mCurrentPage = mStory.getPage(choice);
    //edw prepei na kanw kati gia to previous
    //mPreviousPage = mStory.getPage(choice);

    Drawable drawable  = getResources().getDrawable(mCurrentPage.getImageId());
    mImageView.setImageDrawable(drawable);

    String pageText = mCurrentPage.getText();
    //add the name if placeholder included.
   //pageText  = String.format(pageText,mName);
    mTextView.setText(pageText);

    if(mCurrentPage.isSingle()){
        mPreviousButton.setVisibility(View.INVISIBLE);
        mNextButton.setText("Start From The Begining");
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadPage(1);
            }
        });

    }else{

        mPreviousButton.setText(mCurrentPage.getChoices().getText1());
        mNextButton.setText(mCurrentPage.getChoices().getText2());

        mPreviousButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int previousPage = mCurrentPage.getChoices().getPreviousPage();
                loadPage(previousPage);
            }
        });

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

} content_story.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".ui.StoryActivity" tools:showIn="@layout/activity_story2" android:background="@android:color/white">

<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" />

<ScrollView
    android:id="@+id/SCROLLER_ID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:fillViewport="true"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:id="@+id/storyTextView"
    android:layout_below="@+id/storyImageView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="15dp"
    android:lineSpacingMultiplier="1.2"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Back"
    android:id="@+id/previousButton"
    android:layout_marginBottom="45dp"
    android:background="@android:color/white"
    android:textColor="#3a8aec"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignWithParentIfMissing="false"
    android:layout_alignParentTop="false"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:id="@+id/nextButton"
    android:background="@android:color/white"
    android:textColor="#ff3a8aec"
    android:layout_alignTop="@+id/previousButton"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="false"
    android:layout_alignWithParentIfMissing="false"
    android:layout_alignParentTop="false"/>

</RelativeLayout>

Ben Morse
Ben Morse
6,068 Points

Hey did you get this issued resolved?

2 Answers

Eleni Minadaki
Eleni Minadaki
3,687 Points

Hey Ben, In your MAIN activity write before the clickListener : theNameofYourtext.setMovementMethod(new ScrollingMovementMethod()); In XML activity, before the textView : <ScrollView android:id="...." android:layout_width="...." android:layout_height="...." ...and all the layouts you need.....>

here write your TextView

after just write again </ScrollView> Hope this helps, if you need further help let me know.

Ben Morse
Ben Morse
6,068 Points

ah gotcha. That worked. Thanks man :)

Eleni Minadaki
Eleni Minadaki
3,687 Points

Yes I did, thanks a lot Ben

Ben Morse
Ben Morse
6,068 Points

ok, I was curious on how you did?