Luciano Santis
Luciano Santis

Reputation: 173

Scrollable TextView in ViewPager

I have a TextView inside a Fragment in a ViewPager and I want to make the text in the TextView scrollable. For some reason this doesn't work and the textview does not scroll.

This is what I've tried:

Code in fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
   ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_profile_view, container, false);
   about = (TextView) view.findViewById(R.id.profileView_aboutContent_textView);
   about.setMovementMethod(new ScrollingMovementMethod());  
   return view;

}

xml:

<TextView
    android:id="@+id/profileView_aboutContent_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/profileView_age_editText"
    android:layout_alignBottom="@+id/profileView_age_editText"
    android:layout_marginLeft="30dp"
    android:layout_toRightOf="@+id/profileView_profileName_textView"
    android:scrollbars="vertical"
    android:maxLines="5"
    android:text="@string/provisional_about_text"
    android:textColor="#0066CC" />

I know this works because I used it to make a tetxview scrollable which is in an activity not in a fragment within a ViewPager. When I tried the same in my fragment it doesn't work. I've also tried to apply setmovementmethod to the textview in onStart method within the fragment class but that did not work either. I also thought the textview there was a problem with the TextView id and it was returning null so I tried to set the TextView text with setText(); in the fragment to see if the class was getting the textview id and it actually worked so I don't know why the setmovementmethod doesn't work.

Does anybody know what could be the problem?

Thanks for any help.

Upvotes: 4

Views: 2074

Answers (2)

Luciano Santis
Luciano Santis

Reputation: 173

I figured out the problem. The TextView was scrolling vertically because it was following the Pager View behavior which was to move horizontally from one fragment to the other so I had to tell the TextView in the Fragment class not to follow the parent behavior.

This is the code:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_profile_view, container, false);
     about = (TextView) view.findViewById(R.id.profileView_aboutContent_textView);
    about.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            about.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

     about.setMovementMethod(new ScrollingMovementMethod());  
    return view;

Upvotes: 2

Libin
Libin

Reputation: 17085

Add a scrollview to textview in layout like this...

 <ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<TextView
    android:id="@+id/testText"
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</ScrollView>

Here is the complete code it works fine with Viewpager..

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(0);
}

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

ViewPagerAdapter class inside MainActivity

 private class ViewPagerAdapter extends FragmentStatePagerAdapter {
    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return new TestFragment();
    }

    @Override
    public int getCount() {
        return 3;
    }
}

TestFargment.java

 public class TestFragment extends Fragment
{
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View row =  inflater.inflate(R.layout.activity_main, container, false);

    TextView textView = (TextView)row.findViewById(R.id.profileView_aboutContent_textView);
    textView.setMovementMethod(new ScrollingMovementMethod());
    return row;
  }
}

Upvotes: 1

Related Questions