Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imageView is not stretching to fit the screen

Tags:

android

I have a drawable, which needs to work as the background. It needs to stretch to fill the screen (ignoring the aspect ratio).

I have provided appropriate sizes for ldpi,mdpi,hdpi and xhdpi. (I know they are appropriate by the ratio of the corresponding widths and heights, is that enough?)

I have tried to do this as follows:

using scaleType:fitXY on the imageView

This does leaves a white margin around (inside?) the imageView.

fitXY image

A work around that I'm not satisfied with is using centre for the scaleType like this:

I'm not satisfied because the problem still exists on tablet screens. enter image description here

Note:

  • As you might have noticed, I have tried the suggested fixes elsewhere on SO like setting Adjust View Boundaries to true. They didn't help here. :(
  • I would like a non-programmatic answer/fix, as in doing this in XML itself. I wouldn't really want to be writing code to set a background image; Life should be much simpler than that. (:P)

Update 1:

Here's the XML Layout

<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" -->
    <!-- xmlns:tools="http://schemas.android.com/tools" -->
    <!-- android:id="@+id/FrameLayout1" -->
    <!-- android:layout_width="fill_parent" -->
    <!-- android:layout_height="fill_parent" -->
    <!-- tools:context=".SplashScreen" > -->


    <!--
         This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows
    -->

    <View
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:onClick="onClickTapToContinue"
         >
    </View>

    <ImageView
        android:id="@+id/bg_gradient"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="fill"
        android:adjustViewBounds="true"
        android:contentDescription="@string/this_is_the_background_image"
        android:scaleType="center"
        android:src="@drawable/bg_gradient" />

    <ImageView
        android:id="@+id/bg_artwork"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:contentDescription="@string/artwork_man"
        android:src="@drawable/bg_artwork" />

    <!-- </FrameLayout> -->

    <ImageView
        android:id="@+id/branding"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|top"
        android:adjustViewBounds="true"
        android:contentDescription="@string/branding_text"
        android:maxHeight="95dp"
        android:paddingTop="5dp"
        android:src="@drawable/title_wlcm" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:paddingBottom="5dp"
        android:text="@string/tap_continue"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/white" />

</merge>
like image 887
Dheeraj Bhaskar Avatar asked Oct 21 '22 20:10

Dheeraj Bhaskar


2 Answers

Instead of using android:src="...", use android:background="..." attribute. Hope it works.

like image 101
Shivanand Darur Avatar answered Nov 01 '22 17:11

Shivanand Darur


The hack I'm going with for now is using these set of images as is for phones and maybe another set of images(and layouts?) for WVGA(Tablets).

Haven't tried this yet though. I'll post with screenshots as soon as I do.

Edit: Figures it had to do with the images themselves. There was transparent padding. Will ask designer to fix it. :)

like image 24
Dheeraj Bhaskar Avatar answered Nov 01 '22 18:11

Dheeraj Bhaskar