3

I want to parse text, and create for each word - button, but i don't know how to arrange them one after the other

String s = "Alice was beginning to get very tired of sitting";
String[] q = s.split(" ");
for (int i = 0; i < q.length; i++) {
    Button myButton = new Button(this);
    myButton.setText(q[i]);

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
    layout.addView(myButton, params);

}

img

AdamMc331
  • 16,492
  • 10
  • 71
  • 133
IGentlich
  • 81
  • 2
  • 13
  • 2
    I suggest you try a `TableLayout`. Dynamically building a RelativeLayout like that will be difficult – OneCricketeer Aug 16 '16 at 17:56
  • I second cricket_007's comment. It would be neigh impossible to fill a relativelayout like that using a for loop. – John Gallagher Aug 16 '16 at 17:57
  • addrules but will cannot be with loop – Rachit Solanki Aug 16 '16 at 17:58
  • In your case, I think you might want to consider using horizontal LinearLayouts inside another vertical layout. You can check when adding a view to the LinearLayout would cause that view to get cut off of the side of the display and then put it in another LinearLayout below that. - if you don't want to use tablelayout that is. – John Gallagher Aug 16 '16 at 17:59
  • 1
    This is an example of a decent question that doesn't get enough upvotes. – AdamMc331 Aug 16 '16 at 18:12
  • The screenshot on the right looks shows that all your buttons are there. They are just stacked on top of each other. Consider converting to use gridview and adapter or the flowlayout library answered below. – petey Aug 16 '16 at 18:40

4 Answers4

2

See this custom library: FlowLayout

While you're adding views inside FlowLayout, it automatically wraps when there is no space for the next item.

Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73
2

There's not much wrong about your approach, it's only that relative layout as name suggests requires child views to have some parameters to align the views relative to them e.g. above, below etc. As a result you are getting views overlapping each other and hence only the last added view is visible being on top.

Use FlowLayout instead and you'll be fine.

Zuhaib Ahmad
  • 93
  • 2
  • 8
0

You need to define RelativeLayout parameters as in example below

Heres an example to get you started, fill in the rest as applicable:

TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);

The docs for RelativeLayout.LayoutParams and the constructors are here

From: How to add a view programmattically to RelativeLayout?

Check the link below to get more useful informations.

Hope it will help

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
0

In the following code, you should change the upper limits of the for, to a variable.

public class MainActivity
        extends Activity
        implements View.OnClickListener {

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

            TableLayout layout = new TableLayout (this);
            layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );

            layout.setPadding(1,1,1,1);

            for (int f=0; f<=13; f++) {
                TableRow tr = new TableRow(this);
                for (int c=0; c<=9; c++) {
                    Button b = new Button (this);
                    b.setText(""+f+c);
                    b.setTextSize(10.0f);
                    b.setTextColor(Color.rgb( 100, 200, 200));
                    b.setOnClickListener(this);
                    tr.addView(b, 30,30);
                } // for
                layout.addView(tr);
            } // for

            super.setContentView(layout);
        } // ()

        public void onClick(View view) {
            ((Button) view).setText("*");
            ((Button) view).setEnabled(false);
        }
    } // class
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40