Home » Android » layout – How to add a button control to an android xml view at runtime?

layout – How to add a button control to an android xml view at runtime?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have an android xml layout, main.xml. I would like to add controls to this layout at runtime (I would like to add a series of additional linear layouts that contain buttons controls). Can I do that and if yes, how?

Thanks

How to&Answers:

I see the error u r doing here

LinearLayout mainLayout = (LinearLayout) findViewById(R.layout.main);

You r taking the layout as Linearlayout object, you should take the LinearLayout id

Try this

LinearLayout lnr = (LinearLayout) findViewById(R.id.LinearLayout01);

Button b1 = new Button(this);

b1.setText("Btn");

lnr.addView(b1);

Answer:

You can add controls programmatically if you want in your code, or even another XML with a View and an Inflater.

Here you can read the basics: http://developer.android.com/guide/topics/ui/declaring-layout.html

Answer:

Ok, I have got it to work.

The steps are the following:
First inflate the xml layout, ie,

View view = View.inflate(this, R.layout.main, null);

Then instantiate the container object from the xml layout into a ViewGroup class, ie,

ViewGroup container = (ViewGroup) view.findViewById(R.id.myContainer);

Then create a linearLayout object, create and add onto that any controls needed, add the linearLayout to the container object and use setContentView on the view object, ie,

container.addView(buttonsLayout);
this.setContentView(view);

Answer:

You can do this quite easy by setting an id on the layout on which you want to add views to. Say your main.xml look like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <TextView android:id="@+id/label"
      android:layout_width="fill_parent"/>
  <LinearLayout android:id="@+id/container"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
  </LinearLayout>
</LinearLayout>

Lets assume that you want to add your additional views to the LinearLayout with id id/container. In your onCreate method you could retrieve that object for later use:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContainer = (ViewGroup)view.findViewById(R.id.container);
}

Now you are all set to add other views to your container ViewGroup:

LinearLayout theButtons = getButtons()
mContainer.addView(theButtons);

In the getButtons method you need to create your LinearLayout containing the buttons you need. Either you do this programmatically or by inflating a view defined in an XML file. See LayoutInflater.inflate.

Answer:

just try this:

LinearLayout mainLinearLayout = (LinearLayout) findViewById(R.layout.llmain);

now create button dynamically like this

 Button btn1 = new Button(this);
 btn1.setText=("Button 1");
 mainLinearLayout .addView(btn1);

now if you want to add onether linearlayout then add it below button then

 LinearLayout llinner = new LinearLayout(this);

 Button btn2 = new Button(this);
 btn2.setText=("Button 2");
mainLinearLayout .addView(btn2);

llinner.addView(btn2 );

mainLinearLayout .addView(llinner);

Answer:

Try this :

LinearLayout ll =(LinearLayout)findViewById(R.id.linlay);
Button b = new Button(this);
b.setText("Hello");
l.addView(b);

This might help you