How to create matrix of buttons to cover the entire screen - android

How to actually create a matrix of buttons to cover the entire screen programmatically.
What I tried : I am currently able to create buttons but I want them to occupy the entire screen area, they are just occupying top left part

You can use a TableLayout or LinearLayout too assigning weights in your Buttons.
Let's use a LinearLayout. This can be done both in xml and in your class. Let's create a 2 by 2 matrix of buttons.
Add a LinearLayout with any orientation (let's use horizontal)
Add 2 LinearLayouts inside with opposite orientation of number 1 (in this case we use vertically. Then we set the width=0dp and layout_weight=1 for both to occupy equal spacing)
Now we can add our buttons inside the vertical oriented LinearLayouts and set their height=0dp and layout_weight=1 for them to have equal sizes.
That's it. using the TableLayout is a cheaper way if you have more rows and columns

Related

How to set multiple scroll views size to change dynamically relative to each other?

I have two scroll views in a vertical linear layout.
I want them to be relative to each other so that they fill the entire linear layout and compensate if one cant cover half the screen.
Lets call that scroll views TOP and BOT.
If the screen can display 4 rows and both scroll views have infinite rows, each scroll views should display 2 rows and be able to scroll down to se the rest rows.
If TOP has 1 and BOT infinite rows, BOT should be resized to 3/4 of the linear layout.
If TOP have infinite and Bot has 1 row TOP should still just display 2, i.e. it should never pass the linear layouts vertical center.
Here are some pictures for reference:
my setup with weight set to 0.5/0.5.
result of 0.5/0.5 weight. Notice the gray bar above the BOT title bar. This empty space should be filled by the BOT bar.
if Using fixed size or wrap content the TOP will push the Bot out of view.
How can I have them hugging each other and still set TOP to a maximal height?
Preferable in XML.
Its better to set the weight dynamically. Count the number of items in both views. Set the weight of each view according to the ratios of their number of items. You can refer set weight dynamically for setting weights at run time

Android: how define android:layout_span for height

Suppose there are two Button in TableLayout and if I mention android:layout_span="1" in both the button then the complete width is divided into two part and each button sit into one part.
Can we achieve this same thing for height.
In my application four button is there which arranged in linear order one on the top of other.
I want all these four button cover complete height of the screen in any size of screen.
My Problem is here if I launch this screen on one device then it look like:-
But if I launch this layout on big screen then it looks like:-
How on all the screen the button cover(uniformly distribute)?
Set the layout height for each view to be 0 and instead use layout weight. Use the value 1 for layout weight for all the buttons for them to be equally spaced!

Regarding Layouts

as i am new to Android ... I have a small doubt on Layouts?
which layout is better and preferable among all(i think there are 2 layouts if my guess is not wrong RelativeLayout,LinearLayout) layouts.
There are more then 2 layouts, used by needs:
FrameLayout : Layout that acts as a view frame to display a single object.
RelativeLayout :Enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent).
LinearLayout :A layout that organizes its children into a single horizontal or vertical row. It creates a scrollbar if the length of the window exceeds the length of the screen.
TableLayout :A tabular layout with an arbitrary number of rows and columns, each cell holding the widget of your choice. The rows resize to fit the largest column. The cell borders are not visible.

Designing a layout with multiple ImageButtons and Buttons

I have to make a layout like in the figure below (Each block in the 3 x 3 grid would be a ImageButton and each of the ImageButton has text and a small image in the bottom):-
I think i can use a TableLayout, but the problem is that I want to fit all the components in a single screen without any extra scrolling. When I used LinearLayout, the imageButtons where overlapping each other, how do I avoid it?
I want the ImageButtons to fill up the width of the screen, but match_parent is not helping. Should I fix the size of the ImageButtons. But I want the layout to work in all the screens, not specifically in only one.
If I include the buttons also in the TableLayout (as a separate rows), then they are of the size of the ImageButton, wrap_content is not effective. And I dont know how to place them in the centre.
Please suggest something. Any other layout suggestions? I am not using GridLayout because the App is for GingerBread (Android API 10).
Thank you.
I would use:
- Linear layout, orientation: vertical - as an external layout
- Linear layouts, orientation: horizontal - for each line. Set layout weight for any of the 3 "items" in it to be 33.
Each item consisting of: image button, text and image - a linear layout, orientation: vertical, and the text & image: again, a linear layout, orientation: hortizontal.
Image Button: set width to fill_parent. You can match height accordingly.

Android LinearLayout fill-the-middle

I have a vertical, set height (300px) LinearLayout (LL) with 3 nested LLs. 1 and 3rd are set with android:layout_height="wrap_content" and the middle one with android:layout_height="fill_parent". To my dismay, 3rd LL gets pushed out with 2nd one filling parent layout right to the bottom. How do I achieve desired effect since I want potentially resize the outside container with the middle portion expanding and contracting to accommodate the change
Turned out (Thanks Mark Murphy for the answer) that all I was looking for was to set middle row to
layout_height="0px" and layout_weight="1"
If, after all the wrap_content and fixed-sized items are allocated for
along an axis (horizontal or vertical), there is still room on that axis
left over, LinearLayout then allocates the remaining space to those
widgets with specified weights, in proportion to the weight.

Categories

Resources