Search code examples
androidandroid-layoutandroid-relativelayoutlayoutparams

Creating a layout Multiple times in Android


How to create a whole layout (Relative/Linear) multiple times in Android? I want the same layout to be created multiple times inside a horizontal scroll view.


Solution

  • You can use RecyclerView for Horizontal scrolling-

    or-

    1. Take horizontal scrollview reference in java code by findViewById.

    2. Create one other xml for view which you want to display multiple

      time.

    3. inflate that view by getlayoutinflator. Create a loop in view.

    4. create a linearlayout at runtime and add those view to it by add view

    5. and add linearlayout to horizontal scroll view. by addview()

    take a idea and modify the below code

    scrollview = findViewByID(scrollview);
    LinearLayout ll = new LinearLayout(this);
    for(your loop){
    View v= getLayoutInflator().inflate(R.layout.xml);
    ll.addView(v);
    }
    scrollview.addView(ll);