I use a button with an image as its background, this image has a size of 30×29, but it’s resized and enlarged x2 (I think). Here is the XML code of my button :
<Button
android:id="@+id/buttonBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:background="@drawable/bouton_back"
android:maxHeight="30dp"
android:maxWidth="30dp" />
And in Java :
backButton = (Button) findViewById(R.id.buttonBack);
What is wrong with that ? I do the same with other buttons and there is no problem… thank you.
Are you supplying
bouton_backin your resources using multiple densities? Usingdpunits to size your button isn’t sufficient; you must also provide multiple sizes of your images:The conversion formula is as follows:
Where
pxis the final image size in pixels,dpis the desired size in density-independent units, anddpiis the target density.Simplifying this formula, using the size in pixels of your
mdpiimages as the baseline:Going back to your sample code, if you want a button that is 30dp by 30dp, you should be providing images for each density: