Android: how do I implement the following layout? - android

I want to have this layout in my Android app.
Requirements:
All rectangles should distribute evenly over the entire screen
The two rectangles on the bottom right should have the same height (so also distributed evenly)
I tried GridLayout but that won't let me stretch the children across the screen. I tried RelativeLayout without success. I tried nesting LinearLayouts with weights, it works, but I read that nesting weights is bad for performance. And finally, I tried TableLayout with weights, but it does not have something like "row span" like an HTML table so I cannot do the two rectangles in the bottom right corner like I want them to be (without nesting weights again).
Somebody, please help! How hard can it be? I don't know what to do here. I could implement my own ViewGroup or something to manually implement the layout. But it feels like this is something very simple, so I should be able to use standard layouts.

You are able to use GridLayouts to do such a thing, for some help fitting the screen I suggest you look at the following: How to make a GridLayout fit screen size.
Depending on what you are doing, nested weights with a series of Linear Layouts will not harm your performance too much, however it is is something extremely large like an intricate game or something of that nature I suggest using the GridLayout.

Related

Is there any determining factor for selecting a specific layout in Android?

I'm familiar with almost all the basic layouts in Android & understand when they are to be used. I know that a RelativeLayout is to be used when elements in the UI are to positioned relative to each other, that a LinearLayout is to be used when UI elements are to be displayed vertically or horizontally. So I was wondering if the ease of development was the only factor that determined what layout should be selected or if there was any performance factor involved. I mean I can lay out two ImageViews vertically in Android using both LinearLayout and RelativeLayout, so why use a particular layout then?
There are some performance gains for avoiding certain things, such as nesting RelativeLayout (see this question). Also, the docs recommend making layouts shallow and wide, which can facilitated with RelativeLayout, again for performance reasons.
However, often thinking about such things will be premature optimization for simple layouts, and you should use whatever makes the most sense for the situation.

Is there a layout in android to fit an arbitrary number of children views of varying sizes in the parent view to fit

I want to acheive a look something like in this mock up but as far as i can tell there are no ways to do it without a whole new custom view. The children are going to have different sizes and they need to fit in the parent. I was looking at the gridlayout, but in the grid layout you seem to have to specify postition and size.. where the children end up in the parent view is ireelevant but their sizes are important.
I have used RelativeLayout with nested RelativeLayouts before for something similar. It looked good, but it was not the simple, dynamic solution you are looking for.
I would look into using Canvas to give a more custom feel with more flexibility (randomness), but then you'll be working more programmatically. This gives you the ability to change their sizes whenever the app is opened.

Android is it better to use a TableLayout or GridView

I am developing an app and I would like to have 12 ImageButtons (3 x 4). centered in the android screen and the center section of the screen where the ImageButtons are located to have a color or gradient background.
Is it best to do this with a TableLayout or a GridView... or maybe something else. I am a newbie to Mono and Android so any help is appreciated. I was hoping to do most of the layout in the designer, but I am not sure of the best approach.
Can anyone point me in the right direction?
I think GridView is better to use as it supports recycling of views. In case of TableLayout all the views remain in memory.
Even if the no of buttons increases, GridView will take care of it.
Make sure you are implementing view recycling correctly, or otherwise you may have a memory leak.
If there are only 12 buttons, a TableLayout would probably be simpler to implement because you don't have to mess with the adapter. If you need scrolling or the number of buttons may increase over time, you would probably be better off going with the GridView.
You may want to look into the GridLayout in the Android Support library v7. It is kind of like the TableLayout but seems simpler to do things like just wrap button into a set number of columns.

Android wallpaper program layouts

When I look at several of the more popular android walpaper programs, (HD Wallpapers and the like), they all seem to have a very similar layout with 4 pictures in a 2 x 2 grid where each takes up approximately 1/2 of the parent's respective width and height. After not having a lot of success emulating this with various combinations of layouts and objects, I am trolling for a pointer. Anyone who could get me pointed in the right direction would be greatly appreciated. Is there a standard mix to achieve this sort of view?
Side note: Please be gentle, it's my first time... asking a question. ;)
You could use a GridView with android:numColumns=2.
You can see a GridView example here. You would set the height by manipulating the LayoutParams on the views that you put inside the GridView (via its Adapter).
If that doesn't float your boat and you're not doing much scrolling, you can always create a TableLayout with TableRows of 2 Views apiece, or a vertical LinearLayout containing two horizontal LinearLayouts of 2 Views apiece.
What have you tried already? Can you expand on your use case a little bit more? Do the images need to scroll? Can they be static? Will there be more than 4 images on a page?

Android TableLayout or GridView?

I want to create a grid of words approximately three wide and eight deep. I would like to be able to select some of the words and do a fade animation on them. I would also like to know when a word has been selected. I have been looking at TableLayout and GridView to do this. Would one of these be better than the other to do this? Is one of them more adaptable to the different screen sizes in Android than the other?
I've played around a bit with tablelayout (and tablerow) and it will definitely do the trick, I guess more importantly the question is whether you expect to have a fixed number of columns or not ("approximately" doesn't help...). If so and you have very good control of what goes in the table/grid, tablelayout may very well be what you're looking for (I just find it simpler, but maybe I'm wrong).
They both can adapt to screen size with the right layout instructions, and proper programming will give you similar results.
But then, if you allow the screen to rotate, you may want to use gridview
parameters, parameters, parameters...
I think a TableLayout would be more easy to use. For a GridLayout you need to build custom adapters and so one which result in a more complicated application.
Looking at different sceensizes, a GridLayout will choose a good number of columns and rows by itself according to the content and it will be more flexible with adding items.
Also the GridLayout will be more flexible but it is some more work to make.

Categories

Resources