Android: Programmatically Adding Buttons to a Layout

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 add button based on existing layout

See this similar question with answer.

Try this:

Button btn = (Button) inflater.inflate(R.layout.btn, layout, false);

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

Add button to a layout programmatically

This line:

layout = (LinearLayout) findViewById(R.id.statsviewlayout);

Looks for the "statsviewlayout" id in your current 'contentview'. Now you've set that here:

setContentView(new GraphTemperature(getApplicationContext()));

And i'm guessing that new "graphTemperature" does not set anything with that id.

It's a common mistake to think you can just find any view with findViewById. You can only find a view that is in the XML (or appointed by code and given an id).

The nullpointer will be thrown because the layout you're looking for isn't found, so

layout.addView(buyButton);

Throws that exception.

addition:
Now if you want to get that view from an XML, you should use an inflater:

layout = (LinearLayout) View.inflate(this, R.layout.yourXMLYouWantToLoad, null);

assuming that you have your linearlayout in a file called "yourXMLYouWantToLoad.xml"

how to add buttons programmatically in android

For your case i want to suggest you to use Flow from constraintLayout instead of RelativeLayout. You can learn more by reading this article.

Creating multiple buttons programmatically: Android

First of all, you should use LinearLayout with HORIZONTAL orientation, istead of RelativeLayout , because in Relative all your views will be in the same place (that's why you can see only one Button)

Programmatically adding a grid of buttons inside Relative layout

1.findViewById for RelativeLayout

2.add outer LinearLayout

3.add inner LinearLayout and add Button

4.add Button to the inner LinearLayout

5.add inner LinearLayout to the outer LinearLayout

6.add outer LinearLayout to the RelativeLayout .

And I use in the Activity class .And You can change when you use other class .
Try this .

private RelativeLayout mRelativeLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your);
// findViewById for RelativeLayout
mRelativeLayout = (RelativeLayout) findViewById(R.id.your_relative);
// add LinearLayout
LinearLayout linear = new LinearLayout(this);
linear.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
// set setOrientation
linear.setOrientation(LinearLayout.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));
row.setGravity(Gravity.CENTER_HORIZONTAL);
row.setPadding(0, 40, 0, 0);

for (int j = 0; j < 3; j++) {
Button eBtn = new Button(this);
eBtn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
eBtn.setText("" + (j + 1 + (i * 3)));
eBtn.setId(j + 1 + (i * 3));

eBtn.setBackgroundResource(R.drawable.exercisebutton);
// add view to the inner LinearLayout
row.addView(eBtn);

eBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), ListActivity.class);
id = "" + view.getId();
intent.putExtra(EXTRA_MESSAGE, id);
startActivity(intent);
}
});
}
// add view to the outer LinearLayout
linear.addView(row);
}
mRelativeLayout.addView(linear);
}

Edit

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
</LinearLayout>

Edit

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,10,10,10);
yourBtn.setLayoutParams(layoutParams);

OUTPUT
Sample Image



Related Topics



Leave a reply



Submit