2

How do I make the background image fill the screen? I have tried several different suggestions and nothing seems to work.

enter image description here

Below is the code for my activity_main.xml:

<?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="com.andrewvanpeter.upandaway.MainActivity">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/background_flat_720x1280" />

</android.support.constraint.ConstraintLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

1

First solution :

You can put the layout_width and the layout_height of your ImageView to match_parent :

<ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/background_flat_720x1280" />

Second solution :

You can set the background directly in the ConstraintLayout (and don't forget to delete the ImageView) :

<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"
    android:background="@drawable/background_flat_720x1280"
    tools:context="com.andrewvanpeter.upandaway.MainActivity">
Jéwôm'
  • 3,753
  • 5
  • 40
  • 73
  • 1
    The match_parent solution did not work; maybe I'm doing something else wrong. However, setting the background directly did work. Thank you very much! – CheetahBongos Dec 02 '17 at 20:36
  • No problem, it's a pleasure! Maybe you have a padding or margin somewhere... Or you have to fit the ImageView. – Jéwôm' Dec 02 '17 at 20:38
1

You can also to set the scaletype of imageview to fitXY like this :

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    app:srcCompat="@drawable/background_flat_720x1280" />
ROHIT LIEN
  • 497
  • 3
  • 8