androidandroid-layoutandroid-relativelayoutandroid-tablelayoutandroid-scrollview

Trying to set LayoutWidth Programmatically


I am trying to set layout width pro-grammatically, Here is my XML layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/startTime_EndtimeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >



    <TextView
        android:id="@+id/HeadingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="TextView" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/HeadingTextView"
        android:layout_above="@+id/dialogButtonOK"
        android:scrollbars="vertical" >

        <RelativeLayout
            android:id="@+id/startTime_EndtimeLayoutWithScroll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TableLayout
                android:id="@+id/startTime_EndtimeTableLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableLayout>
        </RelativeLayout>
    </ScrollView>

    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text=" close " />

</RelativeLayout>

I want to set the relative layout width, the child view of Scroll View. The ways I have tried,

1.

RelativeLayout rl = (RelativeLayout)findViewById(R.id.startTime_EndtimeLayoutWithScroll);
rl.setLayoutParams(new 

LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

2.

RelativeLayout rl = (RelativeLayout)scrollView.findViewById(R.id.startTime_EndtimeLayoutWithScroll);
    rl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

In both the cases,I am getting this error,

10-11 17:36:31.526: E/dalvikvm(1555): [DVM] mmap return base = 4586a000
10-11 17:36:32.824: E/dalvikvm(1555): [DVM] mmap return base = 459e1000
10-11 17:36:32.966: E/AndroidRuntime(1555): FATAL EXCEPTION: main
10-11 17:36:32.966: E/AndroidRuntime(1555): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.widget.FrameLayout.onLayout(FrameLayout.java:324)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.widget.ScrollView.onLayout(ScrollView.java:1331)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.view.View.layout(View.java:7225)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:943)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.view.View.layout(View.java:7225)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.view.View.layout(View.java:7225)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.view.View.layout(View.java:7225)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1181)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1913)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.os.Handler.dispatchMessage(Handler.java:130)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.os.Looper.loop(SourceFile:351)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at android.app.ActivityThread.main(ActivityThread.java:3820)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at java.lang.reflect.Method.invokeNative(Native Method)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at java.lang.reflect.Method.invoke(Method.java:538)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
10-11 17:36:32.966: E/AndroidRuntime(1555):     at dalvik.system.NativeStart.main(Native Method)

Any ideas to resolve the problem. Thanx


Solution

  • I resolved the problem.I need to put ScrollView Instead of Relative layout.

    Because the relative layout i want to set; is the child of Scroll View in my XML.

    ScrollView.LayoutParams params = new ScrollView.LayoutParams(width,LayoutParams.WRAP_CONTENT);
            rl.setLayoutParams(params);