Search code examples
androidandroid-imageviewandroid-relativelayout

Imageview images responsiveness


I have 3 images in PNG format.They are in same resolution and i want them to be next to each other in a relative layout. I want it to be responsive for example the imageviews should resize them according to the space provided. I dont want to give a fixed height and width in dps like:

<ImageView
            android:layout_width="60dp"
            android:layout_height="100dp"/>

And if i try to define width and height like:

<ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

And if i do, they are way too big.What should i do? Any recommendations ?

EDIT: Here is the code:

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/streetlong">
        <ImageView
            android:layout_width="60dp"
            android:layout_height="100dp"
            android:id="@+id/btn_hospital"
            android:onClick="onClick"
            android:background="@drawable/hospital"
            />

        <ImageView
            android:id="@+id/btn_pharmacy"
            android:layout_width="60dp"
            android:layout_height="100dp"
            android:layout_marginLeft="@dimen/_15sdp"
            android:onClick="onClick"
            android:layout_toRightOf="@+id/btn_hospital"
            android:layout_toEndOf="@+id/btn_hospital"
            android:background="@drawable/pharmacys" />
        <ImageView
            android:id="@+id/btn_police"
            android:layout_width="60dp"
            android:layout_height="100dp"
            android:layout_marginLeft="@dimen/_15sdp"
            android:layout_toRightOf="@+id/btn_pharmacy"
            android:layout_toEndOf="@+id/btn_pharmacy"
            android:layout_weight="1"
            android:background="@drawable/polices"
            android:onClick="onClick" />
    </RelativeLayout>

I want the imageviews to adjust according to the screensize and i dont want to give them height and width


Solution

  • You can achieve this by using property android:layout_weight

      <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3>
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
    
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>
    
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </LinearLayout>
        </RelativeLayout>