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.
Asked
Active
Viewed 2,368 times
1
-
[this](http://stackoverflow.com/questions/4203506/how-can-i-add-a-textview-to-a-linearlayout-dynamically-in-android) link should be usefull – Piotr Golinski Apr 24 '15 at 09:38
-
you need to provide what you have been working on, so answerers can look at it help you with it, but what about inflating a view from resources or creating it programmatically that is hard? – Elltz Apr 24 '15 at 09:38
-
1Why not use a ListView or RecyclerView, with each row containing the relevant layout? – PPartisan Apr 24 '15 at 09:43
-
I have a relative layout containing a list view and two text views i want to dynamically create the whole relative layout dynamically for multiple times. – user3898351 Apr 24 '15 at 09:45
-
1Can you post what you already did? – CAS Apr 24 '15 at 09:46
-
Thanks for your help .I got it by using the idea in the comment below .. – user3898351 Apr 24 '15 at 10:38
2 Answers
1
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
- 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);

T_V
- 17,440
- 6
- 36
- 48
-
Hey got it finally using your idea.. Thanks for the idea it works great. – user3898351 Apr 24 '15 at 10:37
1
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" />

Deniz
- 12,332
- 10
- 44
- 62