Create Linear Layout programmability on button click in android studio

For this You Need two separate layout, one is parent and another one is child layout. In parent there will be only LinearLayout and Another view will consist a custom layout which you want to add on this.

For Example follow this.

layout1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
</LinearLayout>

And some other Layout like this:

layout2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
 
    <TextView
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView 1" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3" 
        android:layout_weight="1"/>
 
</LinearLayout>

You can inflate the layout2.xml file, edit the texts, and add it to the first layout:

public class MyFragment extends Fragment {

        private LinearLayout mLinearLayout;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.yourMainLayout, container, false);
            .
            .
            .
            mLinearLayout = (LinearLayout) view.findViewById(R.id.linear_layout);
            Button fab = (Button) view.findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
              @Override
                public void onClick(View view) {
                  // new elements on click
                  addLayout("This is text 1", "This is first button", "This is second Button");
            }
        });
            addLayout("This is text 1", "This is first button", "This is second Button");
        }

        private void addLayout(String textViewText, String buttonText1, String buttonText2) {
            View layout2 = LayoutInflater.from(getActivity()).inflate(R.layout.layout2, mLinearLayout, false);

            TextView textView = (TextView) layout2.findViewById(R.id.button1);
            Button button1 = (Button) layout2.findViewById(R.id.button2);
            Button button2 = (Button) layout2.findViewById(R.id.button3);

            textView1.setText(textViewText);
            button1.setText(buttonText1);
            button2.setText(buttonText2);

            mLinearLayout.addView(layout2);
        }

You may want to change android:layout_height of the layout2.xml root view to wrap_content.

Here I haven’t taken the Button in XML LAYOUT, you can put the Button according to your need.

If you are using ViewBinding, here is how it would look like for the addLayout function :

MyLayoutBinding binding = MyLayoutBinding.inflate(getLayoutInflater(), mLinearLayout, false);
binding.getTextView1().setText(textViewText);
binding.getButton1().setText(buttonText1);
binding.getButton2().setText(buttonText2);

mLinearLayout.addView(binding.getRoot());

In your Case you can call this method from onClick() of Add Button

addLayout("This is text 1", "This is first button", "This is second Button");

Leave a Comment