Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add to horizontalscrollview

I've followed a few tutorials online that show you how to create a static horizontalscrollview with multiple xml files.

However I would like to be able to grab content from a database, fill a new view (from a stock xml layout) with the content and then add it to the horizontalscrollview.

Are there any tutorials that show you how to add dynamic views to a horizontalscrollview?

like image 437
phalt Avatar asked Apr 20 '12 13:04

phalt


2 Answers

That is easy,

Your HorizontalScrollView must contain a container like a LinearLayout or a RelativeLayout, grab an instance to that Layout in your activity, and add the views as required...

LinearLayout yourLayout = (LinearLayout)findViewById(R.id.someID);

and then iterate through the number of items in your database and keep adding the views to your Layout until end like this...

for (int i = 0; i < yourData.size(); i++) {             
  TextView tv = new TextView(getApplicationContext());
  tv.setText(yourData.get(i));
  yourLayout.addView(tv);
}
like image 103
Arif Nadeem Avatar answered Nov 15 '22 18:11

Arif Nadeem


R.layout.column is another layout that you want to add.

<HorizontalScrollView ...>
   <LinearLayout android:id="@+id/row" ..>
      ...
   </LinearLayout>
</HorizontalScrollView>

LinearLayout featureLayout = (LinearLayout) View.inflate(YourActivity.this, R.layout.column, null);
row.addView(featureLayout);
like image 39
MAC Avatar answered Nov 15 '22 18:11

MAC