I have a question for Android developers. I have a layout with a button (developed programmatically, not with xml) and I want the button to fill the entire layout right now but it currently doesn’t and I’m not sure why, I thought I had everything set up correctly with the gravity of the button and the layout params but here’s what I have. If you can point me in the right direction I would really appreciate it! Thanks.
LinearLayout bottom = new LinearLayout(this);
bottom.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.FILL;
bottom.setLayoutParams(params);
bottom.setBackgroundColor(Color.BLUE);
Button eqbttn = new Button(this);
eqbttn.setText("=");
eqbttn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
}
});
bottom.addView(eqbttn);
You’ve only applied the
MATCH_PARENTsize to theLinearLayout. You need to apply it to the button, too.This will force the button to fill the LAYOUT both vertically and horizontally. If you need the layout itself to take up the whole screen, change its
WRAP_CONTENTtoMATCH_PARENTas well.Also, in this case, you do not require the
Gravity.FILLlayout parameter on theLinearLayout.