user3293120
user3293120

Reputation: 63

How to set the size of an imageView

I have 2 imageview in my layout that i define like this

<ImageView
    android:id="@+id/image1"
    android:layout_width="320dp"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/image1"
    android:contentDescription="@string/image1" />

<ImageView
    android:id="@+id/image2"
    android:layout_width="320dp"
    android:layout_height="220dp"
    android:layout_alignLeft="@+id/image1"
    android:layout_alignParentBottom="true"
    android:src="@drawable/image2"
    android:contentDescription="@string/image2" />

On the emulater it works fine, but when i test the application on a tablet the image ramain 320x220. How can i size the image?

Upvotes: 0

Views: 1154

Answers (3)

SearchForKnowledge
SearchForKnowledge

Reputation: 3751

To add to Devunwired answer, you also have to add res/values-sw720dp/dimens.xml for 10" devices. So you should have three:

res/values/dimens.xml (standard screen)

res/values-sw600dp/dimens.xml (7" devices)

res/values-sw720dp/dimens.xml (10" devices)

Upvotes: 0

devunwired
devunwired

Reputation: 63293

The reason is because many tablets have the same density as phones (mdpi, xhdpi), so the dp unit resolves to the same number of pixels. Set your image size to a dimens resource that you can control for different minimum screen sizes. In other words:

res/layout/your_layout.xml

<ImageView
    android:id="@+id/image1"
    android:layout_width="@dimen/image_width"
    android:layout_height="@dimen/image_height"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/image1"
    android:contentDescription="@string/image1" />

res/values/dimens.xml

<resources>
    <dimen name="image_width">320dp</dimen>
    <dimen name="image_height">200dp</dimen>
</resources>

res/values-sw600dp/dimens.xml

<resources>
    <dimen name="image_width">640dp</dimen>
    <dimen name="image_height">400dp</dimen>
</resources>

The larger values will be used on screens with a smallest-width of 600dp or greater (the average 7" tablet) and the smaller values used everywhere else as the default. If you want to add more discrete changes, you can create additional dimens to override the default values in specific use cases.

Upvotes: 3

cbrulak
cbrulak

Reputation: 15629

You mean you want a different size for different devices? like 320x200 for phones but something like 640x400 for tablets?

You'll need to have different layout files.

or you can changing the size/layout/whatever programmatically: https://stackoverflow.com/a/15171766/29505

Upvotes: 0

Related Questions