0
<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:fillViewport="true"
    android:layout_weight="19" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/layoutScrollContainer"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@drawable/card_background" >

           <
            .
            .
            .
            .
            .
            .
            />
        </LinearLayout>


    </LinearLayout>
</HorizontalScrollView>

I want to get the width of the HorizontalScrollView1 and set it to the LinearLayout (layoutScrollContainer)

This is my code in java but my app crashes , and I cant understand why . If you know how to do this with xml please tell me.

HorizontalScrollView scrollView = (HorizontalScrollView) findViewById (R.id.horizontalScrollView1);
    LinearLayout lrLayout = (LinearLayout ) findViewById (R.id.layoutScrollContainer);

    int  width = scrollView.getMeasuredWidth();
    lrLayout.setLayoutParams(new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT));
user3199577
  • 249
  • 2
  • 5
  • 14

3 Answers3

0

So... you haven't defined any ID for the Linear Layour. Check this:

 LinearLayout lrLayout = (LinearLayout ) findViewById (R.id.llayoutContainer1);

Just set the id for the linear layout :D

Ignasi Sánchez
  • 75
  • 1
  • 2
  • 10
0

it crashes because the LayoutParams refers always to the Parent. In your case you are getting a ClassCastException because you are using the LinearLayout.LayoutParams

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

I know this is old, but I came across it and have the answer.

It crashes probably because you doing this in onCreate - before the elements have been drawn or measured.

The best way to handle this is to use the ViewTreeObserver and after the layout is drawn, you can reset the width:

How can you tell when a layout has been drawn?

in particular, scrollView.getMeasuredWidth() won't work until the view is measured, after onCreate

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36