Predrag Beocanin
Predrag Beocanin

Reputation: 1400

Dynamic button layout android

I'm trying to build buttons via xml layout, depending on parameters grabbed from the server (x/y coords mostly). Best I got so far requires me to update the app each time I want a new layout, but I'm trying to find a more efficient way. So the number of actual buttons and their position is relative to the data grabbed from the database. How would I go on about doing this?

Upvotes: 0

Views: 52

Answers (1)

18446744073709551615
18446744073709551615

Reputation: 16832

To move buttons at run-time, use an empty <View /> whose size is set programmatically; the buttons will be, say, below it. (Works both for <LinearLayout ...> and <RelativeLayout ...>). Use ViewTreeObserver.OnGlobalLayoutListener and getMeasuredWidth() to get the actual sizes.

Note that you can do really a lot by just making some buttons/views invisible (someView.setVisibility(View.GONE), the other two possible values are View.VISIBLE and View.INVISIBLE). If possible, you should prefer this approach because it allows you to have a more or less sane layout under any circumstances.

Upvotes: 1

Related Questions