0

I am creating many button from JSON using java code

 Relativelayout rl = (Relativelayout) findById(R.id.layout_productos); ... JSONArray jsonArray = new JSONArray(tmp.getString("productos")); Button bt[] = new Button[jsonArray.length()]; for (int i = 0; i < jsonArray.length(); i ++){ final float scale = getResources().getDisplayMetrics().density; int padding_40dp = (int) (40 * scale + 0.5f); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, padding_40dp); bt[i] = new Button(DetalleServicioActivity.this); bt[i].setText(jsonArray.getJSONObject(i).getString("nombre")); bt[i].setTag(jsonArray.getJSONObject(i).getString("id_producto")); bt[i].setTextColor(Color.parseColor("#FFFFFF")); bt[i].setBackgroundColor(Color.parseColor("#D8D8D8")); bt[i].setLayoutParams(params); bt[i].setEnabled(false); bt[i].setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); rl.addView(bt[i]); } 

inside Relativelayout

 <RelativeLayout android:id="@+id/layout_productos" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" > </RelativeLayout> 

The problem is when is run the app, the buttons are created one upon another one.

2 button (Chilena and cuenta rut)

But I need that they are located one next to the other and when the screen of a line jump approaches the edge to continue

How can I do that??

1
  • You might want to consider a FlowLayout instead (it's not a standard Android component -- search Google for it) Commented Jan 24, 2015 at 21:16

1 Answer 1

1

You need to add rule in order to have them aligned next to eachother. Rules are up to you.

params.addRule(RelativeLayout.RIGHT_OF, previousViewId); 
Sign up to request clarification or add additional context in comments.

2 Comments

but, what happend when the button touch the edge of the screen?
If you want to use relativelayout, you also need to consider this and add another rule such as below etc. But this is not a good idea. Instead you can use recyclerview which has staggered options. That will be a good solution for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.