1

I have a screen that contains some TextViews and an ImageView inside a LinearLayout with vertical orientation. I would like the whole thing to fit exactly in the screen (no matter what its size is) where the ImageView is the one that adjusts itself to accommodate this.

I've seen here a few variations of this question (including this) but didn't find anything that really answers my requirement.

So far i've used a solution which is not very "pretty", which is putting the entire LinearLayout inside a ScrollView, and use a custom ImageView that on its onMeasure method calculates the delta between the height of the ScrollView to the height of the screen. If the former is larger than the latter then the delta is subtracted from the ImageView's measured height.

This solution is not perfect and i would really like to know if there is a better one. Im sure there is.

Appreciate your help.

EDIT: here is the layout xml

<com.xxx.widgets.LockableScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.venews"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    custom:scrollable="false"
    android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/login_horizontal_margin"
    android:paddingRight="@dimen/login_horizontal_margin"
    android:orientation="vertical">

    <com.xxx.widgets.ResizableToFitScreenImageView android:id="@+id/login_logo_image"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/login_logo_margin"
        android:layout_marginBottom="@dimen/login_logo_margin"
        android:src="@drawable/ic_logo_and_name"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView android:id="@+id/login_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"/>

        <TextView android:id="@+id/login_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/login_username"/>

        (...Other stuff...)

    </RelativeLayout>

</LinearLayout>

</com.xxx.widgets.LockableScrollView>

EDIT2: and here's a sketch that i hope will make things clearer. enter image description here

The sketch is showing 2 different screen sizes cause this solution would need to support also different devices with different screen sizes.

Community
  • 1
  • 1
AsafK
  • 2,425
  • 3
  • 32
  • 38

1 Answers1

1

On the ImageView, set android:layout_height="0dp" and android:layout_weight="1". This will cause it to fill the remaining space (more about layout_weight here).

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
Community
  • 1
  • 1
mpkuth
  • 6,994
  • 2
  • 27
  • 44
  • Well that is just plain embarrassing... sometimes im just overcomplicating stuff. Thanks a lot for the ridiculously simple answer. – AsafK Dec 15 '14 at 21:18