Ali A. Jalil
Ali A. Jalil

Reputation: 921

Image have margin from top in constraintlayout

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

Upvotes: 1

Views: 1386

Answers (3)

Tamir Abutbul
Tamir Abutbul

Reputation: 7661

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>

Upvotes: 0

ismail alaoui
ismail alaoui

Reputation: 6063

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

Upvotes: 0

Koushik Mondal
Koushik Mondal

Reputation: 875

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.

Upvotes: 0

Related Questions