Add multiple custom views to layout programmatically

70,835

Solution 1

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

public class MyActivity extends Activity {

    private ViewGroup mLinearLayout;

    @Override
    protected void onCreate(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");
    }

    private void addLayout(String textViewText, String buttonText1, String buttonText2) {
        View layout2 = LayoutInflater.from(this).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.

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());

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android: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;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout1);
    my_linear_layout = (LinearLayout) findViewById(R.id.my_linear_layout);

    for (int i = 1; i <= 5; i++) {
        View view = LayoutInflater.from(this).inflate(R.layout.layout2, null);
        TextView button1 = (TextView) view.findViewById(R.id.button1);
        Button button2 = (Button) view.findViewById(R.id.button2);
        TextView button3 = (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':

<LinearLayout xmlns: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:

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

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

firstlayout.addView(secondlayoout);
Share:
70,835
user3304086
Author by

user3304086

Updated on November 19, 2021

Comments

  • user3304086
    user3304086 over 2 years

    If I for example have empty layout like 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>
    

    I would like to add layout2.xml to layout1.xml programmatically. I would need to have multiple different version layout2.xml which I would add when I want. Each one would have different text on TextView and Buttons.

    In case of regular Android View I would usually do it similarly like this with addView:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        TextView tv1 = new TextView(this);
        tv1.setText("HELLO");
        my_linear_layout.addView(tv1);
    
        Button btn2 = new Button(this);
        bt2.setText("WORLD");
        my_linear_layout.addView(btn2);
    }
    

    How can I do it if I don't have something simple like TextView or Button, and if I have my own XML defined View?

    Would it be possible to somehow put all the views inside adapter like for a ListView and then get it when needed and just call addView on those Views?

  • Anand Savjani
    Anand Savjani about 8 years
    how to access all button outside the view ?
  • Haresh Chhelana
    Haresh Chhelana about 8 years
    Try to get child from my_linear_layout or add all buttons reference one array list.
  • Dr. aNdRO
    Dr. aNdRO over 7 years
    Specified child already has a parent please call removeView()
  • Patrick
    Patrick over 2 years
    How can I do this with view binding?
  • pdegand59
    pdegand59 over 2 years
    @Patrick check the edited answer
  • Bharat Lalwani
    Bharat Lalwani over 2 years
    Please see below my answer.