Farid Ala
Farid Ala

Reputation: 668

Changing scrollview's position in Android

I'm writing an Android application that has a scrollview which contains a linearlayout:

<ScrollView android:id="@+id/ScrollView01"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"  
        android:layout_gravity=top"
        android:layout_marginTop="240dip"
        android:layout_marginRight="0dip"
        android:layout_marginBottom="60dip">

         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/centerlayout"
        android:orientation="vertical"
        android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:layout_gravity="top"
                android:layout_marginRight="20dip"
                android:layout_marginLeft="10dip"
            android:layout_marginBottom="120dip">

As you see, scrollview's margin top is "240dip" . I want to be able to change it in my Java code. I tried this:

ScrollView scrollView=(ScrollView)findViewById(R.id.ScrollView01);
LayoutParams  params=scrollView.getLayoutParams();

But I can't do this: params.top=100; Would you please help me how I can do it?

Upvotes: 2

Views: 2115

Answers (2)

user2060883
user2060883

Reputation: 121

Try

params.gravity = Gravity.LEFT | Gravity.TOP;

without the line you will have problems.

Upvotes: 1

Reed
Reed

Reputation: 14984

What do you mean that you "can't do this". You would have to do params.top=100; and then scrollView.setLayoutParams(params);.

But you could change it to:

ScrollView scrollView=(ScrollView)findViewById(R.id.ScrollView01);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(scrollView.getLayoutParams());
params.topMargin = 100;
scrollView.setLayoutParams(params);

Upvotes: 4

Related Questions