0

My name is Pawel.

I want to have layout like this:

Layout

I want to make the layout in java, because the number of button will change. Squares are buttons. Layout consisting of:

<LinearLayout>  
    <Button/>  
    <TableLayout>  
        <TableRow>  
            <Button1/>  
            <Button2/>  
        </TableRow>  
        <TableRow>  
            <Button3/>  
            <Button4/>  
        </TableRow>  
    <TableLayout>  
    <Button/>  
</LinearLayout>

My code:

@Override
protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        int title = getIntent().getIntExtra(KEY, MainScreen.CHOICE_COMPUTER);

        switch(title)
        {
            case MainScreen.CHOICE_COMPUTER:
                setTitle("Computer");
                break;
            case MainScreen.CHOICE_LAPTOP:
                setTitle("Laptop");
                break;
            case MainScreen.CHOICE_TV:
                setTitle("Tv");
                break;
            case MainScreen.CHOICE_FRIDGE:
                setTitle("Fridge");
                break;
        }

        LinearLayout choiceLayout = new LinearLayout(this);
        choiceLayout.setOrientation(LinearLayout.VERTICAL);
        choiceLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        choiceLayout.setPadding(15, 15, 15, 15);
        choiceLayout.setGravity(Gravity.CENTER);
        choiceLayout.setBackgroundResource(R.drawable.background);

        button1 = new Button(this);
        LinearLayout.LayoutParams buttonParameters = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        buttonParameters.setMargins(50, 20, 50, 70);
        button1.setLayoutParams(buttonParameters);
        button1.setGravity(Gravity.CENTER);
        button1.setPadding(10, 10, 10, 10);
        button1.setBackgroundResource(R.drawable.background_button);
        switch(title)
        {
            case MainScreen.CHOICE_COMPUTER:
                button1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_button_computer, 0, 0);
                button1.setText("Computer");
                break;
            case MainScreen.CHOICE_LAPTOP:
                button1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_button_laptop, 0, 0);
                button1.setText("Laptop");
                break;
            case MainScreen.CHOICE_TV:
                button1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_button_tv, 0, 0);
                button1.setText("Tv");
                break;
            case MainScreen.CHOICE_FRIDGE:
                button1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_button_fridge, 0, 0);
                button1.setText("Fridge");
                break;
        }

        TableLayout choiceTableLayout = new TableLayout(this);
        TableLayout.LayoutParams param1 = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        param1.setMargins(20, 20, 20, 20);
        choiceTableLayout.setLayoutParams(param1);
        choiceTableLayout.setGravity(Gravity.CENTER);
        choiceTableLayout.setStretchAllColumns(true);

        TableLayout.LayoutParams rowLayout = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        TableRow row1 = new TableRow(this);
        TableRow row2 = new TableRow(this);

        row1.setLayoutParams(rowLayout);
        row2.setLayoutParams(rowLayout);

        LinearLayout.LayoutParams buttonLayout = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
        buttonLayout.setMargins(10, 10, 10, 10);

        buttonOption1 = new Button(this);
        buttonOption1.setLayoutParams(param1);
        buttonOption1.setId(objectCounter++);
        Log.d("Counter: ", Integer.toString(objectCounter));
        Log.d("ID", Integer.toString(buttonOption1.getId()));
        buttonOption1.setGravity(Gravity.CENTER);
        buttonOption1.setBackgroundResource(R.drawable.option_off_background_button);
        buttonOption1.setTextSize(19);
        buttonOption1.setTextColor(getResources().getColor(R.color.color_text_button));
        buttonOption1.setText("Off");
        buttonOption1.setBackgroundResource(R.drawable.option_off_background_button);
        buttonOption1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.option1, 0, 0);

        buttonOption2 = new Button(this);
        buttonOption2.setLayoutParams(buttonLayout);
        buttonOption2.setId(objectCounter++);
        buttonOption2.setGravity(Gravity.CENTER);
        buttonOption2.setBackgroundResource(R.drawable.option_background_button);
        buttonOption2.setTextSize(19);
        buttonOption2.setTextColor(getResources().getColor(R.color.color_text_button));
        buttonOption2.setText(R.string.button_text_option2);
        buttonOption2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.option2, 0, 0);

        buttonOption3 = new Button(this);
        buttonOption3.setLayoutParams(buttonLayout);
        buttonOption3.setId(objectCounter++);
        buttonOption3.setGravity(Gravity.CENTER);
        buttonOption3.setBackgroundResource(R.drawable.option_background_button);
        buttonOption3.setTextSize(19);
        buttonOption3.setTextColor(getResources().getColor(R.color.color_text_button));
        buttonOption3.setText(R.string.button_text_option3);
        buttonOption3.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.option3, 0, 0);

        buttonOption4 = new Button(this);
        buttonOption4.setLayoutParams(buttonLayout);
        buttonOption4.setId(objectCounter++);
        buttonOption4.setGravity(Gravity.CENTER);
        buttonOption4.setBackgroundResource(R.drawable.option_background_button);
        buttonOption4.setTextSize(19);
        buttonOption4.setTextColor(getResources().getColor(R.color.color_text_button));
        buttonOption4.setText(R.string.button_text_option4);
        buttonOption4.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.option4, 0, 0);

        buttonSend = new Button(this);
        LinearLayout.LayoutParams buttonSendParameter = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        buttonSendParameter.gravity = Gravity.RIGHT|Gravity.BOTTOM;
        buttonSend.setLayoutParams(buttonSendParameter);
        buttonSend.setId(objectCounter++);
        buttonSend.setGravity(Gravity.RIGHT|Gravity.BOTTOM);
        buttonSend.setBackgroundResource(R.drawable.background_button);
        buttonSend.setBackgroundResource(R.drawable.button_send);

        row1.addView(buttonOption1);
        row1.addView(buttonOption2);
        row2.addView(buttonOption3);
        row2.addView(buttonOption4);
        choiceTableLayout.addView(row1);
        choiceTableLayout.addView(row2);
        choiceLayout.addView(button1);
        choiceLayout.addView(choiceTableLayout);
        choiceLayout.addView(buttonSend);

        setContentView(choiceLayout);

        buttonOption1.setOnClickListener(this);
        buttonOption2.setOnClickListener(this);
        buttonOption3.setOnClickListener(this);
        buttonOption4.setOnClickListener(this);
        buttonSend.setOnClickListener(this);
    }

Error in eclipse:

09-21 12:41:09.062: E/AndroidRuntime(758): java.lang.ArithmeticException: divide by zero

I think it is connected with stretching columns.

My question is: how to correct this in android to make layout like in the picture?

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
Pawel
  • 1,457
  • 1
  • 11
  • 22
  • How to create layout for the button in the TableLayout? There is no error without setting layout to buttons inside the TableLayout. – Pawel Sep 21 '12 at 13:41
  • at which line the the error exception happens – Ron Sep 21 '12 at 14:50
  • 09-21 15:15:13.285: E/AndroidRuntime(779): at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:583) – Pawel Sep 21 '12 at 15:16
  • try adding `android:stretchColumns="0,1"` to `TableLayout`.. dont use weight. – Ron Sep 21 '12 at 15:34
  • Why do you have zero width for buttons? – Ron Sep 21 '12 at 15:37
  • [link](http://stackoverflow.com/questions/2393847/how-can-i-get-an-android-tablelayout-to-fill-the-screen) – Pawel Sep 21 '12 at 15:40

1 Answers1

2

Change this line

LinearLayout.LayoutParams buttonLayout = 
             new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);

To

TableRow.LayoutParams buttonLayout = 
             new TableRow.LayoutParams(LayoutParams.FILL_PARENT, 
                                       LayoutParams.WRAP_CONTENT);

As you are stretching all fields, i feel there is no need for the weight parameter. It same as FILL_PARENT

Ron
  • 24,175
  • 8
  • 56
  • 97