Reputation: 23
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.
Upvotes: 1
Views: 2447
Reputation: 17580
You can use RecyclerView
for Horizontal scrolling-
or-
Take horizontal scrollview reference in java code by findViewById
.
Create one other xml for view which you want to display multiple
time.
inflate that view by getlayoutinflator
. Create a loop in
view.
create a linearlayout at runtime and add those view to it by add view
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);
Upvotes: 1
Reputation: 12530
Either you need to add inflated child views to the root view like below
RelativeLayout rootView = (RelativeLayout)findViewById(R.id.rootView);
View child = getLayoutInflater().inflate(R.layout.child, null);
rootView.addView(child);
OR you can define and include that layout multiple times inside other.
Check this link http://developer.android.com/training/improving-layouts/reusing-layouts.html
Include your reusable layout like this
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/reusabelLayout" />
Upvotes: 1