TuGordoBello
TuGordoBello

Reputation: 4509

How I can create buttons one next to the other inside Relativelayout usigin Java code?

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??

Upvotes: 0

Views: 91

Answers (1)

Nathua
Nathua

Reputation: 8826

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);

Upvotes: 1

Related Questions