Android: how to center a button programatically created?
===

I need to create a button programatically and have it centered on the layout, both horizontally and vertically. I am trying with the following code:

    LinearLayout ll = (LinearLayout)findViewById(R.id.layoutItem);
    Button b = new Button(this);        
    b.setBackgroundDrawable(getResources().getDrawable(R.drawable.button));
    b.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
    b.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
    ll.addView(b);

But it's not working. The button comes out on top all to the left.

Any clues on how to fix this?

Thanks a lot.