Mr.
Mr.

Reputation: 10102

Android - set layout width dynamically for buttons

I automated the process of creating the layout and the buttons it contains. Working with the GUI that Eclipse provide for editing layout, it let the use set the button's layout width property. I wish to do the same through code (dynamically). Basically, I wish to set the button's layout width property to MATCH_PARENT.

A snipped code or a good reference will be great. Thanks.

Upvotes: 0

Views: 4680

Answers (1)

jakk
jakk

Reputation: 1213

If your container is a LinearLayout its going to be like this:

    LinearLayout container = (LinearLayout)findViewById(R.id.container);

    Button btn = new Button(this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT);
    btn.setLayoutParams(lp);

    container.addView(btn);

Upvotes: 1

Related Questions