user2132383
user2132383

Reputation: 227

How to fit imageView inside LinearLayout in android, but always keep its aspect ration

I am trying to make an ecommerce app, the biggest problem i face is fitting the images when i get it from an api call. The images can be in any size, so i need to make sure they fit perfectly in all screen sizes.

This is my cart xml listview item layout:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/primary">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="0.2">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/temp_image_product_detail"
        android:id="@+id/layout_sort_by_listview_icon"/>

</LinearLayout>


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_weight="0.8">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="adkfj;aklsjdf"
        android:textSize="25sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rs. 255"
        android:textSize="25sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4.2"
        android:textSize="15sp"/>

</LinearLayout>

I want to achieve the perfect fitting look for the cart page and the product listing page. Something like this shown in:

http://traverphillips.com/Amazon-Material-Design

Upvotes: 0

Views: 2115

Answers (4)

user1953449
user1953449

Reputation:

I hope it will help i didn't tested it though :

your first LinearLayout should look like this

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/primary">

<LinearLayout
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:gravity="center"
    android:layout_weight="0.2">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/temp_image_product_detail"
    android:id="@+id/layout_sort_by_listview_icon"/>

Upvotes: 1

y7876768
y7876768

Reputation: 333

Try layout_width="match_parent" and use layout_weight

Upvotes: 0

Anand Savjani
Anand Savjani

Reputation: 2535

Please use android:scaleType="fitXY" for fit in screen

Upvotes: 0

sadegh saati
sadegh saati

Reputation: 1170

if you want the image to be fit horizontaly you can set layout_width to fill_parent and layout_height to wrap_content but you must set adjustViewBount "true"

Upvotes: 0

Related Questions