1

I faced with the following problem: ScrollView in my activity should be placed below ToolBar. Here's the layout of this activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SongLyricsActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0000FF"
        tools:ignore="MissingConstraints" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp">

        <TextView
            android:id="@+id/lyrics_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </ScrollView>



</android.support.constraint.ConstraintLayout>

But when I run the app, I see this:

enter image description here

I don't understand, why it happens, as I position it below the toolbar, so, what's the matter?

Sergei Mikhailovskii
  • 2,100
  • 2
  • 21
  • 43

3 Answers3

3

I solved this problem using a marginTop parameter (equals to toolbar height), like this:

 <ScrollView
    android:id="@+id/activity_show_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="?attr/actionBarSize"
    android:background="@color/transparent_background"
    >

This works for my app.

2

This is happening because your scroll views height is match_parent and not 0dp - so your scroll view will not respect your constraints and will spread all over your screen.

Please notice that you are using tools:ignore="MissingConstraints" and the tool attribute will only affect the preview so you will see your layout in a different way from the preview.

In addition, you were missing some constraint - app:layout_constraintTop_toTopOf="parent"

Now with that constraint, it should work:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#0000FF"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="0dp" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/toolbar"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginTop="16dp">

    <TextView
        android:id="@+id/lyrics_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
0

you might try:

 <LinearLayout>
      vertical
      <RelativeLayout>
           <Toolbar>
               someID
               parent top
               parent left
           <Scrollview>
               below someID
               parent left
Will Thomson
  • 875
  • 6
  • 19