user3898351
user3898351

Reputation: 23

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.

Upvotes: 1

Views: 2447

Answers (2)

T_V
T_V

Reputation: 17580

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

Upvotes: 1

Deniz
Deniz

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

Related Questions