0

I have some problems trying to add a background image in my button programmatically. It works well on my XML file, but programmatically the background image of my button is bigger than normal. I mean my image is High:150px but when I run this code its looks bigger. I had already try setting my button maxheight function, but it doesn't works. BTW the button is inside a row, and the row (obviously) inside a TableLayout.

Thanks in advice for the help!

for(int i = 0; i < 3; i++){
    TableRow tableRow = new TableRow(this);         
    tableRow.setId(0 + i);
    tableRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 150));   

    Button btnProduct = new Button (this);
    btnProduct.setId(10 + i);
    btnProduct.setBackgroundResource(R.drawable.shoes1);
    btnProduct.setHeight(150);
    btnProduct.setWidth(161);   
    btnProduct.setText("View");

    tableRow.addView(btnProduct);
    mainTableLayout.addView(tableRow);
}
ninjalj
  • 42,493
  • 9
  • 106
  • 148
FelipeDev.-
  • 3,113
  • 3
  • 22
  • 32
  • You should post your XML for comparison, but I'm guessing that you have in your XML your button set to `150dp` and not `150px`. This would account for the difference. See [this post](http://stackoverflow.com/questions/4605527/converting-pixels-to-dp-in-android) for converting dp to px. – kabuko Dec 03 '11 at 00:53

1 Answers1

0

I agree with kabuko, you could possibly confusing dp(density pixels) with px(normal pixels) That mix-up could create a notable size difference depending on the resolution of your screen.

Lijap
  • 625
  • 3
  • 7
  • 23