How to make ImageButton in Android automatically scale based on screen density?

android, dpi, imagebutton

Solution

As ponkin mentioned, 9-patches are your best bet. Combined with resource directory qualifiers (`drawable-hdpi`, `drawable-mdpi`, etc.) you can make assets that look good at any density and size.

An example of 9-patches and resource directory qualifiers can be found in the Multiple Resolutions Android SDK sample. The semi-transparent, black, rounded-corner background is implemented as a 9-patch with different PNGs for ldpi, mdpi, and hdpi.

Problem

I have an image put on an `ImageButton`, which looks good on a mdpi (medium-dpi) screen. However, when the device has a hdpi (high-dpi) screen, the image is still pixel-by-pixel accurate, which look small on the device screen. How do I make such that the image (and therefore the `ImageButton`) scaled based on the density-independent pixels (dp or dip) instead of pixels?

Original source