Hassaan Rabbani
Hassaan Rabbani

Reputation: 2465

Add Layout programmatically android

I am working on an application that will get images from SD-Card and will respectively show them in the layout like this

enter image description here

The Issue is:

Lets say the user has 100+ files in the SD-Card, to show them in the layout i need to add first 100+ views and then i would do the styling. Wrong approach

My Question is:

What can be done either progamatically or through XML so that i could define only five basic views and depending upon the number of images in the SD-card, it creates the remaining Layouts and views so that i can access them through horizontal scroll view.

My research includes:

I tried using the Grid view layout but it does not full fill my needs as it has a uniform size for all grids. more over i know how to add a layout programmatically but i do not know how to make it work with my design.

Upvotes: 4

Views: 180

Answers (2)

Payal
Payal

Reputation: 913

Use this approach :-

1.) Put the path to all images in an arraylist.

2.) create a for loop that will run till size of arraylist.

3.) In this loop, create your imageview using code.

ImageView im = new ImageView(this);

4.) to Show image, in src attribute add the path from arraylist.

You need to use some extra layout params to design according your needs.

Upvotes: 0

Shani Goriwal
Shani Goriwal

Reputation: 2104

You can make it by changing you xml both side scrollable as-

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320px" android:layout_height="fill_parent">

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linlay" android:layout_width="320px"
        android:layout_height="fill_parent" android:stretchColumns="1"
        android:background="#000000"/>

</HorizontalScrollView>

Upvotes: 1

Related Questions