Search code examples
javaandroidmobilemargingrid-layout

How to set size of MARGINS in GridLayout


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 want to add a margin to the right of each button and text, so the words don't run into each other (like my image below).


Solution

  • No, these are not the sort of things an adapter is for.

    Set margin or padding (you probably want padding) inside your XML layout file.

    You can set android:paddingRight or android:paddingEnd in the XML declaration of the view. But if you want to be sure the text won't go beyond a certain point you are going to have to either cut it off at a certain point or allow it to wrap to another line.