There is a png picture and the goal is to add it to the button . The problem is that if I add via @android:background buttons, then the picture becomes with the ladders (as if anti-aliasing is turned off), or becomes muddy. If I use the markup below, then in general the picture is not shown, only the borders on the button remain.

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:bottom="2dp" android:drawable="@drawable/lmb_leaved" android:left="2dp" android:right="2dp" android:top="2dp" /> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="30dp" /> <gradient android:angle="270" android:endColor="#000000" android:startColor="#000000" /> <stroke android:width="4px" android:color="#FF00BB2B" /> </shape> </item> </layer-list> </item> <item android:state_pressed="true"> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:bottom="2dp" android:drawable="@drawable/lmb_pressed" android:left="2dp" android:right="2dp" android:top="2dp" /> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="30dp" /> <gradient android:endColor="#000000" android:startColor="#000000" /> <stroke android:width="20px" android:color="#FF00BB2B" /> </shape> </item> </layer-list> </item> 

There are assumptions that incorrectly add png to the project. I add through res-> new-> image asset

Corrected, now everything works

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <bitmap android:src="@drawable/lkmveryhigh" /> </item> <item> <shape android:shape="rectangle"> <corners android:radius="30dp" /> <solid android:color="#0000"/> <stroke android:width="5dip" android:color="#FF00BB2B" /> </shape> </item> </layer-list> </item> <item android:state_pressed="true"> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <bitmap android:src="@drawable/lkmveryhigh" /> </item> <item> <shape android:shape="rectangle"> <corners android:radius="30dp" /> <solid android:color="#0000"/> <stroke android:width="10dip" android:color="#FF00BB2B" /> </shape> </item> </layer-list> </item> </selector> 

I move the picture manually to the res / drawable-hdpi folder. Only there is a small problem that the android:state_pressed="true" selector android:state_pressed="true" still refuses to work.

  • one
    Tried the ImageButton ? - Vladyslav Matviienko 6:08 pm
  • I'll try it out now - Jaktens Teed
  • It still doesn’t work - Jaktens Tied
  • one
    And where do you put the picture? in android:background or android:src ? - Vladyslav Matviienko
  • one
    Then they probably incorrectly added the picture. You just need to copy it to the folder of the desired density, for example drawable-xhdpi - Vladyslav Matviienko 6:51 pm

3 answers 3

According to the description, the problem is that there is no resource with sufficient picture quality. Either the resource with the appropriate qualifier is missing altogether (for example, res / drawable-xxhdpi / ), or the image stored in it is of poor quality — its own size in pixels is small for a given resolution or sufficiently large, but unsuccessful scaling, that is, the size is normal processing to increase it turned out crap instead of a clear picture.
So, for a screen with XXHDPI density (~ 480dp), an image for a standard icon (size 48x48dp) should have an absolute geometric dimensions of 144x144 pixels with content of appropriate quality (for more information see off-documentation ).

In the first case, the missing resources need to be created, in the second, to see what the images are all there and bring them in line.

PS: your selector is incorrect. At the end of the selector, there should be an item for the default view, which has no steate (in your case, instead of <item android:state_pressed="false"> should just be <item> and be the most recent in the item hierarchy. Choosing an item in the selector is done according to the following algorithm: the condition of the state of the first item is checked, if it is the same, it is displayed and the selector finishes work, otherwise it goes to the second item and checks its condition, and so on, if none of the conditions is fulfilled the last one is fulfilled by the item without conditions that As a rule, the normal form (a state without any clicks, tricks, selectors, etc).
The picture in the selector is not visible, because it is closed by shape - the shapes are not transparent by default. See, for example, this answer (second part) to create a view from the overlay image and shape.

  • Your tips helped in part. Now the rekthangl and the picture are shown, which is now on the whole button, but it is still of poor quality and with the "ladders". As for the off-documentation, I corrected the picture up to 144x144 pixels and moved it to the drawable / xxhdpi folder, in the android studio, the double click is shown in normal quality - and on two tested devices in bad. - Yaktens Teed
  • @ YaktensTede also similarly need to create folders for other densities (mdpi, hdpi, xhdpi, xxxhdpi) with images of the appropriate size (see offd. Documentation by reference in the reply) - pavlofff

Detailed documentation from Google will tell about Button

reference to the documentation

You can also read about resource files.

link to resource documentation

Also, to avoid the deformation of the image, I advise you to study such a thing as VectorDrawable, the link above has a description.

    I know that not everybody's advice is suitable, but why not use ImageView, for which, like the button, attach OnClickListener?

    • For me, this is quite an option if you do not need any animation of pressing and holding on the element. - DimXenon