Search code examples
android-intenthorizontalscrollview

Adding layout to HorizontalscrollView


I am trying to create a horizontalscrollview which is supposed to hold a few views that are scrollable horizontally.

The initial view is a horizontal scroll view with a linear layout inside it

I have another view, something like a screen that shows weather for a city.

If i have 5 cities in my database, i would like to add 5 instances of the view into the horizontalscroll view. And each instance has its own events such as clicks and http requests.

I can create a horizontalscrollview from the xml, but i would like to do it dynamically. I have been looking around for some examples to try to get a starting point, but nothing seems to be out there.

Can somebody point me to the right direction?

This is my horizontalscrollview xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
        <RelativeLayout android:layout_height="50dp"
            android:id="@+id/TableRow01" android:layout_width="fill_parent"
            android:background="#82db7a">
            <ImageView android:id="@+id/imageView1" android:src="@drawable/logo_topbar"
                android:layout_height="wrap_content" android:layout_width="wrap_content" />
            <ImageButton android:background="@drawable/topicon_btn"
                android:src="@drawable/btn_icon_locateme" android:layout_gravity="center_vertical"
                android:layout_height="45dp" android:id="@+id/locatemebtn"
                android:layout_width="wrap_content" android:layout_centerVertical="true"
                android:layout_alignParentRight="true" android:layout_margin="5dp" />
            <ImageButton android:background="@drawable/topicon_btn"
                android:src="@drawable/btn_icon_star" android:layout_height="45dp"
                android:id="@+id/favbtn" android:layout_width="wrap_content"
                android:layout_toLeftOf="@+id/locatemebtn" android:layout_alignTop="@+id/locatemebtn"
                android:layout_alignBottom="@+id/locatemebtn" />
        </RelativeLayout>     
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:fillViewport="true"
    android:id="@+id/scrollView1" android:layout_height="fill_parent"
    android:layout_width="fill_parent" >
    <LinearLayout android:layout_width="fill_parent"  android:layout_height="fill_parent" android_id="@+id/mainScroll">


    </LinearLayout>
</HorizontalScrollView>
</LinearLayout>

This is how i tried to add a view (just to test if it works, though i am not sure even if it works, the view is just a layout or layout with functionality)

LinearLayout mainscroll=(LinearLayout) findViewById(R.id.mainScroll);
         LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         View view = vi.inflate(R.layout.weather_tab, null);
         mainscroll.addView(view);

Since a scrollview can only have one child, i added a linearlayout inside (not sure if it should be linear layout), and i want to add all the instances of my views into the linear layout.

P.S: i Have done the same application for iphone without too much pain. Android just seems to confuse me too much :(


Solution

  • Well,it just happens to be an xml error, the android_id missed my eye, the linear layout does not take it, but was surprised it did'nt throw an error