-2

I want to make a calculator with TableLayout. I know it fits better with GridLayout but TableLayout is a must.

I try to get this design:

enter image description here

As you seae, the numbers(1 to 9) are in the same size and order with connect and disconnect buttons. However, in my current design, Its like that:

but button in my design look like:

enter image description here

This is my code:

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

TextView titleView = new TextView(this);
titleView.setText("Table Layout");
titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(titleView);


LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.HORIZONTAL);
layout2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));




Button btnConnect = new Button(this);
btnConnect.setText("Connect");
btnConnect.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
layout2.addView(btnConnect);

TextView titleViewSpace = new TextView(this);
titleViewSpace.setLayoutParams(new  LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
layout2.addView(titleViewSpace);


Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
btnDisconnect.setLayoutParams(new  LinearLayout.LayoutParams(0,  LinearLayout.LayoutParams.WRAP_CONTENT, 1));
layout2.addView(btnDisconnect);

layout.addView(layout2);

TableLayout tblLayout = new TableLayout(this);
tblLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT));
TableRow tblrow = null;


for (int i = 1; i <= 9; i++) {
    if (i % 3 == 1) {
        tblrow = new TableRow(this);
        tblLayout.addView(tblrow);

    }
    Button b = new Button(this);
    b.setText("" + i);
    tblrow.addView(b);
}




TableRow tr = new TableRow(this);
Button btnZero = new Button(this);
btnZero.setText("0");
Button btnHash = new Button(this);
btnHash.setText("#");
Button btnStar = new Button(this);
btnStar.setText("*");


tr.addView(btnZero);
tr.addView(btnHash);
tr.addView(btnStar);


tblLayout.addView(tr);
layout.addView(tblLayout);


setContentView(layout);

in order to make my buttons in same order and size with connect and disconnect buttons

i simply do that when i create button in loop

for (int i = 1; i <= 9; i++) {
        if (i % 3 == 1) {
            tblrow = new TableRow(this);
            tblLayout.addView(tblrow);

        }
        Button b = new Button(this);
        b.setText("" + i);
        b.setLayoutParams(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1));
        tblrow.addView(b);
    }

but when i put this LayoutParams:

b.setLayoutParams(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1));

buttons(1 to 9) are disappered in the screen. Why? How can i overcome with this problem? How can i make button like that in the same screen?

3
  • so why you make your life so hard and you create a view programmatic ally, why not xml file?
    – Daler
    Mar 6, 2013 at 10:37
  • because this is must in the assignment.
    – abc
    Mar 6, 2013 at 10:37
  • my advice for you is to make this view first in xml, then copy and paste correct properties in to the coding style. To make buttons stretch accordingly you should put LinearLayout's weightSum property also..
    – Daler
    Mar 6, 2013 at 10:45

1 Answer 1

0

this is all i needed:

tblLayout.setStretchAllColumns(true);

Not the answer you're looking for? Browse other questions tagged or ask your own question.