How can I get an ImageView to fill parent on a relative layout?

523 views Asked by At

I am attempting to stretch a 300x300 image to fill the parent as a background for a sample layout. However, it seems that at most it can reach it's max dimensions as a perfect square. Here is my code.

<ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/app_background"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"/>

There is still white space at both the top and bottom of the app and I want to eliminate that. Any and all solutions to this dilemma would be greatly appreciated.enter image description here

4

There are 4 answers

2
Pedro Massango On

Use match_parent

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/app_background"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"/>
0
Jeffrey Blattman On

Worked for me:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="org.jtb.stretchtest.MainActivity">

  <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter"
    android:src="@drawable/test"/>
</RelativeLayout>

enter image description here

0
Nomeh Uchenna Gabriel On

Check if the parent element has paddings.

Set Android padding of the parent widget to 0dp Eg: android:padding = "0dp"

0
ruben On

This works for me:

<ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/app_background"/>