Alex Bond Alex Bond - 1 month ago 8
Java Question

How to move a row with buttons?

I need you help.

I made a small application, and it is necessary to bring "tiles" button.

Now i have an array with letters

<string-array name="let_terms">
<item>A</item>
<item>B</item>
<item>C</item>
......


And then programmatically output the button with these letters:

public class letterms extends AppCompatActivity {

String[] mArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.letterms);

int length =0;

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

length =getResources().getStringArray(R.array.let_terms).length;

mArray = getResources().getStringArray(R.array.let_terms);

final LinearLayout linearLayout=(LinearLayout)findViewById(R.id.buttonlayout);

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

final String nazv = mArray[i];
final String[] splittedItem = nazv.split(":");

Button button=new Button(this);
button.setId(i);
button.setWidth(20);
button.setLayoutParams(params);
button.setText(splittedItem[0]);
button.setTextColor(0xFF2C85A6);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

Intent is = new Intent(getApplicationContext(), termin_full.class);
is.putExtra("fVariableName", nazv);
startActivity(is);
}
});

linearLayout.addView(button);
}

}
}


In XML:

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/buttonlayout">

</LinearLayout>


All right, go bat buttons in a row, and I would like to carry on a new line when it reaches the end of the screen. How to tile...

Where can I see an example of such an implementation?

Answer

Use vertical LinearLayout in XML. Then programmatically create horizontal LinearLayout and add buttons in horizontal layout. For each line, create and add new horizontal layout.

XML:

<LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/buttonlayout">
</LinearLayout>

ACTIVITY:

public class letterms extends AppCompatActivity {

    String[] mArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.letterms);

        int length =0;

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        length =getResources().getStringArray(R.array.let_terms).length;

        mArray = getResources().getStringArray(R.array.let_terms);

        final LinearLayout verticalLayout= LinearLayout)findViewById(R.id.buttonlayout);

        int verticalWidth = verticalLayout.getWidth();
        int numberOfButtonsPerLine = (verticalWidth/buttonWidth);
        int numberOfLines = (length/numberOfButtonsPerLine) + 1;

        for(int i=0;i<length;){
          LinearLayout newLine = new LinearLayout(this);
          newLine.setLayoutParams(params);
          newLine.setOrientation(LinearLayout.HORIZONTAL);
          for(int j=0;j<numberOfLines;j++){

            final String nazv = mArray[i];
            final String[] splittedItem = nazv.split(":");

                Button button=new Button(this);
                button.setId(i);
                button.setWidth(20);
                button.setLayoutParams(params);
                button.setText(splittedItem[0]);
                button.setTextColor(0xFF2C85A6);
                button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                        Intent is = new Intent(getApplicationContext(), termin_full.class);
                        is.putExtra("fVariableName", nazv);
                        startActivity(is);
                    }
                });

                newLine.addView(button);
                i++;
                if(i>=length) {
                   break;
                }
          }
          verticalLayout.addView(newLine);

        }

    }
}