Search code examples
javaandroidxmlandroid-layout

How to remove additional space between ImageView and edge of screen/parent in Android Studio?


so I am essentially trying to remove some extra space that seems to be above an imageview, that I am using as a banner for the top of a page on my app (shown in image below).

Image - Additonal Space above my ImageView

However, when I set this imageview to cover the entire screen, there is no space anymore. Would someone know how to fix this, as I have tried changing the ScaleType, etc.

Here is the .XML code for this ImageView, and i'm using a constraintLayout if that has any relevance:

<ImageView
        android:id="@+id/pagebanner"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:background="@drawable/bannerborder"
        android:contentDescription="@string/page_banner"
        app:layout_constraintBottom_toTopOf="@+id/guideline8"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Also, this is the code for the simple drawable (bannerborder) that's being used in the ImageView object:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="@color/colorPurple" />
</shape>
  • I have tried changing ScaleType to: android:scaleType="fitXY", along with all the other options.
  • Tried setting adjustViewBoundaries to both true and false
  • tried using negative layout margins

Solution

  • It seems that no margin is set for the imageview. Defining margin attribute like below would help:

    android:layout_marginTop="0dp"
    

    UPDATE

    Your imageview bottom constrained with guideline. If it is not mandatory to use guideline you could delete bottom constraint and just keep top constrait of imageview. And set imageview height with something like "app:layout_constraintHeight_percent="0.2"" instead of using "android:layout_height" attribute. This way height better compatible with different screen sizes. This could solve the problem. So your imageview attributes should look like this:

    <ImageView
        android:id="@+id/pagebanner"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_percent="0.2"
        android:layout_marginTop="0dp"
        android:background="@drawable/bannerborder"
        android:contentDescription="@string/page_banner"        
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />