0

how do I create an array of buttons of this kind???

btnA btnB btn1 btnC
btnD btnE btnF btnJ
     btnX btnN

I do so, but buttons are displayed vertically. can someone tell me the algorithm?

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TableLayout;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
   TableLayout layout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         layout = (TableLayout) findViewById(R.id.lay);
         TableLayout.LayoutParams layoutParams = new
        TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,
        TableLayout.LayoutParams.WRAP_CONTENT);
        for(int i = 0; i < 12; i++){
            Button button = new Button(this);
            button.setLayoutParams(layoutParams);
            button.setId(i);
            button.setText("Buttun"+i);
            layout.addView(button,50,20);
        }
    }
    }
Gangnus
  • 24,044
  • 16
  • 90
  • 149
user2954895
  • 77
  • 1
  • 8

4 Answers4

0

You should create 3x TableRows in the TableLayout. And place your buttons in these rows. Look at this table layout tutorial.

As it is, you are simply using the TableLayout incorrectly. So you can see virtually anything.

I have done an activity that created a large table of things two years ago.

Look at the code here

/**
 * the current row of the currently selected element (starting by 0)
 * @return
 */
int currentRow(int position) {
    int numOnPage = position - leftTopNumber;
    return numOnPage - currentColumn(position) * rowsNumber;
}

/**
 * the current column of the currently selected element (starting by 0)
 * @return
 */
int currentColumn(int position) {
    int numOnPage = position - leftTopNumber;
    return numOnPage / rowsNumber;
}

/**
 * setting width and height for a ViewGroup instance
 * @param viewGroup instance to be changed
 * @param width if it is 0, the ViewGroup instance width remains as it was
 * @param height if it is 0, the ViewGroup instance height remains as it was
 */
static void setMeasures(ViewGroup viewGroup, int width, int height) {
    LayoutParams params = viewGroup.getLayoutParams();
    if (width != 0) params.width = width;
    if (height != 0) params.height = height;
    viewGroup.setLayoutParams(params);
}

@Override
public void onResume() {
    super.onResume();
}

static private LinearLayout table;
static private TextView leftTriangle, rightTriangle;

boolean fillTableByCells() {
    int i = 0, j = 0;
    try {
        final LayoutInflater inflater =
                (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setContentView(R.layout.channels_list_table);
        LinearLayout allForm = (LinearLayout) findViewById(R.id.AllChannelsListForm);

        LinearLayout upperMargin = (LinearLayout) findViewById(R.id.UpperMargin);
        LinearLayout middleStripe = (LinearLayout) findViewById(R.id.MiddleStripe);
        LinearLayout bottomMargin = (LinearLayout) findViewById(R.id.BottomMargin);

        RelativeLayout leftMargin = (RelativeLayout) findViewById(R.id.LeftMargin);
        LinearLayout tableWrapper = (LinearLayout) findViewById(R.id.CentralFieldWrapper);
        RelativeLayout rightMargin = (RelativeLayout) findViewById(R.id.RightMargin);

        tvComposeNumber = (TextView) findViewById(R.id.tvComposeNumber);

        leftTriangle = (TextView) findViewById(R.id.TriangleLeft);
        rightTriangle = (TextView) findViewById(R.id.TriangleRight);

        table = (LinearLayout) findViewById(R.id.table_channels);

        allForm.measure(0, 0);
        int formWidth = allForm.getWidth();
        int formHeight = allForm.getHeight();

        formWidth = 960;
        formHeight = 540;

        int heightUpper = formHeight * 66 / 570;
        int heightMiddle = formHeight * 410 / 570;
        int heightBottom = formHeight * 94 / 570;

        int widthLeft = formWidth * 60 / 1140;
        int widthMiddle = formWidth * 1020 / 1140;
        int widthRight = formWidth * 60 / 1140;

        setMeasures(allForm, formWidth, formHeight);

        setMeasures(upperMargin, formWidth, heightUpper);
        setMeasures(middleStripe, formWidth, heightMiddle);
        setMeasures(bottomMargin, formWidth, heightBottom);

        setMeasures(leftMargin, widthLeft, heightMiddle);
        setMeasures(tableWrapper, widthMiddle, heightMiddle);
        setMeasures(rightMargin, widthRight, heightMiddle);

        LinearLayout.LayoutParams wrapperLP =
                (LinearLayout.LayoutParams) tableWrapper.getLayoutParams();
        LinearLayout.LayoutParams tableLP = (LinearLayout.LayoutParams) table.getLayoutParams();
        int widthInsideTable =
                widthMiddle - wrapperLP.leftMargin - wrapperLP.rightMargin
                        - tableWrapper.getPaddingLeft() - tableWrapper.getPaddingRight()
                        - tableLP.leftMargin - tableLP.rightMargin - table.getPaddingLeft()
                        - table.getPaddingRight();

        // setMeasures(table,widthMiddle,heightMiddle);
        cellsNumber = TvChannel.list.size();

        columnsNumber = (cellsNumber - 1) / rowsNumber + 1;

        for (i = 0; i < columnsNumber; i++) {
            LinearLayout column =
                    (LinearLayout) inflater
                            .inflate(R.layout.channels_list_column, table, false);
            table.addView(column, i);

            for (j = 0; j < rowsNumber; j++) {
                int numberChannel = i * rowsNumber + j;
                if (numberChannel >= TvChannel.list.size()) break;
                LinearLayout cell =
                        (LinearLayout) inflater.inflate(R.layout.channels_list_cell, column,
                                false);
                column.addView(cell, j);

                TextView numberChannelView = (TextView) cell.getChildAt(0);
                TextView nameChannelView = (TextView) cell.getChildAt(1);

                nameChannelView.setText(TvChannel.list.get(numberChannel).name);
                String numberString = String.format("%d", i * rowsNumber + j);
                numberChannelView.setTextSize(new float[] { 22f, 20f, 17f }[numberString
                        .length() - 1]);
                numberChannelView.setText(numberString);
                cell.setVisibility(View.VISIBLE);
            }
            // column.setLayoutParams(new
            // LayoutParams(tableWidth/3,LayoutParams.MATCH_PARENT));

            LinearLayout.LayoutParams columnLP =
                    (LinearLayout.LayoutParams) column.getLayoutParams();
            int colWidth =
                    widthInsideTable / maxSeenColumns - columnLP.leftMargin
                            - columnLP.rightMargin;
            setMeasures(column, colWidth, LayoutParams.MATCH_PARENT);

            for (int iCell = 0; iCell < column.getChildCount(); iCell++) {
                LinearLayout cell = (LinearLayout) column.getChildAt(iCell);
                setMeasures(cell, LayoutParams.MATCH_PARENT,
                        (heightMiddle - column.getPaddingBottom() * 2) / rowsNumber);
            }

        }
        return true;
    }
    catch (Throwable e) {
        ErrorMessage.outputMessageByName("channels_list_activity_create", this, e.toString()
                + "; column=" + i + "; row=" + j);
        finish();
        return false;
    }
}
Gangnus
  • 24,044
  • 16
  • 90
  • 149
0

You can use a vertical LinearLayout with horizontal childs that contain buttons.

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

Means it will take as much horizontal space as it can so this way all buttons stretch equally.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button5"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button6"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button7"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button8"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <View
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/button9"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button10"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button" />

        <View
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
0

dynamically u want add button,

the rough idea is,

create one main linear layout with vertical orientation.

and sublayout1 with horizontal orientation with layout gravity center.

and sublayout2 with horizontal orientation with layout gravity center.

and sublayout3 with horizontal orientation with layout gravity center.

and sublayout4 with horizontal orientation with layout gravity center.

add button on

then

sublayout1.add(btn1)
sublayout1.add(btn2)
sublayout1.add(btn3)
sublayout1.add(btn4)

mainliniarlayout.add(sublayout)

similiarly add other button and layout.

for add button dynamically, read below thread,

How can I dynamically create a button in Android?

Community
  • 1
  • 1
0

thank you all! I thought about it and decided to do not dynamically (although it seems to me that long). I need only two alphabets, so probably will not be subverted.

user2954895
  • 77
  • 1
  • 8