ScrollView inside a fragment with TabLayout is not scrolling, why?

10,435

The main problem is in your main_activity.xml. Try to copy this layout that I got from chrisbanes' project and let's see what happens:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="24dp"
        app:fabSize="normal"
        app:borderWidth="0dp"
        app:elevation="6dp"
        android:src="@drawable/ic_done"/>

</android.support.design.widget.CoordinatorLayout>

In some cases, using ScrollView within CoordinatorLayout may cause the screen is unable to scroll. So use NestedScrollView instead.

Share:
10,435
Sebas Pinto
Author by

Sebas Pinto

Updated on June 23, 2022

Comments

  • Sebas Pinto
    Sebas Pinto about 2 years

    im a noob in android development and i was in a introduction course of Cryptography so i decide combine both and make this app. I use a TabLayout for making a tab for each cipher method (RSA and XOR), and one tab for Instructions and Help. Everything was working perfectly until i add a scrollView in the Help Tab with the instructions. The scrollView is not scrolling, and i don´t know why, i've been searching on web for help and doing every posible solution like change the width an height ScrollView properties, i'm stuck, can you help me please? Screenshots

    -----EDIT 3/02/16-----

    ** My main_activity.xml **

        <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        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"
        android:fitsSystemWindows="true"
        tools:context="com.example.usuario.chatparritos.MainActivity">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/toolbar"
                android:background="?attr/colorPrimary"
                android:elevation="6dp"
                android:minHeight="?attr/actionBarSize"
                app:tabMode="fixed"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
    
            <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/toolbar"
                tools:context="net.voidynullness.android.tabitytabs.TabLayoutActivity"
                                               android:background="#ffffff"/>
    
        </android.support.design.widget.AppBarLayout>
    
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_info"/>
    
    </android.support.design.widget.CoordinatorLayout>
    

    -----//EDIT 3/02/16-----

    Here is my tab_fragment_3.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:focusableInTouchMode="true"
                android:focusable="true"
                android:scrollbars="vertical"
                android:id="@id/scrollView">
    
        <LinearLayout android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical"
                      android:background="#FFFFFF"
                      android:layout_marginLeft="20dp"
                      android:layout_marginRight="20dp"
                      android:gravity="left">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/title_chatparritos"
                    android:textSize="20dp"
                    android:layout_marginTop="15dp"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/body_chatparritos"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                    android:textSize="15dp"
                    android:layout_marginTop="9dp"
                    android:background="@drawable/blue_cornered_textview"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/link_khan"
                    android:layout_marginTop="-19dp"
                    android:textAppearance="@style/TextAppearance.Design.Hint"
                    android:textSize="15dp"
                    android:linksClickable="true"
                    android:id="@+id/linkAcademy"
                    android:layout_gravity="center_horizontal"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/title_xor"
                    android:textSize="20dp"
                    android:layout_marginTop="25dp"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/body_xor"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                    android:textSize="15dp"
                    android:layout_marginTop="9dp"
                    android:background="@drawable/blue_cornered_textview"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/instructions_xor"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                    android:textSize="15dp"
                    android:layout_marginTop="15dp"
                    android:background="@drawable/yellow_cornered_textview"
                    android:textStyle="bold"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/title_rsa"
                    android:textSize="20dp"
                    android:layout_marginTop="25dp"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/body_rsa"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                    android:textSize="15dp"
                    android:layout_marginTop="9dp"
                    android:background="@drawable/blue_cornered_textview"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/link_rsa"
                    android:layout_marginTop="-19dp"
                    android:textAppearance="@style/TextAppearance.Design.Hint"
                    android:textSize="15dp"
                    android:linksClickable="true"
                    android:id="@+id/linkRSA"
                    android:layout_gravity="center_horizontal"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/instructions_rsa"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                    android:textSize="15dp"
                    android:layout_marginTop="15dp"
                    android:background="@drawable/yellow_cornered_textview"
                    android:textStyle="bold"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/title_numeros"
                    android:textSize="20dp"
                    android:layout_marginTop="25dp"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/body_numeros"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                    android:textSize="15dp"
                    android:layout_marginTop="9dp"
                    android:background="@drawable/blue_cornered_textview"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/nota"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                    android:textSize="15dp"
                    android:layout_marginTop="30dp"
                    android:layout_marginBottom="30dp"
                    android:background="@drawable/red_cornered_textview"/>
    
        </LinearLayout>
    </ScrollView>
    

    Here is my TabFragment3.java

    public class TabFragment3 extends Fragment {
        TextView linkAcademy;
        TextView linkRsa;
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.tab_fragment_3, container, false);
    
    
            linkAcademy = (TextView) view.findViewById(R.id.linkAcademy);
            linkAcademy.setMovementMethod(LinkMovementMethod.getInstance());
    
            linkRsa = (TextView) view.findViewById(R.id.linkRSA);
            linkRsa.setMovementMethod(LinkMovementMethod.getInstance());
    
    
            return view;
        }
    

    My PagerAdapter.java

    public class PagerAdapter extends FragmentStatePagerAdapter {
    
        int mNumOfTabs;
    
        public PagerAdapter(FragmentManager fm, int numOfTabs) {
            super(fm);
            this.mNumOfTabs = numOfTabs;
        }
    
        @Override
        public Fragment getItem(int position) {
    
            switch (position) {
                case 0:
                    TabFragment1 tab1 = new TabFragment1();
                    return tab1;
                case 1:
                    TabFragment2 tab2 = new TabFragment2();
                    return tab2;
                case 2:
                    TabFragment3 tab3 = new TabFragment3();
                    return tab3;
                default:
                    return null;
            }
        }
    
        @Override
        public int getCount() {
            return mNumOfTabs;
        }
    

    And my MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
    
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
    
    
            TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
            tabLayout.addTab(tabLayout.newTab().setText("XOR"));
            tabLayout.addTab(tabLayout.newTab().setText("RSA"));
            tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_help));
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    
            final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
            final PagerAdapter adapter = new PagerAdapter
                    (getSupportFragmentManager(), tabLayout.getTabCount());
            viewPager.setAdapter(adapter);
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                                                   @Override
                                                   public void onTabSelected(TabLayout.Tab tab) {
                                                       viewPager.setCurrentItem(tab.getPosition());
                                                   }
    
                                                   @Override
                                                   public void onTabUnselected(TabLayout.Tab tab) {
    
                                                   }
    
                                                   @Override
                                                   public void onTabReselected(TabLayout.Tab tab) {
    
                                                   }
                                               });
    
    
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, R.string.fab_info, Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });