-1

I'd like to create lots of buttons with constant width and height on demand.

How do do the following in android?

for(row =0; row*buttonSizeY<otherButtonYCoardinate;row++){
    addButtonsToRow();
}

do{
    row++;
    addButtonsToRow();
}while(stillNeedToAddButton);

void addButtonsToRow(){
    for(col=0;col*buttonSizeX<screenWidth;col++){
        //instantiate button and set its X,Y coords according to row and col 
        relativeLayer.add(button);
    }
}

I didn't find a real way to add layouts with buttons on demand. I wanted to the above with TableLayout and add LinearLayouts to it as long as I have space for them, but I didn't find a way to do include without specifying all the LinearLayout-s in the XMLs. I'm also having a problem with the spacing in the screen width: my button has a constant height and width, since its oval. So I can't use layout_weight.

Vitali Pom
  • 602
  • 1
  • 8
  • 29

2 Answers2

1

You can create and add buttons as per demand with specifications required. Please check https://stackoverflow.com/a/7198409/1878148

Community
  • 1
  • 1
Narendra
  • 609
  • 1
  • 12
  • 28
  • One of the challenges was to create a round button, which was pretty complex since the roundness is destroyed when you apply weight to a normal round button. Or simply trying to make it's size dynamically set. And thanks for the link, I found a couple of things that will help me from here. – Vitali Pom Apr 25 '15 at 10:09
  • 2
    You can create "rounded button drawable" for button and apply it to the button you are adding. – Narendra Apr 25 '15 at 10:13
  • Yeah, I think that's what they did in markushi.ui.CircleButton – Vitali Pom Apr 25 '15 at 10:15
  • 2
    You can apply rounded button drawable programatically like yourBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_button)); – Narendra Apr 25 '15 at 10:16
0

Found a way without harming the design. markushi.ui.CircleButton allows to set weight without harming the roundness of the button, which became oval when I tried to implement it in the same way on my own.

The only disadvantage is that the above plugin requires api lvl 11 and I used lvl 9.

   <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/login_button"
        android:orientation="vertical"
        android:id="@+id/login_table_layout"
        >

        <TableRow

            android:background="#FFFFFF"
            android:orientation="horizontal"
            android:layout_weight="33" 

            >

            <at.markushi.ui.CircleButton
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="20"  
                app:cb_color="#99CC00"
                app:cb_pressedRingWidth="8dip"
                />
Vitali Pom
  • 602
  • 1
  • 8
  • 29