0

I want to create custom button class to create buttons dynamically in code. General request is opportunity to dynamically changing button size so that total button row width must fit screen width regardless of amount of buttons (1 or 4 or 10.. it doesn't matter). And in addition there is request to make square buttons. Thanks.

UPD: May be i need use another layout istead of button. My purposes is find good way to implement a few games with square field with many square buttons (elements) like tik-tac-toe, sudoku etc.

meathme
  • 71
  • 8

2 Answers2

0

To have all the elements on a row have same width, you should try the weight property:

android:layout_width="0dp"
android:weight="1"

This should make all your elements the same size.
For the square part you can find a solution here.

Community
  • 1
  • 1
cosmincalistru
  • 1,283
  • 9
  • 20
  • I can't add weight to button programmatically. May be its because button as relative layout deprived of such feature. May be i'm mastaken in something. Thanks for idea. – meathme May 04 '14 at 18:30
0
  • You don't need custom buttons to create them dinamically. You can instantiate Android's Button class as you'd do with any other.
  • Neither you need custom classes for this kind of styling. Check this quick guide about buttons from Android devs.
  • For your case, best is to use a LinearLayout that fits your needs. Reference it on your activity as you'd normally do and add buttons dynamically to it. As cosmincalistru points, make sure to set the newly added buttons width to 0dp and weigth to 1.

Other resources:

Adding Buttons programmatically

Setting the weight of a button programmatically

More on LinearLayour and weights

Community
  • 1
  • 1
Jose L Ugia
  • 5,960
  • 3
  • 23
  • 26