1

I have a simple layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:padding="15dp">


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/scrollLayout">

        </LinearLayout>

    </ScrollView>
</RelativeLayout>

Now, I inflate the outer RelativeLayout to retrieve the inner LinearLayout to put items in it.

RelativeLayout relative =  (RelativeLayout) LayoutInflater.from(activity).inflate(R.layout.gradient_pick_view, null);
LinearLayout view = (LinearLayout) relative.findViewById(R.id.scrollLayout);

After that I created a method to add some buttons to it:

for(int i=0;i<10;i++){
     LinearLayout wrapper = (LinearLayout) LayoutInflater.from(activity).inflate(R.layout.button_wrapper, null);
     Button button = (Button)wrapper .findViewById(R.id.button);
     view.addView(layout);
}

Everything works fine, but it doesn't scroll. What am I doing wrong here?

Here's the screenshot (displaying 7 of 10 buttons): enter image description here

I forgot to mention - I'm using a MaterialDialog library and add this RelativeLayout as a custom view to a dialog.

4
  • Are the buttons being added ? Commented Mar 3, 2017 at 11:51
  • yep! I add 10 of them, 7 are displayed Commented Mar 3, 2017 at 11:52
  • remove the linear Layout.. add the button in Scroll View.Let me know if it works or not Commented Mar 3, 2017 at 11:53
  • @Spectre, what's the purpose of parent RelativeLayout? You mat remove it and ScrollView would work as expected. Commented Mar 3, 2017 at 12:12

2 Answers 2

1

Try to set the following attribute to your scrollview,

android:fillViewport="true"

above attribute is used to make your scrollview to use entire screen of your application.

Sign up to request clarification or add additional context in comments.

2 Comments

and If I don't want to fill the entire screen?
what entire screen here means is the height and width that you have provided it will fill that area fully.
0

I had a false parameter passed to a customView in a MaterialDialog.

dialog = new MaterialDialog.Builder(activity)
                    .title(R.string.about)
                    .customView(view, true)
                    .positiveText(R.string.changing_fragments)
                    .show();

As doc says:

If wrapInScrollView is true, then the library will place your custom view inside of a ScrollView for you. This allows users to scroll your custom view if necessary (small screens, long content, etc.). However, there are cases when you don't want that behavior. This mostly consists of cases when you'd have a ScrollView in your custom layout, including ListViews, RecyclerViews, WebViews, GridViews, etc. The sample project contains examples of using both true and false for this parameter.

Now it's working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.