Skip to content Skip to sidebar Skip to footer

Add Multiple Custom Views To Layout Programmatically

If I for example have empty layout like this: layout1.xml

Solution 1:

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

publicclassMyActivityextendsActivity {

    private ViewGroup mLinearLayout;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
        mLinearLayout = (ViewGroup) findViewById(R.id.linear_layout);
        addLayout("This is text 1", "This is first button", "This is second Button");
    }

    privatevoidaddLayout(String textViewText, String buttonText1, String buttonText2) {
        Viewlayout2= LayoutInflater.from(this).inflate(R.layout.layout2, mLinearLayout, false);

        TextViewtextView= (TextView) layout2.findViewById(R.id.button1);
        Buttonbutton1= (Button) layout2.findViewById(R.id.button2);
        Buttonbutton2= (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.

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

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

mLinearLayout.addView(binding.getRoot());

Solution 2:

layout1.xml contains ScrollView as parent layout and main LinearLayout as child if no row item more screen size ScrollView handle overflow item with scroll:

layout1.xml

<ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/my_linear_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"></LinearLayout></ScrollView>

Use LayoutInflater to add row item to parent LinearLayout:

private LinearLayout my_linear_layout;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout1);
    my_linear_layout = (LinearLayout) findViewById(R.id.my_linear_layout);

    for (inti=1; i <= 5; i++) {
        Viewview= LayoutInflater.from(this).inflate(R.layout.layout2, null);
        TextViewbutton1= (TextView) view.findViewById(R.id.button1);
        Buttonbutton2= (Button) view.findViewById(R.id.button2);
        TextViewbutton3= (TextView) view.findViewById(R.id.button3);

        button1.setText("HELLO " + i);
        button2.setText("HELLO " + i);
        button3.setText("HELLO " + i);
        my_linear_layout.addView(view);
    }
}

Solution 3:

Try this method

Add id to LinearLayout 'layout1.xml':

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/firstlayout"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"></LinearLayout>

In onCreate:

LinearLayoutfirstlayout= (LinearLayout) findViewById(R.id.firstlayout);

LinearLayoutsecondlayoout= (LinearLayout) this.getLayoutInflater().inflate(R.layout.layout2, null); // inflating view from xmlTextViewbtn1= (TextView) secondlayoout.findViewById(R.id.button1);
btn1.setText("TEST");

firstlayout.addView(secondlayoout);

Solution 4:

You can achieve this for view binding by using below.

val layoutBindingView: LayoutBinding =
            LayoutBinding.inflate(layoutInflater)
layoutBindingView.textView.text = "TextOne"

val layoutBindingView: LayoutBinding =
            LayoutBinding.inflate(layoutInflater)
layoutBindingView.textView.text = "Text Two"

Post a Comment for "Add Multiple Custom Views To Layout Programmatically"