Adding Views Programmatically: RelativeLayout vs. GridLayout - sahajss/knowledge_base GitHub Wiki

A big problem I ran into recently was how I can add imageViews programmatically to the layouts when I wanted to display my table. Before I used pictures, though, I displayed the little android icon so that I could see if it was working without going through the hassle of selecting frames.

#GridLayout Obviously, my first choice was to turn to GridLayout to help me do so since it would be the simplest to implement. Theoretically, every view would have an x- and y- value and the grid would display as we would expect: in a grid. Also, I could programmatically set the dimensions of the grid to display any number of frames. I found some code on StackOverFlow that I tried to implement into my project, but it only served to display the icons in a strange manner that I can’t explain.

#RelativeLayout After I showed Mr. Kosek the effect of using GridLayout, he suggested that I should try using RelativeLayout because GridLayout was designed to be rigid. Again, I found code on StackOverFlow that helped me do so. Even though it is a lot more to write, it is much easier to understand because, as with the activity_main.xml file, I just set all the parameters for the view, but this time using the RelativeLayout.LayoutParams class. I then set the layout parameters to the imageView and add it to the RelativeLayout.

However, I again ran into trouble because setting the id of the views to a variable makes them overlap and not display correctly, so I’m still working on finding a solution for that.