Search code examples
androidandroid-studiohorizontalscrollview

Adding Image views to the horizontal scroll dynamically (through code)


I just started working on android a day ago and I'm working on scrolls. I have already made one but I would now like to do the same thing dynamically.

This is the code for my activity_main.xml

<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="horizontal">

    <LinearLayout
        android:id= "@+id/linearlayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:onClick="onTouch">


    </LinearLayout>

</HorizontalScrollView>




<LinearLayout
    android:id="@+id/bottomlinear"
    android:layout_width="match_parent"
    android:layout_height="400px"
    android:gravity="center"
    android:background="#00ffff"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="46dp">>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/drop"
        android:textSize="30sp"
        android:text="Drop Zone" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Total"
        android:textSize="20sp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Success"
        android:textSize="20sp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Fail"
        android:textSize="20sp" />

</LinearLayout>

Basically I would like to add 10 images from the drawable into the horizontal scroll as image views dynamically. Any help or ideas are much appreciated.


Solution

  • I solved the problem. I'm gonna post the solution hoping that it helps someone else having a similar issue. I originally made 3 horizontal scroll views and the xml for one of them is like this

        <HorizontalScrollView
        android:id="@+id/HorizontalScrollView1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginBottom="1dp"
        android:background="#FFF"
        android:scrollbars="none">
    
        <LinearLayout
            android:id="@+id/imgLayout1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">
    
        </LinearLayout>
    
    
    </HorizontalScrollView>
    

    The code I wrote to create Image Views inside Linear Layout of horizontal scroll view:

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
       for (int j=1; j<=10; j++)
        {
            b1=j;
            create_img1("drawable/a"+j, b1);
        }
    
    
    }
    
    void create_img1(String ss, int ID)
    {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout1);
        LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(200, 200);
        parms.gravity = Gravity.CENTER;
        parms.setMargins(20, 20, 20, 20);
        final ImageView imageView = new ImageView(this);
        imageView.setLayoutParams(parms);
    
        int id = getResources().getIdentifier(ss, "id", getPackageName());
        imageView.setImageResource(id);
        linearLayout.addView(imageView);
        imageView.setId(ID);
    }
    

    I made one for multiple scroll views with drag and drop functionality but I filtered it out and if you want to create image views inside scroll views dynamically, this is what you are looking for. Hope this helps someone else with a similar issue.