How to Programmatically Add Buttons into Layout One by One in Several Lines

How do I programmatically add buttons into layout one by one in several lines?

The issue is that your buttons are not going to automatically wrap to the next part of the screen. You have to specifically tell Android how you want your Views to be positioned. You do this using ViewGroups such as LinearLayout or RelativeLayout.

LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical"

for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

for (int j = 0; j < 4; j++) {
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
btnTag.setText("Button " + (j + 1 + (i * 4)));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}

layout.addView(row);
}

I'm assuming that R.id.linear_layout_tags is the parent LinearLayout of your XML for this activity.

Basically what you're doing here is you're creating a LinearLayout that will be a row to hold your four buttons. Then the buttons are added and are each assigned a number incrementally as their id. Once all of the buttons are added, the row is added to your activity's layout. Then it repeats. This is just some pseudo code but it will probably work.

Oh and next time be sure to spend more time on your question...

https://stackoverflow.com/questions/how-to-ask

Programmatically adding three buttons in line in a LinearLayout

ll.setOrientation(LinearLayout.HORIZONTAL);

Lets you set buttons in horizontal line allignment.

But to give balanced space to all three buttons. you must set weight property for all the three Button objects to 1.

Edit:

Do this for all buttons.

LinearLayout.LayoutParams params = button.getLayoutParams();
params.weight = 1;
button.setLayoutParams(params);

to set weight for all buttons.

Regards,
Aqif Hamid

Add buttons dynamically

On the Android platform, a Button is a specific widget that is intended to perform a specific action when clicked. You describe a need for a dynamic number of buttons in a list format, for this you should use a RecyclerView, which would allow you to bind data to the number of clickable items.

If you want the appearance of each item in the RecyclerView list to look like a button, you can style the XML row item to look like one by inheriting the style from the built-in Button widget. This question asks how to style items with a background to provide Button-like visual states.

Adding multiple buttons on the fly

public void AddAll() {
final RelativeLayout rl = (RelativeLayout)findViewById(R.id.rel);

for(int i=0;i<4;i++)
{
final Button btn = new Button(this);
rl.addView(btn);
btn.setText("hello");

btn.setWidth(320);
btn.setHeight(40);
}
//////////////////////////////////////////////////////////
}

For more detail :-

How do I programmatically add buttons into layout one by one in several lines?

or

 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollView= new ScrollView(this);
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
for (int i=0; i<10; i++){
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setTag(i);
TextView tv=new TextView(this);
tv.setText("Row " + i);
ll.addView(tv);
Button b = new Button(this);
b.setTag(i);
b.setText("Button " + i);
ll.addView(b);
mainLayout.addView(ll);
}
scrollView.addView(mainLayout);
setContentView(scrollView);
}

How to set multiple buttons in Relative Layout in multiple lines

If you are creating the root layout dynamically you have to set root layout's layoutParams dynamically.

LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

ll.setLayoutParams(layoutParams);

and add the views to this layout

Button btn=new Button(this);
ll.addView(btn, layoutParams);

didn't tested it please try this code

Update

 RelativeLayout lLRoot = (RelativeLayout) findViewById(R.id.container);

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);

lLRoot.addView(ll);

Button btn=new Button(this);
btn.setText("hai");
ll.addView(btn);

Dynamically adding buttons into a table or grid like form layout programmatically

You are adding tr twice to the rLayout5. Delete the line where your error is....

Android - How to add buttons programmatically depending on screen size and button below each other dynamically?

Your buttons overlapped each others because your layout params is not appropriate:

params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
b.setLayoutParams(params);
b.setText("Bouton n°" + numBoutton);

With above code, all the buttons will align left with parent.

If you want to add buttons next to others, you should better use LinearLayout instead of RelativeLayout.

Suppose that you already have a linear layout with horizontal orientation.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
b.setLayoutParams(params);
b.setText("Bouton n°" + numBoutton);
mainLinearLayout.addView(b);
numBoutton++;

That's all.

Edit: Here are some code, dont know if this is your need or not.

In this app, i have a LinearLayout inside a ScrollView and a Button. When i click the Button, i add new button to the ScrollView.

The MainActivity.java

package com.tiennt.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

private LinearLayout llButtons;
private int buttonCount;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btnAdd = (Button) findViewById(R.id.btn_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doAddButton();
}
});
llButtons = (LinearLayout) findViewById(R.id.ll_buttons);
}

private void doAddButton() {
Button button = new Button(this);
button.setText("Button " + ++buttonCount);
button.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
llButtons.addView(button);
}
}

The activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.tiennt.myapplication.MainActivity">

<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD BUTTON"
android:layout_gravity="center_horizontal" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/ll_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />

</ScrollView>

</LinearLayout>

Here are the result:
Result



Related Topics



Leave a reply



Submit