Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create buttons in sequential order programmatically

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

like image 997
IGentlich Avatar asked Sep 12 '26 21:09

IGentlich


2 Answers

See this custom library: FlowLayout

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

like image 156
Ugurcan Yildirim Avatar answered Sep 15 '26 11:09

Ugurcan Yildirim


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.

like image 30
Zuhaib Ahmad Avatar answered Sep 15 '26 10:09

Zuhaib Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!