Android: how to center a button programatically created?
<p>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:</p>

<pre><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);
</code></pre>

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

<p>Any clues on how to fix this?</p>

<p>Thanks a lot.</p>
