1

Inside GridLayout I have 32 very similar FrameViews, the difference are ids (numbers)

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myFrame1">
        <ImageView
            android:id="@+id/myImg1"
            android:layout_width="190dp"
            android:layout_height="105dp"
            android:layout_margin="4dp"
            android:src="@drawable/myImg"
            android:contentDescription="@string/station1" />
        <TextView
            android:id="@+id/descTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="4dp"
            android:layout_marginBottom="34dp"
            android:background="@android:color/transparent"
            android:text="@string/Station 1"
            android:textColor="@android:color/black"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginStart="4dp" />
    </FrameLayout>

I might add more text views later and it's starting to get pretty messy so I need a general idea of how to insert this kind of layout dynamically, preferably in Kotlin language.

GeoCap
  • 505
  • 5
  • 15

1 Answers1

1

I have created a full project in which I am inserting frame dynamically.

First You have to create layout frame_layout.xml. In this layout paste this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myFrame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/myImg"
        android:layout_width="190dp"
        android:layout_height="105dp"
        android:layout_margin="4dp"
        android:contentDescription="station1"
        android:src="@drawable/ic_launcher_foreground" />

    <TextView
        android:id="@+id/descTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginBottom="34dp"
        android:background="@android:color/transparent"
        android:text="Station 1"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textStyle="bold" />
</FrameLayout>

In activity layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <androidx.gridlayout.widget.GridLayout
        android:id="@+id/gridRoot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:columnCount="2"
        app:rowCount="32"
        >

    </androidx.gridlayout.widget.GridLayout>

</ScrollView>

And MainActivity.kt:

import android.os.Bundle
import android.widget.ScrollView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.frame_layout.view.*

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        for (i in (1..32))// numbers of frames
        {
            val frame = ScrollView(this) // create frame
            layoutInflater.inflate(R.layout.frame_layout, frame) // add layout to frame

            frame.tag = i.toString() //add tag to enable finding specific frame at runtime

            frame.descTextView.text = "Section $i" // change textView on frame ( use string resources! not hardcoded string like me)

            gridRoot.addView(frame) // add frame to grid 
        }

    }
}

Result:

Result

If this works fine You can edit it and make it looks like You want.

iknow
  • 8,358
  • 12
  • 41
  • 68
  • It works good, thanks. May I ask how I could remove them all afterwards, through code. – GeoCap Sep 04 '20 at 14:35
  • [I think this will help](https://stackoverflow.com/questions/3995215/add-and-remove-views-in-android-dynamically). If You want to delete all just iterate from all children of `gridRoot` and remove them – iknow Sep 04 '20 at 15:30
  • Also I'm having trouble finding specific frame at runtime. I tried with frame.findViewWithTag("1") but it doesn't seem to wrok – GeoCap Sep 04 '20 at 15:46
  • It seems I can only access the view with last tag and other tags don't work. – GeoCap Sep 04 '20 at 17:32
  • Strange :/ Can You iterate for every view in `gridRoot` and log `tag`? – iknow Sep 04 '20 at 17:34
  • Oh yes I can. I should've called findViewWithTag on gridRoot and not on frame. – GeoCap Sep 04 '20 at 17:48