1

I have a scrolled content, so I put a constraint layout inside a scrollview, I have a problem when put an image inside a constraint layout, but the image show a space from top such as in screenshot below, how can I remove that space?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

    <android.support.constraint.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <!-- have whatever children you want inside -->
        <ImageView
                app:srcCompat="@drawable/main_header"
                android:id="@+id/img_icon"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="#ffccbb"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintWidth_percent="1"
                android:contentDescription="TODO" app:layout_constraintDimensionRatio="1:1"
                app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="0.0"/>
    </android.support.constraint.ConstraintLayout>
</ScrollView>

screenshot

Ali A. Jalil
  • 873
  • 11
  • 25

3 Answers3

0

In your Imageview you have added app:layout_constraintDimensionRatio="1:1" that means it will take space 1:1, so width and height also will be 1:1. But your image app:srcCompat="@drawable/main_header" is not 1:1. So you have to add an image which has width and height of the same size. Otherwise, you can add android:scaleType="fitXY" or android:scaleType="centerCrop" according to your expectation.

Koushik Mondal
  • 865
  • 7
  • 15
  • What must I change in my code to get my image at top with it's parent and fill width? – Ali A. Jalil May 26 '19 at 17:33
  • Add just android:scaleType="fitXY" and image will adjust with your image view width and height. – Koushik Mondal May 27 '19 at 06:38
  • That is my Image, https://drive.google.com/file/d/1OScROggogF0h_O3f4Evs9OWQ7zhukx9J/view , I want to be 25% of screen height, and fill width, I do not want to scaled. – Ali A. Jalil May 27 '19 at 08:40
  • You have to scale the image or have to change the ratio of width and height. If you don't want to scale, then change as app:layout_constraintDimensionRatio="16:8". It will work. – Koushik Mondal May 27 '19 at 10:13
0

I just checked your code on my phone and it looks good. One thing that I made different from you is that I have used a different image resource for the imageView.

So I think that you don't actually have a problem, I think that it's just the image and how it looks, try to change your image resource and see if this problem remains.

He is how my layout file looks like:

<ScrollView 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">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/main_header"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

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

Try this, remove app:layout_constraintTop_toTopOf="parent" in your ImageView it works!

ismail alaoui
  • 5,748
  • 2
  • 21
  • 38