10

In one of the layouts,i am using scrollview and want to set its layoutMarginBottom programmatically.How can i do that?This is the xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/frameLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="90.0dip"
            android:layout_marginLeft="1.0dip"
            android:layout_marginRight="1.0dip"
            android:layout_marginTop="-2.0dip"
            android:animateLayoutChanges="true"
            android:fadeScrollbars="true"
            android:fadingEdge="none" >

            <!-- android:layout_marginBottom="60.0dip" -->
            <!-- android:fillViewport="true" -->

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:visibility="visible" >

                <ImageView
                    android:id="@+id/QuestionImageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="visible" />

                <TextView
                    android:id="@+id/tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/QuestionImageView"
                    android:fadingEdge="vertical"
                    android:scrollbars="vertical" />

                <TextView
                    android:id="@+id/questionnumber"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:fadingEdge="vertical"
                    android:scrollbars="vertical" />

                <TextView
                    android:id="@+id/question"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_below="@id/tv"
                    android:fadingEdge="vertical"
                    android:padding="10dp"
                    android:scrollbars="vertical"
                    android:text=""
                    android:textColor="@color/dark_green" />

                <RadioGroup
                    android:id="@+id/radioGroup1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/question"
                    android:orientation="vertical" >

                    <RadioButton
                        android:id="@+id/option1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" />

                    <ImageView
                        android:layout_width="fill_parent"
                        android:layout_height="1.0dp"
                        android:background="@drawable/thinnest_blue_gradient" />

                    <RadioButton
                        android:id="@+id/option2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" />

                    <ImageView
                        android:layout_width="fill_parent"
                        android:layout_height="1.0dp"
                        android:background="@drawable/thinnest_blue_gradient" />

                    <RadioButton
                        android:id="@+id/option3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" />

                    <ImageView
                        android:layout_width="fill_parent"
                        android:layout_height="1.0dp"
                        android:background="@drawable/thinnest_blue_gradient" />

                    <RadioButton
                        android:id="@+id/option4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" />
                </RadioGroup>
            </RelativeLayout>
        </ScrollView>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativequestion"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <com.google.ads.AdView
            android:id="@+id/ad"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="a1501e5633125fb"
            ads:loadAdOnCreate="true"
            ads:testDevices="TEST_EMULATOR, 123456789ABCDEF" />

        <LinearLayout
            android:id="@+id/newquestion"
            android:layout_width="fill_parent"
            android:layout_height="40.0dip"
            android:layout_above="@id/ad"
            android:background="#ff777777"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/PrevButton"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_marginLeft="5.0dip"
                android:layout_weight="1.0"
                android:background="@drawable/ibtn"
                android:onClick="onPrevButtonClick"
                android:text="Prev" />

            <Button
                android:id="@+id/NextButton"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_marginLeft="5.0dip"
                android:layout_weight="1.0"
                android:background="@drawable/ibtn"
                android:onClick="onNextButtonClick"
                android:text="Next" />

            <Button
                android:id="@+id/SkipButton"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_marginLeft="5.0dip"
                android:layout_weight="1.0"
                android:background="@drawable/ibtn"
                android:onClick="onSkipButtonClick"
                android:text="Skip" >
            </Button>

            <Button
                android:id="@+id/SubmitButton"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_marginLeft="5.0dip"
                android:layout_marginRight="5.0dp"
                android:layout_weight="1.0"
                android:background="@drawable/ibtn"
                android:onClick="onSubmitButtonClick"
                android:text="Submit" />
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>

Here i want to set the layoutMarginBottom of the scrollview.Please help me.Thanks in advance.

user1662334
  • 659
  • 2
  • 9
  • 21
  • Do you want to just set the layoutMarginBottom of scrollview through activity class? – Jivan Sep 23 '12 at 06:27
  • Yes sir,i want to do exactly that kind of stuff. – user1662334 Sep 23 '12 at 06:30
  • You may need to set margins through linearlayout adding into scrollview. – Jivan Sep 23 '12 at 06:57
  • 3
    you should edit this question instead of asking multiple same question... http://stackoverflow.com/questions/12550325/getting-exception-while-setting-layoutparams-of-scrollview-programmatically – nKandel Sep 23 '12 at 07:28

3 Answers3

24

I made it done in this way:

sv = (ScrollView)findViewById(R.id.scrollView);
sv.setBackgroundColor(Color.TRANSPARENT);

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) sv
    .getLayoutParams();

layoutParams.setMargins(0, 0, 0, 100);
sv.setLayoutParams(layoutParams);
Snowman
  • 31,411
  • 46
  • 180
  • 303
user1662334
  • 659
  • 2
  • 9
  • 21
3

In case if you are getting

java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

with above solution then try:

 ScrollView sv = (ScrollView)findViewById(R.id.scrollView);    
 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)sv.getLayoutParams();

 lp.bottomMargin = 100;
 sv.setLayoutParams(lp);
Amit
  • 3,422
  • 3
  • 28
  • 39
0

I got this error:

Blockquote java.lang.ClassCastException: androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams cannot be cast to ...

then as I want to change Top Margin for NestedScrollView I try the above solution with some modifications:

CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) nestedScrollView.getLayoutParams();

layoutParams.topMargin = 56;

nestedScrollView.setLayoutParams(layoutParams);