Add Layout programmatically android

I am working on an application that will receive images from an SD card and accordingly show them in a layout like this

enter image description here

The Issue is:

Suppose the user has 100+ files on the SD card to show them in the layout, I need to add the first 100+ views, and then I will do the styling. Wrong approach

My Question is:

What can be done either progamously or through XML so that I can define only five main views and depending on the number of images on the SD card, it creates the remaining layouts and views so that I can access them through a horizontal scroll view.

My research includes:

I tried to use the Grid view layout, but it does not fill my needs, since it has the same size for all grids. Moreover, I know how to add a layout programmatically, but I don’t know how to make it work with my design.

+4
source share
2 answers

You can do this by changing the xml on both sides to scroll -

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

+1
source

Use this approach: -

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

2.) create a for loop that will run up to the size of an arraylist.

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

ImageView im = new ImageView(this);

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

.

0

Source: https://habr.com/ru/post/1524258/


All Articles