Tim Tim - 2 months ago 8
Android Question

How to set my scrollView layout height to the top of a fragment

I have an app where the scrolView (relative layout inside it) take up the whole screen (fill_parent). But instead of ending at the end of the phone i would like to have a fragment at the bottom and have the ScrollView layout to stop at the top of the fragment. I think it probably has to do with the height but can't really figure out the code. Please look at my imgur image to get my idea. Imgur_Image, press here



<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"><![CDATA[


tools:context="c.timno.smsgsm.MainActivity">



]]>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="status?"
android:id="@+id/skicka"
android:hint="Status?"

android:layout_marginTop="38dp"
android:layout_below="@+id/buttonanvandare"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="på"
android:id="@+id/skickaaon"
android:layout_marginEnd="36dp"
android:layout_below="@+id/textViewlarm"
android:layout_alignEnd="@+id/skicka" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Av"
android:id="@+id/skickaaoff"

android:layout_below="@+id/textViewlarm"
android:layout_toEndOf="@+id/skickarela1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relä 1"
android:id="@+id/skickarela1"
android:layout_alignTop="@+id/skickarela2"
android:layout_alignStart="@+id/skickaaon" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relä 2"
android:id="@+id/skickarela2"
android:layout_below="@+id/textView"
android:layout_toEndOf="@+id/skickarela1" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aktivera"
android:gravity="center"
android:textSize="29dp"
android:id="@+id/textView"
android:layout_below="@+id/skickaaon"
android:layout_alignParentStart="true" />


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"

android:text="Larm"
android:textSize="29dp"
android:id="@+id/textViewlarm"
android:layout_below="@+id/skicka"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Avaktivera"
android:textSize="29dp"
android:id="@+id/textView3"
android:autoText="false"
android:layout_below="@+id/textView"
android:layout_alignEnd="@+id/buttoninstallator"
android:layout_marginTop="46dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relä 1"
android:id="@+id/skickatrela1off"
android:layout_below="@+id/textView3"
android:layout_toStartOf="@+id/skickatrela2off" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relä 2"
android:id="@+id/skickatrela2off"
android:layout_alignTop="@+id/skickatrela1off"
android:layout_alignStart="@+id/skickarela2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Användare"
android:id="@+id/buttonanvandare"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/skickaaoff" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Installation"
android:id="@+id/buttoninstallator"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/buttonanvandare" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="status?"
android:id="@+id/statustxt"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="aon"
android:id="@+id/aon"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="aoff"
android:id="@+id/aoff"
android:layout_alignParentStart="true"
android:layout_below="@+id/statustxt" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="on*1*"
android:id="@+id/rela1txxt"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="On*2*"
android:id="@+id/rela2onn"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="off*2*"
android:id="@+id/rela2offtxt"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="off*1*"
android:id="@+id/offrela1txt"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Värmestyrning"
android:textSize="33dp"
android:id="@+id/textView8"
android:layout_below="@+id/skickatrela1off"
android:layout_marginTop="25dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Komfortvärme"
android:textSize="29dp"
android:id="@+id/textView9"
android:layout_below="@+id/textView8" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heaton"
android:id="@+id/btnHeaton"
android:layout_below="@+id/textView9"
android:layout_alignEnd="@+id/skickatrela1off" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heatoff"
android:id="@+id/btnHeatoff"
android:layout_below="@+id/textView9"
android:layout_alignStart="@+id/skickatrela2off" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Underhållsvärme"
android:textSize="29dp"
android:id="@+id/textView10"
android:layout_below="@+id/btnHeaton"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Termon"
android:id="@+id/btnTermon"
android:layout_below="@+id/textView10"
android:layout_toStartOf="@+id/btnHeatoff" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Termoff"
android:id="@+id/btnTermoff"
android:layout_alignBottom="@+id/btnTermon"
android:layout_toEndOf="@+id/btnTermon" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Heaton"
android:id="@+id/txtHeaton"
android:layout_alignTop="@+id/statustxt"
android:layout_alignEnd="@+id/statustxt" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Heatoff"
android:id="@+id/txtHeatoff"
android:layout_alignBottom="@+id/statustxt"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Termon"
android:id="@+id/txtTermon"
android:layout_alignBaseline="@+id/aoff"
android:layout_alignBottom="@+id/aoff"
android:layout_alignEnd="@+id/statustxt" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Termoff"
android:id="@+id/txtTermoff"
android:layout_alignTop="@+id/statustxt"
android:layout_alignEnd="@+id/txtHeatoff" />

<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="c.timno.smsgsm20.SecondFragment"
android:id="@+id/fragmentsms"
android:layout_below="@+id/textView8"
android:layout_alignParentStart="true"
tools:layout="@layout/second_layout"
android:layout_above="@+id/textView10" />


</RelativeLayout>
</ScrollView>




Answer

I would suggest putting the ScrollView and the Fragment inside a new top-level element and then using the respective method within that element to adjust the size and position of each.

For instance, using a RelativeLayout:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  ...>
   <ScrollView
     android:id="@+id/scrollview"
     android:layout_above="@+id/frag"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     ...>
       <RelativeLayout
         android:paddingBottom="50dp"
         ...>
          ...
       </RelativeLayout>
     ...
   </ScrollView>
   <Fragment
      android:id="@+id/frag"
      android:layout_alignParentBottom="true"
      android:layout_width="match_parent"
      android:layout_height="50dp"
      ...>
     ...
   </Fragment>
</RelativeLayout>

(Note that the padding on the bottom of the RelativeLayout inside the ScrollView matches the height of the Fragment so that contents of the ScrollView don't get hidden behind the Fragment)

You could also do this using LinearLayout as the parent element with an attribute of android:orientation="vertical", and the ScrollView and Fragment each getting an appropriate layout_weight attribute, but that's not as clean imho, so I'll leave it to you if you'd prefer to do that.

Comments