Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android programatically set margin to ImageButton in TableRow

I have defined a TableLayout in my Java code. For each row, I'm displaying three ImageButtons. When one of those buttons is pressed, the background color changes. To display it nicely, I have set a padding of 10px.

The problem I'm having now is that if you press two buttons that are next to each other, you don't exactly see a break between the buttons. So I'm wondering, is there a possibility to set a margin on the ImageButtons or an other solution whatshowever?

like image 306
Hannelore Avatar asked Oct 17 '25 15:10

Hannelore


2 Answers

Try

int leftMargin = 10;
((MarginLayoutParams) imageButton.getLayoutParams()).leftMargin = leftMargin;

EDIT: If you don't use ImageButton defined in xml, you have to set LayoutParams like this:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
params.leftMargin = 10;
b.setLayoutParams(params);

Here I assume you use LinearLayout in your list item.

like image 120
ernazm Avatar answered Oct 20 '25 05:10

ernazm


Make sure to use to use the LayoutParams for the container the control is in. In your case the buttons are in a TableRow so use TableRow.LayoutParams if you use any other type of params, the control will not show at all.

like image 32
Shabnam Sarup Avatar answered Oct 20 '25 05:10

Shabnam Sarup



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!