Blast
Blast

Reputation: 955

ScrollView not working with fragment

I am trying to create a dynamic fragment. I am adding inside of it 30 buttons and I want it to be scrollable.

Here is my xml code for Fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_orange_light"
    tools:context="com.viewpagerexample.app.FragmentA">

            <ScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/scrollView">

                <TableLayout
                    android:id="@+id/table_layout"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:baselineAligned="false"
                    android:clickable="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false">
                </TableLayout>
            </ScrollView>
</LinearLayout>

And this is code for my fragment:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_a,container,false);
        // Inflate the layout for this fragment
        int numberOfButtons= getArguments().getInt("someInt",0);
        linearLayout = new LinearLayout(v.getContext());
        view = new TableLayout(v.getContext());
        TableRow tr;
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        // Inflate the layout for this fragment
        view.setOrientation(TableLayout.VERTICAL);
        view.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams
                .MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
        TableLayout.LayoutParams layoutParams =  new TableLayout.LayoutParams
                (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(20,20,20,20);
        int numberOfRows = numberOfButtons/2;
        int masaCounter = 0;
        for (int i = 0;i<numberOfRows;i++)
        {

            tr = new TableRow(view.getContext());
            tr.setLayoutParams(layoutParams);
            for (int j= 0;j<2;j++)
            {
                masaCounter++;
                btn = new Button(getActivity());
                btn.setBackgroundResource(R.drawable.buttonstyle);
                btn.setHeight(200);
                btn.setText("Masa" + masaCounter);
                btn.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams
                        .MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1));
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                tr.addView(btn);
            }
            view.addView(tr);
        }
        linearLayout.addView(view);
        myView = linearLayout;
        return myView;
    }

I adds buttons but I can't see all of them. Because scrollview is not working. I could not find what i am doing wrong. How can I make it to work?

Upvotes: 0

Views: 3777

Answers (2)

Pankaj Arora
Pankaj Arora

Reputation: 10274

Change the height,width of parent layout and ScrollView to match_parent and add property of

 android:fillViewport="true"

in ScrollView and change the height,width of table layout to wrap_content and fill_parent respectivity will solve your problem.

Upvotes: 2

joao2fast4u
joao2fast4u

Reputation: 6892

Try having the ScrollView as rootView on the layout and put LinearLayout and all the other Views inside it.

Like this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_light"
tools:context="com.viewpagerexample.app.FragmentA" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TableLayout
        android:id="@+id/table_layout"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:baselineAligned="false"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false" >
    </TableLayout>
</LinearLayout>

Upvotes: 0

Related Questions