4

Its create dynamic button in a single row

 final LinearLayout layoutshape = (LinearLayout) findViewById(R.id.linearshape);
  a = new Button[10];
  for (int i = 0; i < 10; i++) {
      a[i] = new Button(this);
      a[i].setText(""+i);
      a[i].setId(i);
      a[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                                               LayoutParams.WRAP_CONTENT));
      a[i].setBackgroundResource(R.drawable.background);
     layoutshape.addView(a[i]);
    }

but I wanna make this to be split in to three rows. Eg: If I've 30 buttons then its should be displayed in 10 button in 1st row then next 10 in second row and 10 in third row

o/p buttons as to be:

b1 b2 b3 b4 b5

b6 b7 b8 b9 b10

b11 b12 b13 b14 b15

3
  • try the gridview for your case. Commented Apr 2, 2013 at 5:53
  • any other possibilities in linear? Commented Apr 2, 2013 at 5:57
  • Or you can you tablelayout Commented Apr 2, 2013 at 6:04

4 Answers 4

2

First of all set orientation for your main layout(layoutshape) to vertical.

for (int i = 0; i < i/10; i++) {

LinearLayout row = new LinearLayout(this);

set orientation for row to horizontal.

for (int i = 0; i < 30; i++) {

  a[i] = new Button(this);
  a[i].setText(""+i);
  a[i].setId(i);
  a[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                                           LayoutParams.WRAP_CONTENT));
  a[i].setBackgroundResource(R.drawable.background);
 row.addView(a[i]);
}
layoutshape.addView(row);
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Mj its creates same buttons thrice in a 3rows
@arshad You are right second loop should be for(i=0;i<10;i++) but this is just a example code.
1

You will need at least n LinearLayout where n is your number of rows. If you really want to use a LinearLayout then add your buttons using the following LayoutParams:

new LinearLayout.LayoutParams(0,LayoutParams.WRAP_CONTENT, 1/numButtonsInRow);

1/numButtonsInRow is the weight of the button. For instance if you have 10 buttons in a row, each button will have a weight of 0.1. If your Layout is horizontal, set its width to 0, if it is vertical set its height to 0.

In your case, you probably should use a GridLayout or TableLayout though.

Comments

0

Create 3 LinearLayouts in the "layoutshape" LinearLayout with orientation as vertical. And then add the buttons to the inner linear layouts.

1 Comment

yeah! had to do with. but looking for any otherways else need to create 3 layouts
0

30 buttons look like a keyboard: http://developer.android.com/reference/android/inputmethodservice/Keyboard.html and the keyboard may be created via an xml (see the SoftKeyboard example in SDK, the samples/android-10/SoftKeyboard subdirectory).

If you really need buttons in 3 rows, my suggestion is 3 horizontal linear layouts filled in at run-time. But please note that the screen sizes vary, and what is good on a phone may look ugly on a tablet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.