-1

I am adding ImageButtons one at a time to a GridLayout using RecyclerView. My app listens for incoming images and strings to label the buttons.

I need to be able to set the size of the buttons to a set value (such as 50x50dp). My image below shows how my images are coming in and I realized that each spot in the row doesn't get filled because some images are coming in bigger than others and not leaving room for other images to appear.

Is there a way that I can set this in my incoming ImageButtons? Where do I set these parameters? In my RecyclerViewAdapter?

2 Answers2

1

You might want to try to set adjustViewBounds to true.

This can be done in your XML with android:adjustViewBounds="true"

or in code with imageButton.setAdjustViewBounds(true); - I would call this just after the view is initialized.

Personally I prefer to set these things in XML whenever possible because that means I have less code to try to understand later.

Either way - you can set the size of your ImageButton to values in a value and value-land file so that the OS will handle these things for you. Again, less code is better IMO. If the buttons will always be one size you can just set them in the layout item itself.

jwehrle
  • 4,414
  • 1
  • 17
  • 13
  • 1
    Thank you it worked :) turns out I was actually doing it correct the whole time but that wasn't the problem to why the buttons were replacing/removing each other. I made another post --> http://stackoverflow.com/questions/37284051/android-buttons-dont-show-up-or-remove-each-other-in-gridlayout but thank you still so much for doing this and this solution does work :) – Emily Di-Lee May 17 '16 at 18:49
0

You can try something like the code below. This code worked for me in a similar situation.

public Image resizeImageIcon(Image img)
{
    int w;
    int h;
    if(img.getWidth(null) >= img.getHeight(null))
    {
        w = ((50/img.getWidth(null))*img.getWidth(null));
        h = ((50/img.getWidth(null))*img.getHeight(null));
    }
    else
    {
        w = ((50/img.getHeight(null))*img.getWidth(null));
        h = ((50/img.getHeight(null))*img.getHeight(null));
    }

    return img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
}
josemr
  • 253
  • 1
  • 9
  • I keep getting the error "Cannot resolve method getScaledInstance", is it because I'm using this inside of a Fragment. I'm also getting the error "Cannot resolve symbol SCALE_SMOOTH" – Emily Di-Lee May 16 '16 at 23:47
  • The code above is for a Java desktop application, you can use the code to guide you to a solution for Android. – josemr May 16 '16 at 23:56
  • I see, do you by any chance know the alternatives to "getScaledInstance" & "SCALE_SMOOTH" for Android? :) Thank you – Emily Di-Lee May 17 '16 at 14:36
  • 1
    You can try combining the above code with the following answer http://stackoverflow.com/questions/10413659/how-to-resize-image-in-android – josemr May 17 '16 at 14:48