Android Imageview fill the screen width

23,923

Solution 1

Thank you to all but none of your answers solve my problem. Finally I used Maurycy Wojtowicz's ScaleImageView class.

This view will auto determine the width or height by determining if the height or width is set and scale the other dimension depending on the images dimension

Solution 2

Sounds to me like you want to use android:scaleType="fitCenter" and android:adjustViewBounds="true" on your ImageView.

Solution 3

use android:scaleType="fitXY".

This will scale the image in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.

Share:
23,923

Related videos on Youtube

tsil
Author by

tsil

Updated on July 23, 2022

Comments

  • tsil
    tsil almost 2 years

    Here my layout file:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#000"
    tools:context=".CproductDetails" >
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/shape_bg"
            >
    
            <ImageView
                android:id="@+id/product_image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_tweet_placeholder_photo_dark_error"
                android:scaleType="center" />
    
            <TextView
                android:id="@+id/product_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textIsSelectable="true"
                android:textColor="#95ab56"
                android:textSize="20sp"
                android:layout_marginTop="7dip" />
    
            <TextView
                android:id="@+id/product_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textIsSelectable="true"
                android:textSize="20sp"
                android:textColor="#efeeea" />
    
            <TextView
                android:id="@+id/product_commerce"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textIsSelectable="true"
                android:textSize="20sp" />
    
            <TextView
                android:id="@+id/product_city"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textIsSelectable="true"
                android:textSize="15sp"
                android:layout_marginTop="10dip"
                android:layout_marginBottom="10dip" />
    
            <TextView
                android:id="@+id/product_township"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textIsSelectable="true"
                android:textSize="15sp"
                android:visibility="gone"
                android:layout_marginBottom="10dip"
                android:drawableLeft="@drawable/location_place"
                android:drawablePadding="7dip" />
    
            <TextView
                android:id="@+id/product_website"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textIsSelectable="true"
                android:autoLink="web"
                android:textSize="15sp"
                android:drawableLeft="@drawable/location_web_site"
                android:drawablePadding="7dip"
                android:visibility="gone" />
    
             <TextView
                android:id="@+id/product_tel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textIsSelectable="true"
                android:textSize="20sp"
                android:drawableLeft="@drawable/device_access_call"
                android:drawablePadding="7dip"
                android:visibility="gone" />
    
        </LinearLayout>
    

    The result in a phone:

    enter image description here

    and the result in a 7in tablet:

    enter image description here

    I want to make the image fill the screen width also in tablet. Like Twitter app for example:

    enter image description here

    enter image description here

    • MarsAtomic
      MarsAtomic about 11 years
      Your answer is going to depend on your layout, so why don't you post the entire XML layout file?
    • Raghunandan
      Raghunandan about 11 years
      android:layout_height="fill_parent"
    • CRUSADER
      CRUSADER about 11 years
      android:layout_width='fill_parent' will certainly solve your issue, but i think that will distort or rather stretch your image which will not look good. I think the output you are getting now looks pretty OK to me.
  • lilbyrdie
    lilbyrdie almost 9 years
    which is relatively useless for most photos, since aspect ratio is critical
  • Vikash Kumar
    Vikash Kumar almost 8 years
    fitXY will take the complete x axis and y axis as our described layout height
  • Abandoned Cart
    Abandoned Cart about 7 years
    The class manually adds centerInside or centerCrop scaling to the image. It is a bloated version of many better answers on both this and the 10 duplicates of this question that do not require an extraneous class.