I have all the icons in the mipmap-hdpi folder, but then I wanted to set the background of the pressed button, as they say you need to create a selector, and according to the standard, it pulls the button from the drawable folder, which I did.

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/ic_wok_ovosh_7na8" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/ic_wok_ovosh_7na8_pressed" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/ic_wok_ovosh_7na8_pressed"/> <item android:drawable="@drawable/ic_wok_ovosh_7na8" /> </selector> 

and referenced in xml

 <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton_wok" **android:src="@mipmap/ic_wok_ovosh_7na8" android:background="@drawable/button_background_selector"** android:layout_margin="@dimen/activity_vertical_margin"/>` 

But the problem is that the ImageButton from the drawable folder is superimposed on top of another from mipmap-hdp (which is logical as I indicated in the xml code) and it is more, although the background works, but it transforms like that by its size, not like from mipmap -hdpi, and because of this, the entire layout is twisting. This is actually the question, is it possible to do without the drawable folder in this business? Or is it necessary to redo all the icons and place there so that there is a background?

Here are the screenshots, what icons before adding the selector enter image description here

And here with the use of the selector and how everything falls, I deliberately screamed the pressed button, what would you see that everything works, but the problems start with the layout, since the relative layout is placed in the scroll view enter image description here

  • one
    Set the background and the response to pressing a uniform background of a different color / tone or remove the android:src attribute, if you need the icon to be different when you click or assign your selector for android:src , and not for the background, do not specify the background - depending on what result you need to get - pavlofff
  • @pavlofff, I reassigned the selector to android: src, but the size of the imagebutton is still not what I need, and because of this, lay out the layout, there are a lot of elements. I can't set the imagebutton background color, because when creating the project, the Android Studio itself creates a standard gray background for the imagebutton, and how much I didn’t dig took just turned it off android: background = "@ null", so the only way to probably change the background It is the change of the picture, but in turn, this is not very clear scaling What would you recommend?) - Romik romikromik
  • Describe your task in more detail (by editing the question). What size you need, what it turns out that you want to get, preferably screenshots. Android Studio is an IDE - just a program for conveniently writing code and assembling a project, it does not create any backgrounds, this is defined in the Android framework. - pavlofff
  • @pavlofff Added screenshots to the main page, so that you understand what I mean, I'm new to coding, so sorry for elementary) - Romik romikromik
  • Do you have the same pixel image of a normal picture and a pressed pixel size? Remove the android:src="@mipmap/ic_wok_ovosh_7na8" parameter from the ImageView attributes android:src="@mipmap/ic_wok_ovosh_7na8" , what happens? - pavlofff

1 answer 1

I propose the following solution (here it is six cells, but you can add any number you need):

Reject RelativeLayout as it is a fairly resource-intensive container that is not needed here, in favor of the TableLayout most suitable for this markup with TableLayout columns ( android:stretchColumns="*" ). Use ImageView instead of ImageButton , so it does not have a default background. Here, for all ImageView , one selector resource selectable_image1.xml is set as an image, you will naturally make your own cell for each cell, with different pictures:

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:isScrollContainer="true" android:stretchColumns="*"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="30dp"> <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="30dp"> <ImageView android:id="@+id/imageView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> <ImageView android:id="@+id/imageView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="30dp"> <ImageView android:id="@+id/imageView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> <ImageView android:id="@+id/imageView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> </TableRow> </TableLayout> </ScrollView> 

the android:paddingTop="30dp" in the tableRow elements sets the distance between rows.

The images themselves. In this case, I think it would be appropriate to use an image of the same size for all screen densities ( hdpi , xhdpi , etc.), with its own pixel size sufficient for high-quality display on xhdpi screens, on other screens it will be scaled using the android:adjustViewBounds="true" parameter android:adjustViewBounds="true" for ImageView . We set the parameter for android:clickable="true" for ImageView so that it reacts to clicks.

To make the response to pressing, I suggest not to take another set of images, but simply add a frame to the already existing images, this will reduce the size of the application.

We make a resource for the pressed state. On the image we impose a frame (rectangle) 5dp thick with roundings of 10dp (you can choose your own values). With the background of the fill of the entire rectangle, we specify transparent - # 0000 so that the image can be seen and only the frame is displayed. Let's call it press_image1.xml . We place in the res / drawable / folder:

press_image1.xml

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <bitmap android:src="@mipmap/image1" /> </item> <item> <shape android:shape="rectangle"> <corners android:radius="10dp" /> <solid android:color="#0000"/> <stroke android:width="5dip" android:color="#3dbc24" /> </shape> </item> </layer-list> 

Where image1 is a cell image.

Make a selector to react to clicks, let's call it selectable_image1.xml , put it in the / res / drawable / folder / This selector will be the image source in the ImageView , which we specify using the android:src parameter android:src :

selectable_image1.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/press_image1" /> <item android:drawable="@mipmap/image1" /> </selector> 

Well that's all. You will need to do a similar job for each picture.

  • Thank!!!!!!!!! - Romik romikromik
  • You just really helped, a whole lecture right painted! - Romik romikromik